过滤后,JQuery数据表没有更新数据量

时间:2016-09-25 11:07:47

标签: c# jquery ajax datatables server-side

我有一个已经与服务器端处理绑定的JQuery DataTable。但是,一旦我想过滤数据,它就会返回到通用处理程序,它正在被过滤,但它并没有更新数据的数量。下图显示了我的意思:

enter image description here

代码:

HTML:

<table id="tblMember">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Gender</th>
                    <th>Status</th>
                    <th>Account</th>
                    <th>Action</th>
                </tr>
            </thead>
        </table>

JS:

$("#tblMember").DataTable({
            pagingType: "simple_numbers",
            bLengthChange: false,
            bProcessing: true,
            bServerSide: true,
            bSort: false,
            iDisplayLength: 10,
            sAjaxSource: "../Retrieve.ashx",
            fnServerData: function (sSource, aoData, fnCallback) {
                aoData.push({ "name": "GroupAccount", "value": "GroupAccount" })

                $.ajax({
                    type: "POST",
                    data: aoData,
                    url: sSource,
                    dataType: "json",
                    success: function (msg) {
                        fnCallback(msg);
                    }
                });
            },
            columnDefs: [
                {
                    width: "10%",
                    className: "dt-body-center",
                    targets: -1,
                    defaultContent: ["<i class='fa fa-pencil' aria-hidden='true'></i><i class='fa fa-trash-o' aria-hidden='true'></i>"]
                }
            ]
        });

Retrieve.ashx:

public Member Model;

public int sEcho { get; set; }
public string sSearch { get; set; }
public int recordsTotal { get; set; }
public int recordsFiltered { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public IList<string[]> aaData;

public int echo { get; set; }
public int displayLength { get; set; }
public int displayStart { get; set; }
public string sort { get; set; }
public string search { get; set; }
public int sortCol { get; set; }

public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);

GetDataTableRequest(context);

Model = GetMemberData(displayStart, search, displayLength);

List<string[]> aaData = Model.Select(r => new[]
{
    r.Name,
    r.Gender,
    r.Status,
     r.Account
}).ToList();

context.Response.ContentType = ContentType.JSON;
      context.Response.Write(JsonConvert.SerializeObject(SetResponse(echo, search, Model.Count, Model.TotalRows, aaData)));
}

public void GetDataTableRequest(HttpContext context)
{
    this.echo = int.Parse(context.Request.Params["sEcho"]);
    this.displayLength = int.Parse(context.Request.Params["iDisplayLength"]);
    this.displayStart = int.Parse(context.Request.Params["iDisplayStart"]);
    this.sort = (context.Request.Params["sSortDir_0"] ?? "desc").ToString(CultureInfo.CurrentCulture);
    this.search = context.Request.Params["sSearch"];
    this.sortCol = int.Parse(context.Request.Params["iSortCol_0"] ?? (byte.MinValue).ToString());
}

public void SetResponse(int echo, string searchKey, int records, int totalRecords, List<string[]> aaData)
{
    this.sEcho = echo;
    this.sSearch = searchKey;
    this.recordsTotal = records;
    this.recordsFiltered = records;
    this.iTotalRecords = totalRecords;
    this.iTotalDisplayRecords = totalRecords;
    this.aaData = aaData;
}

SQL:

SELECT * FROM [Member] WHERE [GroupAccount] = @GroupAccount AND [Name] LIKE %search% ORDER BY [Name] ASC OFFSET (displayStart / (displayLength - 1)) * displayLength ROWS FETCH NEXT displayLength ROWS ONLY

如何在过滤后更新数据?

0 个答案:

没有答案