php和datatable行删除/更新

时间:2012-09-19 17:00:00

标签: jquery datatable auto-update

我使用DataTable显示表这里是一个代码:

$(document).ready(function() {
$('#example').dataTable( {
        "iDisplayLength": 50,
    "sPaginationType": "full_numbers",
     "bStateSave": true
} );
} );
</head>
<body>
             <table class="display" id="example">
            <thead>
                <tr>
                <th class="id-th"><strong>ID</strong></th>
                <th class="date-th"><strong>Date Added</strong></th>
                <th class="fullname-th"><strong>Full name</strong></th>
                <th class="email-th"><strong>Email</strong></th>
                <th class="visits-th"><strong>Visits</strong></th>
                <th class="totalleads-th"><strong>Total Leads</strong></th>
                <th class="uniqueleads-th"><strong>Unique Leads</strong></th>
                <th class="unpaid-th"><strong>Unpaid Leads</strong></th>
                <th class="action-th"><strong>Actions</strong></th>
              </tr>
</thead>
<tbody>
<?php$query = "SELECT * FROM publishers";
$result = mysql_query($query) or die("Query failed: " . mysql_error());while ($row = mysql_fetch_array($result))
{
$publisher_id = $row['id'];
$publisher_datetime = $row['date_time'];
$publisher_firstname = $row['first_name'];
$publisher_lastname = $row['last_name'];
$publisher_email = $row['email']; ?>
                <tr class="">
                <td class="id-th"><strong><?php echo $publisher_id; ?></strong></td>
                <td class="date-th"><strong><?php echo $publisher_datetime; ?></strong></td>
                <td class="fullname-th"><strong><?php echo "$publisher_firstname $publisher_lastname"; ?></strong></td>
                <td class="email-th"><strong><?php echo $publisher_email; ?></strong></td>

                <td class="visits-th"><strong><?php echo $publisher_visits; ?></strong></td>
                <td class="totalleads-th"><strong><?php echo $publisher_total; ?></strong></td>
                <td class="uniqueleads-th"><strong><?php echo $publisher_unique; ?></strong></td>
                <td class="unpaid-th"><strong><?php echo $publisher_unpaid; ?></strong></td>
                <td class="action-th">
                    <ul class="button-table-head">
                        <li><div class="button-head edit-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
                        <li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) {
        jAlert('Publisher deleted');
    });"><span>Delete</span></a></div></li>
                    </ul>
                </td>
              </tr><?php } ?></tbody></table>

所以现在我的问题是 - 当我编辑或删除行时 - 我是否每次都要RELOAD页面才能显示更新?我尝试阅读DataTable上的说明,但我真的不明白。

感谢您的提示!

2 个答案:

答案 0 :(得分:0)

如果您想使用纯PHP,那么

如果您对其他方法持开放态度,例如使用AJAX调用PHP脚本,那么

创建一个PHP脚本,用于更新/添加/删除数据库中的数据,并创建另一个从数据库中检索所需数据的脚本。您可以使用AJAX尽可能多地调用这两种脚本,并继续使用retrieve-data脚本(再次使用AJAX调用)来显示更新的数据。

答案 1 :(得分:0)

您可以进行jquery ajax调用以删除行并刷新数据表以显示正确的分页和正确的记录。 我已经搜索了很多方法来刷新数据表,但我最终使用的功能可以做你想要的一切。 函数取行没有。作为执行删除行操作和刷新数据表的输入。 这就是我所做的。

在for循环中创建表时

。分配号码从0开始作为tr的id。 &GT;

<script>
    table = $('#table_id').datatables();
    $(delete_row).click(function(){
     ..
     var row_no=$(this).closest("tr").attr("id");
     $.ajax({
        ...
        success : function(data) {
        ...
        table.fnDeleteRow(row_no);
     }
     });
     });
</script>