Javascript 从服务器MVC重新加载JQuery数据表
我有一个JQuery的Datatable,它是在第一次页面加载时生成的。我正在尝试根据selectlist中的选定条件刷新它 我的Datatable首先初始化,如下代码所示Javascript 从服务器MVC重新加载JQuery数据表,javascript,jquery,asp.net-mvc,datatables,Javascript,Jquery,Asp.net Mvc,Datatables,我有一个JQuery的Datatable,它是在第一次页面加载时生成的。我正在尝试根据selectlist中的选定条件刷新它 我的Datatable首先初始化,如下代码所示 <table class="table table-striped table-hover" id="dataTable" width="100%" cellspacing="0"> <thead>
<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>
我的数据如下所示,来自开发人员工具:
请帮我解决这个问题。。。提前感谢。经过长时间的尝试和脱发。。我找到了一个解决方案,清除并再次添加行,而不是
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>
错误显示datatable试图访问已销毁的数据。datatable.clear();新数据之前可能需要。谢谢,但我试过了,没什么变化
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;
}
<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>
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;
}