Douglas Franco
Douglas Franco

Reputation: 601

jqGrid pager doesn't work

My JqGrid loads data from controller, but it doesn't show list pager.

In this case the LINQ's query returns 15 rows, but only shows 10 and can not navigate to the 2nd page to view the last 5 rows.

What am I doing wrong?? Please, can someone help me?

View

        <script type="text/javascript">
            jQuery(document).ready(function () {
                jQuery("#list2").jqGrid({
                    url: '@(Url.Action("GetMet", "Meta"))',
                    datatype: "json",
                    data: "a",
                    colNames: ['Id', 'Nome da Loja', 'Mês', 'Ano', 'Meta Monetária', 'Meta Seguro', 'Meta Ticket Médio'],
                    colModel: [{ name: 'id', index: 'id', width: 20 },
                        { name: 'nome', index: 'nome', width: 120, search: true, stype: 'text' },
                        { name: 'mes', index: 'mes', width: 40 },
                        { name: 'ano', index: 'ano', width: 40 },
                        { name: 'metamonetaria', index: 'metamonetaria', width: 90 },
                        { name: 'metaseguro', index: 'metaseguro', width: 90 },
                        { name: 'metaticketmedio', index: 'metaticketmedio', width: 110 }],
                    rowNum: 10,
                    rowList: [10, 20, 30],
                    pager: '#pager2',
                    sortname: 'nome',
                    viewrecords: true,
                    sortorder: "desc",
                    caption: 'Metas referente a loja selecionada',
                    height: 240,
                    ondblClickRow: function (id) { $("#FUNCIONARIO").val(id); }
                });

                jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false });

Controller

public JsonResult GetMet(int page = 1, int rows = 10, string sord = "asc", string sidx = "Id")
        {
            var db = new DataContext().GetGenericRepository();
            var totalFunc = db.GetAll<Funcionarios>().Count();
            decimal totalPages = 0;
            if (totalFunc > 0)
            {
                totalPages = totalFunc / rows;
                totalPages = System.Math.Round(totalPages);
            }
            else
            {
                totalPages = 0;
            }
            if (page > totalPages)
            {
                page = Convert.ToInt32(totalPages);
            }

            var result = new
            {
                total = totalPages,
                page = page,
                records = totalFunc,
                rows = (from metas in db.GetAll<Metas>()
                        select new
                        {
                            id = metas.METAID,
                            metamonetaria = metas.METAMONETARIA,
                            metaseguro = metas.METASEGURO,
                            metaticketmedio = metas.METATICKETMEDIO,
                            mes = metas.MES,
                            ano = metas.ANO,
                            nome = (from lojas in db.GetAll<Lojas>()
                                   where lojas.LOJAID == metas.LOJAID
                                   select lojas.NOME).First()



                        }).ToArray()
            };


            return Json(result, JsonRequestBehavior.AllowGet);

Upvotes: 0

Views: 152

Answers (1)

tpeczek
tpeczek

Reputation: 24125

Your totalPages calculation is wrong. First of all rows and totalFunc are integers so the operation:

totalPages = totalFunc / rows;

is an integer division regardless of the totalPages being a decimal. In your case the result is 1.

Also the Math.Round() is not appropriate there as it would change 1.4 (case when you have 14 rows and the page size is 10) into 1 (and you would like it to be 2).

You should change this calculation like this:

int totalPages = 0;
if (totalFunc > 0)
    totalPages = (int)Math.Ceiling((decimal)totalFunc / (decimal)rows);
else
    totalPages = 0;

Upvotes: 2

Related Questions