简体   繁体   English

使用 AJAX 从 PHP 数组填充表格内容

[英]Populate table contents from PHP array using AJAX

I have a table as below;我有一张如下表;

<table>
<tr>
<td id="prev">prev</td>
<td class="content"></td>
<td class="content"></td>
<td class="content"></td>
<td class="content"></td>
<td class="content"></td>
<td id="next">next</td>
</tr>
</table>

and a PHP file, ajax.php for AJAX calls as;和一个 PHP 文件, ajax.php用于 AJAX 调用;

<?php
$array = array(1,2,3,4,5,6,7,8,9,10);
$page = $_POST["page"];
$quantity = 5;
$offset = ($page-1)*$quantity;
$selectedFiles = array_slice($array, $offset, $quantity);
echo $selectedFiles;
?>

The PHP function is suppose to return an array with a limited number of elements with respect to $_POST["page"] like in pagination. PHP 函数假设返回一个数组,其中的元素数量有限,与$_POST["page"]类似,就像在分页中一样。 The script will return first 5 elements for $page = 1 , second 5 elements for $page = 2 , etc..etc.. When page loads, the <td> s having id content may display 1,2,3,4 and 5 respectively.该脚本将返回第一5元素$page = 1 ,第二5个元素$page = 2 ,etc..etc。当页面加载时,所述<td>小号具有ID content可以显示1,2,3,4和5 分别。

When user click next , it may display next results and may return to previous result if user click prev .当用户点击next ,它可能会显示下一个结果,可能会返回到前一个结果,如果用户点击prev How can I do this using JavaScript using jQuery?我如何使用 jQuery 使用 JavaScript 来做到这一点?

It will be more helpful if some effect is given when user clicks and data changes, like sliding (transition), so that it will look like sliding some bar.如果在用户点击和数据变化时给出一些效果会更有帮助,比如滑动(过渡),使其看起来像滑动某个条。

Save yourself a TON of time.为自己节省大量时间。 Use a pre-done grid solution like DataTables that does all this work for you.使用预先完成的网格解决方案,如DataTables ,可为您完成所有这些工作。 It allows you to sort, filter, paginate, order, and limit your table results that can be fed via the dom, JSON, or true server-side AJAX.它允许您对可以通过 dom、JSON 或真正的服务器端 AJAX 提供的表结果进行排序、过滤、分页、排序和限制。

Since DataTables is such a mature project, it has already overcome all the random issues with cross-browser quirks, etc.由于 DataTables 是一个如此成熟的项目,它已经克服了跨浏览器怪癖等所有随机问题。

It also includes a pre-done PHP example with the query done for you.它还包括一个预先完成的 PHP 示例,其中包含为您完成的查询。 Just change to match your table, and voila!只需更改以匹配您的桌子,瞧!

This should help you get started:这应该可以帮助您入门:

<table id="pageLinks">
<tr>
<td id="prev">prev</td>
<td class="content">1</td>
<td class="content">2</td>
...
<td id="next">next</td>
</tr>
</table>

var currPage = 1;
$("#pageLinks tr td").click(function() {
    if($(this).hasClass("content")) {
        var currPage = $(this).text();
    } else {
        if(this.id == "next") {
            currPage++;
        } else {
            if(currPage > 1)
                currPage--;
        }
    }
    $.post("script.php", {currPage: currPage}, function(html) {
        $("#someDiv").hide().html(html).slideUp();
    });
});

Notes:笔记:

  • IDs should not be duplicated in a document, so I've given those cells the class 'content' instead. ID 不应在文档中重复,因此我将这些单元格改为“内容”类。
  • I don't fully understand your question, so I've assumed that the table is for paginated links, and you're loading in the content elsewhere.我不完全理解您的问题,因此我假设该表格用于分页链接,并且您正在其他地方加载内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM