簡體   English   中英

將jQuery數據表與ASP.NET MVC一起使用時如何更新模型數據

[英]How to update model data when using jQuery Data table with ASP.NET MVC

我有大量從JSON返回的數據,並且要進行排序,過濾和分頁,我正在使用jQuery dataTable。

這非常適合排序,搜索和分頁。

但是,當我嘗試將模型與View控件綁定時,我遇到了一個問題。

例如,我返回了以下JSON,

{
    "data":
    [
    {"IsSelected":true,"Name":"SMyDataPoint__01"},
    {"IsSelected":true,"Name":"SMyDataPoint__04"},
    {"IsSelected":true,"Name":"SMyDataPoint__07"},
    {"IsSelected":true,"Name":"SMyDataPoint__08"},
    {"IsSelected":true,"Name":"SMyDataPoint__09"},
    {"IsSelected":true,"Name":"SMyDataPoint__10"},
    {"IsSelected":true,"Name":"SMyDataPoint__11"}
    ]
}

現在,我使用jQuery在瀏覽器中填充json數據,

$('#myTableName').DataTable(
        {
            "ajax": {
                "url": "/API/Loaddata",
                "type": "GET",
                "datatype": "json"
            },
            "columns": [
                {
                    "data": "IsSelected",
                    "render": function (data, type, row) {
                        if (type === 'display') {
                            return '<input type="checkbox" class="editor-active">';
                        }
                        return data;
                    },
                    "className": "dt-body-center"
                   // "autoWidth": true
                },
                { "data": "Name", "autoWidth": true }
            ],
            "rowCallback": function (row, data) {
                // Set the checked state of the checkbox in the table
                $('input.editor-active', row).prop('checked', data.active == 1);
            }
        }
    );

盡管我從JSON中將ISelected設置為true,但在UI中,它們並未被選中。

您顯示的json對象沒有名為active的屬性(因此data.active將返回undefined )。 使用IsSelected設置checked屬性。

$('#myTableName').DataTable({
   ....
   "rowCallback": function (row, data) {
        // Set the checked state of the checkbox in the table
       $('input.editor-active', row).prop('checked', data.IsSelected);
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM