![](/img/trans.png)
[英]How do we set the value of an HTML element using Jquery and ASP.NET MVC 4 complex model data
[英]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.