JQuery 从服务器 MVC 重新加载数据表

JQuery Datatable Reload From Server MVC

我在第一页加载时生成了 JQuery 的数据表。我正在尝试根据选择列表中的选定条件来刷新它。

我的数据表首先像下面的代码一样初始化。

<table class="table table-striped table-hover" id="dataTable" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th>Select All <input type="checkbox" class="checkbox" id="chkBoxAll"></th>
            @foreach (System.Data.DataColumn col in Model.DataTypesTable.Columns)
            {
                <th> @col.Caption</th>
            }
        </tr>
    </thead>
    <tbody>

        @foreach (System.Data.DataRow row in Model.DataTypesTable.Rows)
        {
            <tr>
                <td> <input type="checkbox" class="checkbox" name="chkBox" value="@row.ItemArray[0]"></td>
                @foreach (var cell in row.ItemArray)
                {
                    <td>
                        @cell.ToString()
                    </td>
                }
            </tr>
        }
    </tbody>
</table>

<script>
$(document).ready(function() {
  $('#dataTable').DataTable();
});
</script>

一开始初始化的很好。但是,当我尝试在 selectlistchange 事件中重新加载它时,它不会重新加载任何内容并显示这样的错误。

DataTables warning: table id=dataTable - Requested unknown parameter 'Id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

<script type="text/javascript">
    $("#slctDeviceList").change(function () {
        var selectedValue = $("#slctDeviceList option:selected").text();
        $.ajax({
                traditional: true,
                dataType: 'html',
                type: "GET",
                url: '@Url.Action("GetDeviceDataTypes", "Home")',
                data: { slctDeviceList: selectedValue },
                success: function (result) {
                    console.log("Success");
                    console.log(result);

                    $("#dataTable").DataTable({
                        destroy: true,
                        data: result,
                        columns: [
                            { data: "Id", name: "Id" },
                            { data: "Data Name", name: "Data Name" },
                            { data: "Description", name: "Description" },
                            { data: "Device Type", name: "Device Type" }
                        ], columnDefs: [{
                            "defaultContent": "-",
                            "targets": "_all"
                        }]
                    });

                },
                error: function (result) {
                    console.log("error");
                }
        });
     });

</script>

控制器:

public JsonResult GetDeviceDataTypes(string slctDeviceList)
        {
            ChartRepository repository = new ChartRepository();
            System.Data.DataTable dt = repository.GetDataTypes(slctDeviceList);
            var json = this.Json(new { data = dt }/*, _jsonSetting*/);

            return json;
        }

我的开发者工具数据如下:

请帮我解决这个问题...提前致谢。

经过长时间的尝试并失去了头发..我找到了一个明确的解决方案并再次添加行而不是 destroy 命令。下面是解决方案。

<script type="text/javascript">
        $("#slctDeviceList").change(function () {
            var selectedValue = $("#slctDeviceList option:selected").text();

            $.ajax({
                traditional: true,
                dataType: 'json',
                type: "GET",
                url: '@Url.Action("GetDeviceDataTypes", "Home")',
                data: { slctDeviceList: selectedValue },
                success: function (result) {
                    console.log("Success");
                    var dataTable = $("#dataTable").DataTable();
                    dataTable.clear().draw();

                    $.each(result, function myfunc (index, value) {

                        // use data table row.add, then .draw for table refresh
                        dataTable.row.add([
                            '<input type="checkbox" class="checkbox" name="chkBox" value="' + value.Id + '">',
                            value.Id,
                            value.DataName,
                            value.Description,
                            value.DeviceType
                        ]).draw();
                    });
                },
                error: function (result) {
                    console.log("error");
                }
            });
        });
</script>

此外,return 控制器操作中的 json 对象也很重要。

PS。如果 Json 对象有一个像 data 这样的初始标记,您可能需要将循环 value.Id 更改为 value.data.Id。但是最好不要使用任何标签。

public JsonResult GetDeviceDataTypes(string slctDeviceList)
        {
            ChartRepository repository = new ChartRepository();
            System.Data.DataTable dt = repository.GetDataTypes(slctDeviceList);

            JsonSerializerSettings _jsonSetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
            var json = this.Json(dt , _jsonSetting);

            return json;
        }