简体   繁体   English

jQuery/Ajax 成功后重新加载表(不是 Datatable())

[英]Reload Table (NOT Datatable()) after jQuery/Ajax success

I know this topic has been covered multiple times as i've searched and tried many options before posting this so please don't close it.我知道这个话题已经被多次讨论过,因为我在发布之前已经搜索并尝试了很多选项,所以请不要关闭它。

I have a table that shows shift entries from my mysql database.我有一个表格,显示来自我的 mysql 数据库的班次条目。 After I delete a row using jQuery I would like the totals row to be refreshed upon success, or the whole table itself.在我使用 jQuery 删除一行后,我希望在成功时刷新总计行,或者整个表本身。

The issue im experiencing is that the delete and reload process works but it only works once and not multiple times.我遇到的问题是删除和重新加载过程有效,但它只能工作一次而不是多次。

Can anyone help?任何人都可以帮忙吗?

My table:我的桌子:

<table id="cardTable">
  <tr>
    <td>Mon</td>
    <td>11:00 hrs</td>
    <td>£185.00</td>
  </tr>
  <tr>
    <td>Tue</td>
    <td>11:00 hrs</td>
    <td>£100.00</td>
  </tr>
  <tr>
    <td>Wed</td>
    <td>11:00 hrs</td>
    <td>£100.00</td>
  </tr>
  <tr>
    <th>Totals:</th>
    <th>33:00 hrs</th>
    <th>£300.00</th>
  </tr>
</table>

The jQuery: jQuery:

$(document).ready(function(){

    // Delete 
    $('.delete').click(function(){

        var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];

        // AJAX Request
        $.ajax({
            url: 'system/actions/deleteShift.php',
            type: 'POST',
            data: { id:deleteid },
            success: function(response){

                if(response == 1){
                    // Remove row from HTML Table
                    $(el).closest('tr').fadeOut(600,function(){
                        $(this).remove();
                    });
                    $("#cardTable").reload(" #cardTable");
                }
                else {
                    alert('Invalid ID.');
                }

            }
        });

    });

});

I am not sure if it's it pretty, you can try to put everything in a function and reinitialize it after a successfull ajax.我不确定它是否漂亮,您可以尝试将所有内容放入 function 并在成功 ajax 后重新初始化。

$(document).ready(function(){

    initClicks();
    function initClicks() {
        // Delete 
        $('.delete').unbind().click(function(){

            var el = this;
            var id = this.id;
            var splitid = id.split("_");

            // Delete id
            var deleteid = splitid[1];

            // AJAX Request
            $.ajax({
                url: 'system/actions/deleteShift.php',
                type: 'POST',
                data: { id:deleteid },
                success: function(response){

                    if(response == 1){
                        // Remove row from HTML Table
                        $(el).closest('tr').fadeOut(600,function(){
                            $(this).remove();
                        });
                        $("#cardTable").reload(" #cardTable");
                        initClicks();
                    }
                    else {
                        alert('Invalid ID.');
                    }

                }
            });

        });
    }
});

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

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