Jquery数据表不发出数据检索
本文关键字:数据 检索 数据表 Jquery | 更新日期: 2023-09-27 18:10:47
我使用jQuery DataTable通过c# web方法来填充表格的AJAX请求。我的问题是没有返回任何数据或任何错误。有人能帮我吗?
JS代码:var TableAjax = function () {
var initPickers = function () {
//init date pickers
$('.date-picker').datepicker({
rtl: Metronic.isRTL(),
autoclose: true
});
}
var handleRecords = function () {
var grid = new Datatable();
grid.init({
src: $("#datatable_ajax"),
onSuccess: function (grid) {
// execute some code after table records loaded
},
onError: function (grid) {
// execute some code on network or other general error
},
onDataLoad: function (grid) {
// execute some code on ajax data load
},
loadingMessage: 'Loading...',
DataTable: { // here you can define a typical datatable settings from http://datatables.net/usage/options
// Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
// setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/scripts/datatable.js).
// So when dropdowns used the scrollable div should be removed.
//"dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r>t<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>",
"bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.
"lengthMenu": [
[10, 20, 50, 100, 150, -1],
[10, 20, 50, 100, 150, "All"] // change per page values here
],
"pageLength": 10, // default record count per page
"ajax": {
url: "/WS/WS.asmx/GetCustomers",
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
},
"aoColumns": [
{ "mData": "CustomerAddress" },
{ "mData": "CustomerName" },
{ "mData": "CustomerCode" },
{ "mData": "CustomerCode" },
{ "mData": "CustomerCode" },
{ "mData": "CustomerCode" },
{ "mData": "CustomerCode" }
],
"order": [
[1, "asc"]
]// set first column as a default sort by asc
}
});
// handle group actionsubmit button click
grid.getTableWrapper().on('click', '.table-group-action-submit', function (e) {
e.preventDefault();
var action = $(".table-group-action-input", grid.getTableWrapper());
if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
grid.setAjaxParam("CustomerId", "group_action");
grid.setAjaxParam("CustomerName", action.val());
grid.setAjaxParam("id", grid.getSelectedRows());
grid.getDataTable().ajax.reload();
grid.clearAjaxParams();
} else if (action.val() == "") {
Metronic.alert({
type: 'danger',
icon: 'warning',
message: 'Please select an action',
container: grid.getTableWrapper(),
place: 'prepend'
});
} else if (grid.getSelectedRowsCount() === 0) {
Metronic.alert({
type: 'danger',
icon: 'warning',
message: 'No record selected',
container: grid.getTableWrapper(),
place: 'prepend'
});
}
});
}
return {
//main function to initiate the module
init: function () {
initPickers();
handleRecords();
}
};
}();
和我的web方法代码:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public object GetCustomers()
{
DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from t_crm_customers");
List<Customers> lis = new List<Customers>();
lis = ExtensionList.ToListof<Customers>(dt);
return new
{
data = lis,
length = lis.Count
};
}
我的问题不返回任何数据或任何错误。
现在你只是返回一个对象。
试题:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public JsonResult GetCustomers()
{
DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from t_crm_customers");
List<Customers> lis = new List<Customers>();
lis = ExtensionList.ToListof<Customers>(dt);
var toJson = new
{
data = lis,
length = lis.Count
};
return Json(toJson, JsonRequestBehaviour.AllowGet);
}