Paginate records in server side procesing

Paginate records in server side procesing

alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

hi. i am using server-side procesing in my proyect MVC5. im showing 10 record in 1 page.
but i want show 50 records (5 pages with 10 records each). the buttons next and previous are disable and i dont know why.

what i doing wrong???

here is mi code

$('#tblArticulos').DataTable({
        "processing": true,
        "serverSide": true,        
        "ajax": {
            "url": "LoadArticulos",
            "type": "POST",
            "datatype": "json"
        },
        "columns": [
            { "data": "NArticulo", "name": "NArticulo", "autoWidth": true },
            { "data": "CodigoDeBarras", "name": "CodigoDeBarras", "autoWidth": true },
            { "data": "Descripcion", "name": "Descripcion", "autoWidth": true },
            { "data": "descF", "name": "descF", "autoWidth": true },
            { "data": "UM", "name": "UND. MEDIDA", "autoWidth": true },
            { "data": "PrecioVenta", "name": "PrecioVenta", "autoWidth": true },
            { "data": "CostoPromedio", "name": "CostoPromedio", "autoWidth": true },
            { "data": "EsLote", "name": "EsLote", "autoWidth": true }

        ]
    });

and here is mi controller

            var draw = Request.Form.GetValues("draw").FirstOrDefault();
            var start = Request.Form.GetValues("start").FirstOrDefault();
            var length = Request.Form.GetValues("length").FirstOrDefault();

            var sortColumn = Request.Form.GetValues("Columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
            var sortColumDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
            var search = Request.Form.GetValues("search[value]").FirstOrDefault();
int pageSize = length != null ? Convert.ToInt32(length) : 0;
            int skip = start != null ? Convert.ToInt32(start) : 0;
            int totalRecords = 0;

/*here go my LINQ query*/
////return the result 
            return Json(new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = data }, JsonRequestBehavior.AllowGet);

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,468Questions: 1Answers: 10,467 Site admin

    Most likely the recordsTotal and recordsFiltered parameters are not set to 50.

    I would note that server-side processing with only 50 records is just going to introduce latency into your application. Only when you are into the tens of thousands of records is it worth thinking about normally.

    Allan

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1
    edited September 2017

    im shure that are set to 50, beacouse im set the values manually sometimes

    var art = (from t1 in db.Articulos
                           join t2 in db.Familias on t1.IDFamilia equals t2.IDFamilia
                           join t3 in db.UnidadDeMedidas on t1.IDUnidadDeMedida equals t3.IDUnidadDeMedida
                           into um from um2 in um.DefaultIfEmpty()
    
                           join t4 in db.CBArticulos on t1.IDArticulo equals t4.IDArticulo into cb
                           from fd in cb.DefaultIfEmpty()
                           where t1.Descripcion.Contains(search) || fd.CodigoDeBarras.Contains(search)
                                    || SqlFunctions.StringConvert((double)t1.NArticulo).Contains(search)
                           select new
                           {
                               t1.NArticulo,
                               fd.CodigoDeBarras,
                               t1.Descripcion,
                               descF = t2.Descripcion,
                               t1.EsLote,
                               UM = um2.Descripcion,
                               t1.PrecioVenta,
                               t1.CostoPromedio,
                           }).OrderBy(sortColumn + " " + sortColumnDir).Skip(skip).Take(50);
    
                if (!(string.IsNullOrEmpty(sortColumn)) && string.IsNullOrEmpty(sortColumnDir))
                {                
                    art = art.OrderBy(sortColumn + " " + sortColumnDir);
                }
                var data = art.ToList();
                totalRecords = art.Count();            
                
    
                return Json(new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = data }, JsonRequestBehavior.AllowGet);
    

    the page are show 50 record in 5 page each

  • allanallan Posts: 63,468Questions: 1Answers: 10,467 Site admin

    I'm completely confused. Can you link to a test case showing the issue please?

    The image appears to show that there are 50 records and the pagination control is shown correctly. If it doesn't change page when clicked then the server-side script isn't taking the page parameter into account.

    Allan

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

    really sorry allan
    in this image i take 25 record from mi database, the table lenght is 10. but the tables show me 25 record in 3 pages i want 10 records in each page

    first page->> 1-10
    second page->>11-20
    third page->>21-25

    I need to do it with 25 register for later test with 17000

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Can you link to a test case showing the issue please?

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

    I can not. the policies of my company do not leave me. im desperate

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

    hI, i fix it changing this line

    return Json(new { /*draw = draw, recordsFiltered = totalRecords,*/ recordsTotal = totalRecords, data = data }, JsonRequestBehavior.AllowGet);
    
    

    but now the records filter dont work
    show me "Showing 0 to 0 of 0 entries" always

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1
    edited September 2017 Answer ✓

    @allan now works all.

    the recordsFiltered needs know the total of record (17730 in my case) for that i do a count of my table
    recordsTotal = db.myTable.Count();
    and i pass in the json

                return Json(new { draw = draw, recordsTotal = recordsTotal, data = data , recordsFiltered = recordsTotal }, JsonRequestBehavior.AllowGet);
    
    

    and thats all. working in mvc5.

  • allanallan Posts: 63,468Questions: 1Answers: 10,467 Site admin

    Good to hear it is working now. Thanks for the update.

    Allan

This discussion has been closed.