繁体   English   中英

单击按钮后仅刷新 DataTable

[英]Refreshing only DataTable after button click

在将数据插入数据库后,尝试刷新此数据表时遇到了一些麻烦。 我希望在 javascript ajax 上刷新数据表,但我不想刷新整个页面。

我是数据表的新手,似乎不知道该怎么做。

任何帮助将不胜感激。

JavaScript

        $.ajax({
            url: 'post_project_definition',
            type: 'GET',
            data: {
                discID: $("#disciplinesProjDef").val(),
                discComment: $("#txtDiscipComment").val(),
            },
            success: () => {
                swal({
                    type: 'success',
                    title: 'Your comment has been added successfully'
                });
            }
        })
        $.getJSON('selectProjectDefinitionDescription', function (data) {
            $.each(data, function (i, item) {
                ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description, 
                    "<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input>"+
                    "<script>"+
                    "   $('#"+data[i].id+"delete').on('click', () => {" +
                    "       $.ajax({"+
                    "           url: 'delete_project_definition',"+
                    "           type: 'GET',"+
                    "           data: { "+
                    "               project_def_id: '"+data[i].id+"' "+
                    "           },"+
                    "       });"+
                    "   });" +
                    "</script>"
                ]);

                var table = $("#projDiscComTable").DataTable();
                table.clear().draw();
                table = $("#projDiscComTable").DataTable({
                    data: ProjDefTable,
                    columns: [
                        {title: "ID"},
                        { title: "Discipline Name" },
                        { title: "Discipline Description" },
                        { title: "Delete" },
                    ],
                    destroy: true
                });
            })//end else
        });
    });

PHP

public function selectProjectDefinitionDescription()
{
    $session_data = $this->session->userdata('logged_in');
    $id = $session_data['id'];
    $type =  $session_data['usertype'];
    $project_id = $session_data['project_id'];

    $stuff = $this->Projects_Model->load_Project_Definition_Discipline($project_id);
    echo $stuff;
}

HTML

<table id="projDiscComTable" class="table table-hover display">
  <thead>
   <tr>
    <th>ID</th>
    <th>Disciple</th>
    <th>Discipline Comment</th>
    <th>Delete</th>
   </tr>
  </thead>
</table>

我没有太多使用DataTable ,但我相信它们被用来为您的 HTML 表提供各种功能,例如轻松排序等。 参考

说到这里的问题,我相信您的 scope 是根据 AJAX 响应刷新表格的内容,而无需刷新整个页面。

您可以使用 jQuery 的append()remove()函数来完成。 一个简单的算法是这样的:

  1. 首先,您可以在页面某处隐藏一行 HTML 示例,如下所示:
<div id="definition-row-sample">
    <tr class="definition-row">
        <td class="definition-id"></td>
        <td class="definition-name"></td>
        <td class="definition-description"></td>
        <td class="definition-delete"></td>
    </tr>
</div>
  1. 隐藏.definition-row使其不会显示在您的页面中。
  2. 现在,当您通过 AJAX 获取数据时,首先使用$('.definition-row').remove()删除所有行,然后遍历您的数据和 append 表中的每一行:
    $.each(data, function (i, item) {
        $('#definition-row-sample').find('.definition-id').text(data[i].id);
        $('#definition-row-sample').find('.definition-name').text(data[i].definition_name);
        $('#definition-row-sample').find('.definition-description').text(data[i].description);
        //then simply append it to the table
        let content = $('#definition-row-sample').find('.definition-row').clone();
        $('#projDiscComTable').append(content);
    });

至于删除,您可以定义一个普通的 function ,如:

function definition_delete(id) {
    $.ajax({
        url: 'delete_project_definition',
        type: 'GET',
        data: {
           project_def_id:id
        }
    });   
}

document.on('click', '.definition-delete', function(){
    let id = $(this).parent().find('.definition-id').text();
    definition_delete(id);
});

我还没有测试过代码,但我相信你明白了这个想法并让它工作,因为我也使用了与我的一个项目类似的逻辑。

插入评论后,刷新表格怎么样?

$('#projDiscComTable').DataTable().ajax.reload();

$.ajax({
            url: 'post_project_definition',
            type: 'GET',
            data: {
                discID: $("#disciplinesProjDef").val(),
                discComment: $("#txtDiscipComment").val(),
            },
            success: () => {
                $('#projDiscComTable').DataTable().ajax.reload();
                swal({
                    type: 'success',
                    title: 'Your comment has been added successfully'
                });
            }
        })

不确定这是否符合您的要求,但您仍然可以在插入评论后刷新它。

我想通了,我在 ajax 的成功中重新绘制了表格,从我所看到的情况来看,它似乎有效。 但谢谢你所有的答案。 我将使用你们将来提出的建议。

var ProjDefTable = [];
$('#btnDiscipCommPost').on('click', () => {
    $.ajax({
        url: 'post_project_definition',
        type: 'GET',
        data: {
            discID: $("#disciplinesProjDef").val(),
            discComment: $("#txtDiscipComment").val(),
        },
        success: () => {
            swal({
                type: 'success',
                title: 'Your comment has been added successfully'
            });
            $.getJSON('selectProjectDefinitionDescription', function (data) {
                $.each(data, (i) => {
                    ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description, 
                    "<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input> <span id='" + data[i].id + "deleted' style='display: none; font-weight: 700;'>DELETED</span>"+
                    "<script>"+
                    "   $('#"+data[i].id+"delete').on('click', () => {" +
                    "       $.ajax({"+
                    "           url: 'delete_project_definition',"+
                    "           type: 'GET',"+
                    "           data: { "+
                    "               project_def_id: '"+data[i].id+"' "+
                    "           },"+
                    "           success: () => {"+
                    "               var x = document.getElementById('" + data[i].id + "delete');" +
                    "               x.style.display = 'none';" +
                    "               var y = document.getElementById('" + data[i].id + "deleted');" +
                    "               y.style.display = 'block';" +
                    "           } "+
                    "       });"+
                    "   });" +
                    "</script>"
                    ]);

                    var table = $("#projDiscComTable").DataTable();
                    table.clear().draw();
                    table = $("#projDiscComTable").DataTable({
                        data: ProjDefTable,
                        columns: [
                            {title: "ID"},
                            { title: "Discipline Name" },
                            { title: "Discipline Description" },
                            { title: "Delete" },
                        ],
                        destroy: true
                    });
                })//end else
            });
            var ProjDefTable = [];
        }
    })
});

暂无
暂无

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

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