jQuery Datatable:分页和过滤器不正确显示 [英] jQuery Datatable: pagination and filter not display correctly
问题描述
我不知道如何解决这个问题,尝试一整天,但没有成功修复分页。我正在使用jQuery datatable,并显示我的巨大数据,我正在使用服务器端。
作为测试,只能将10行数据调用到表中。然后在传递给表之前,我使用这个
数据过滤器和分页不正确,应该只有1页的分页。
我有一个解决方案。您应该直接修改您的回复对象。
dataSrc:function(data){
var json = $ .parseJSON(data.d);
data.draw = parseInt(json.otherData [0] .draw);
data.recordsTotal = parseInt(json.otherData [0] .recordsTotal);
data.recordsFiltered = parseInt(json.otherData [0] .recordsFiltered);
data.data = json.searchData;
return data.data;
}
希望它适用于您的情况。
I dont know how to solve this, try a whole day but didn't success to fix the pagination. I'm using jQuery datatable, and to display my huge data, I'm using server side.
As a testing, only call 10 row of data to the table. Then before pass to table, I restructured the data inside dataSrc, using this solution . The table display successfully but the pagination and filter not display correctly.
Can anyone help this.
Below is my code.
AJAX
$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
type: "POST",
contentType: "application/json; charset=utf-8",
url: "datatables.aspx/GetData",
'data': function (data) {
return JSON.stringify(data);
},
"dataSrc": function (data) {
var json = $.parseJSON(data.d);
var myData = {};
myData.draw = parseInt(json.otherData[0].draw);
myData.recordsTotal = parseInt(json.otherData[0].recordsTotal);
myData.recordsFiltered = parseInt(json.otherData[0].recordsFiltered);
myData.data = json.searchData;
return myData.data;
}
},
"columns": [
{ "data": "Username" }
]
}
});
C#
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static string GetData(int draw, object columns, object order, int start, int length, object search)
{
string constr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string mySql = "SELECT TOP 10 username AS Username FROM user_lookup";
using (SqlCommand cmd = new SqlCommand(mySql, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds, "searchData");
DataTable newDT = new DataTable("otherData");
//Add columns to DataTable.
newDT.Columns.AddRange(new DataColumn[4] {
new DataColumn("draw"),
new DataColumn("recordsTotal"),
new DataColumn("recordsFiltered"),
new DataColumn("userRole")
});
//Add rows to DataTable.
newDT.Rows.Add(draw, length, start, "myrole");
ds.Tables.Add(newDT);
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(ds);
return JSONString;
}
}
}
}
This is the data I return back to datatable to structured the table.
Data filter and pagination not correct, should be only 1 page of pagination.
I have a solution for our case. You should modify the object of your response directly.
"dataSrc": function (data) {
var json = $.parseJSON(data.d);
data.draw = parseInt(json.otherData[0].draw);
data.recordsTotal = parseInt(json.otherData[0].recordsTotal);
data.recordsFiltered = parseInt(json.otherData[0].recordsFiltered);
data.data = json.searchData;
return data.data;
}
Hopefully, it works in your case.
这篇关于jQuery Datatable:分页和过滤器不正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!