JQuery datatable server side processing didnt update number of data after filtering

JQuery datatable server side processing didnt update number of data after filtering

NiesNies Posts: 3Questions: 2Answers: 0
edited September 2016 in Free community support

I have a JQuery DataTable which already bind with Server Side Processing. However, once I want to filter the data, it return to the Generic Handler, it is being filtered, but it didn't update the number of data. Image below shows what I mean:

The code:

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

How can I update the number of data after filtering? as well as the pagination records.

Answers

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    It might be that the sEcho isn't being correctly returned, but without a test case (required in the forum rules) its impossible to say I'm afraid.

    Allan

This discussion has been closed.