1

I am stuck with this issue and will appreciate any help... (Oleg, are you here????)

I have a jqgrid that does sorting, searching on server side, i know need to have pagination also done on the server, i found the webService method that will do that, but when i click on any of the paging buttons nothing happens and the call is not sent to the server.

Can any one help me please and see what i have wrong in my code???

My code is the fallow:

$(myGrid).jqGrid({
    datatype: function (pdata) {
        Invoke("GetAll", pdata);
    },
    colNames: columnNames,
    colModel: columnModel,
    jsonReader: {
        root: "Result",
        page: "page",
        total: "total",
        records: "records"
    },
    rowNum: 10,
    //rowList: [5, 10, 20, 30],
    pager: '#ViewNQueryPager',
    viewrecords: true,
    shrinkToFit: true,
    loadtext: "Loading....",
    emptyrecords: "No records to view",
    viewrecords: true,
    //scrollOffset: 0,
    height: '300',
    //width: '100%',
    ignoreCase: true,
    sortname: 'ID',
    sortable: true,
    sortorder: 'asc',
    grouping: true,
    groupingView: {
        groupField: ['ID']
    }
});
$(myGrid).jqGrid('navGrid', '#ViewNQueryPager', { del: false, add: false, edit: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: true, showQuery: true, onSearch: function (response) { showQueryDetails(); } });
$(myGrid).jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true });
$(myGrid).fluidGrid({ base: '#tableBox', offset: -20 });

function Invoke(action, pdata) {

    var request = new Object();
    if (pdata.filters == undefined && pdata._search == false)
        request.Action = "Sort";
    else {
        if (pdata.filters != undefined && pdata._search == false)
            request.Action = action;
        else request.Action = "Filter";
    }
    if (pdata) {
        request.SortIndex = pdata.sidx;
        request.SortOrder = pdata.sord;
        request.PageNumber = pdata.page;
        request.PageSize = pdata.rows;
        request._search = pdata._search;
        request.filters = pdata.filters;
    }

    var cRequest = new Object();
    cRequest.request = request;

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: 'WebService.asmx/Get',
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        dataType: "json",
        data: JSON.stringify(cRequest),
        success: function (xhrResponse, textStatus) {
            var data = xhrResponse.d;
            var table = $('#ViewNQueryTable');
            table.clearGridData();
            //table.total = data.total;

            for (var i = 0; i < data.Result.length; i++) {
                table.addRowData(i + 1, data.Result[i], 'last');
            }
            $('#totalRecordsFound').html(data.records + " Customers");
            pdata.filters = undefined;
            pdata._search = false;
        },
        error: function (data, textStatus) {
            alert("Error fetching data");
        }
    });
}

I tried adding this:

onPaging: function (which_button) {
        doSomething()
    } 

But it didnt help.

How can i get the paging buttons to call the server method?

EDIT

My web method is this:

[WebMethod]
public kResponse Get(kRequest request)
{
    if (count == 0)
    {
        CurrentList = JsonHelper.GetPersons();
        count++;
    }

    var response = new kResponse();

    switch (request.Action)
    {
        case "GetAll":
            var result = new List<Person>();
            var list = JsonHelper.GetPersons();
            CurrentList = list;
            response.records = CurrentList.Count;
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            for (int i = 0; i < 10; i++)
            {
                result.Add(list[i]);    
            }

            //response.Result = result;
            response.Result = list;

            break;
        case "Filter":
            var filterParams = Filter.Create(request.filters);
            List<Person> FilterdList = GetFilteredList(filterParams);
            CurrentList = FilterdList;
            response.Result = CurrentList;
            response.records = CurrentList.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            break;

        case "Sort":
            //var listPersons = JsonHelper.GetPersons();
            IQueryable<Person> SortedList = ApplySort(CurrentList.AsQueryable(), request.SortIndex, request.SortOrder);
            CurrentList = SortedList.ToList();
            response.Result = CurrentList;
            response.records = CurrentList.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            break;
        case "NextPage":
            List<Person> allList = JsonHelper.GetPersons();
            IQueryable<Person> NextPagelist = allList.AsQueryable();
            NextPagelist = NextPagelist.Skip(request.PageNumber * request.PageSize).Take(request.PageSize).AsQueryable();
            response.Result = NextPagelist;
            response.records = NextPagelist.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber++;


            break;
    }
    return response;
}

with the class:

public class kRequest

public string Action { get; set; }
public int PageSize { get; set; }
public int PageNumber { get; set; }
public string SortIndex { get; set; }
public string SortOrder { get; set; }
public string Search { get; set; }
public bool _search { get; set; }

public string filters { get; set; }

and

public class kResponse

//public int Total { get; set; }
public object Result { get; set; }
public int page { get; set; }
public int total { get; set; }
public int records { get; set; }
//public GridRow[] rows { get; set; }

for doing the pager i am trying this:

$('#next_ViewNQueryPager').click(function () {

    grid.Action = "NextPage";
    triggerReloadGrid();

});

but i am sure this is not the way... what is??

Ovi
  • 2,459
  • 9
  • 50
  • 72
  • Could you include the signature of the web service method which you Which version of jqGrid you use? call. You have very strange parameters and I really can't understand why you do this. For example the method name is `Get` and you use HTTP POST. You include `Action` property in the `request` parameter based on `_search` and `filters` parameters, but still send `_search` and `filters` to the server. Has you *already implemented* paging, sorting and filtering on the server side and have only problem with calling of the web method? – Oleg Dec 13 '11 at 10:25
  • @Oleg, Thank you for answering. i edited the question. i sent the Action on order to know what case to go in the webService. i had a felling i was doing something wrong, any ideas how to fix this? also the implementation of paging, sorting and filtering is not complete it is now working on fake data. – Ovi Dec 13 '11 at 10:57
  • @Oleg, i edited again, i appreciate your help, i really need it.. – Ovi Dec 13 '11 at 11:10

1 Answers1

2

I don't see any requirement to have additional Action parameter. The problem come probably from the misunderstanding which data are requested from the server by jqGrid in every standard request. The problem is that sorting, paging and filtering should be always analyzed and applied by the web server and all the "actions" can be combined together.

For example you show data from the database in the grid having two columns: product name and the unit price of every product. Let us you sort the grid by product name as the initial jqGrid option (sortname is 'ProductName') and you use the default value for rowNum: 20. After the initial grid filling the user set the filter to get products with the price less then $100. In the case the _search parameter will be set to true and the filters will be set to the string where the filter is encoded in JSON format (see the documentation). So the web method of your web service will be called with the parameters _search = true, filters = some value, page = 1, rows = 20, sidx ='ProductName' and sord = 'asc'. Let us the resulting list will be 55 products. So you will have 3 pages of the results and the web service have to return the first page corresponds to the input parameter page = 1. Then the user can type manually 3 as the page number and press Enter or the user can click on the header of the 'Price' column to sort the products by price. In any way ythe web method will receive new request with new values of page, rows, sidx, sord, _search parameters and optionally with the filters parameter.

So the server should always test whether _search is true. In the case the server should apply the filter to the original SQL query which get all products. Then the results have to be always sorted corresponds to the value from sidx and sord parameters. At the end the web server should calculates the total number of the filtered resulting rows and return it as the value of the records output parameter. In the same way the web service should calculates the total number of the filtered pages and return it in the total output parameter. At the end the web service should return up to rows records (one page) based on the values of input parameters page and rows.

I tried to explained that you really don't need to define "GetAll", "Sort", "Filter", "NextPage" etc action and the web service should just always take in consideration all input parameters.

By the way if you want rename and input parameter you can use prmNames (see here). In the old answer I explained how you can implement call of the web service directly using datatype: 'json' instead of datatype as function.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you, i will take off the `Action` parameters. I now understood the way it works. but still even if `rowNum:10` and total records is 25, it doesnt separate it to 2 pages, why could that be? – Ovi Dec 13 '11 at 11:55
  • Can you also please show me how to implement those function on the server? – Ovi Dec 13 '11 at 12:00
  • @Ovi: I suppose that your server place wrong value in `total` or probably `records` output parameters. Moreover it could place in the response more as `10` rows. You should just set breakpoint on the `return response;` statement and examine exactly all properties of the `response` output. – Oleg Dec 13 '11 at 12:03
  • @Oli: I posted you already [the link](http://stackoverflow.com/a/3161542/315935). The exact implementation of the server code depend on the technology which you use for the access to the database: Entity Framework, LINQ to SQL or `SqlDataReader` for example. In case of the usage EF you can get the code which you need from [the answer](http://stackoverflow.com/a/5501644/315935). The MVC part is absolutely independent from the part where sorting, paging and filtering are used. You can just download the demos and combine what you need from the both demos. – Oleg Dec 13 '11 at 12:08
  • ok, your example is great... and things are working now... thanks. – Ovi Dec 13 '11 at 12:14
  • By the way, can you also tell me how to implement Grouping on the server?? Thank you again for all you help.... – Ovi Dec 13 '11 at 12:14
  • @Oli: If you use grouping feature of jqGrid with server based data you should analyse the `sidx` parameter after the comma [the documentation](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:grouping). – Oleg Dec 13 '11 at 12:25
  • Thanks a million... you helped allot – Ovi Dec 13 '11 at 12:29
  • @Oli: If your problem is solved you can "accept" the answer. Moreover it seems that you never vote up any answer or question. You have right to vote up to 30 answers or questions *per day* (see [here](http://meta.stackexchange.com/a/5213/147495)). Voting not only gives the reputation points. The main advantage of voting in in the searching engine. If you want *help other* visitors of some page to find the answer or the question you should vote any helpful answer/question which you read. It'is one of the most important right which you has. I recommend you to vote old answers on your questions. – Oleg Dec 13 '11 at 12:35
  • @Oli: You are welcome! I hope you understood me correct. I personally have enough reputation points which I can't use. On the other side there are [the common rule](http://stackoverflow.com/faq#howtoask): "**As you see new answers to your question, vote up the helpful ones by clicking the upward pointing arrow to the left of the answer**". So if you not vote up answers of other (because you just don't know the rule) you can be misunderstood and you could receive less answers as in case if you would use voting permanently. I wanted only that you will be not misunderstood. Best regards. – Oleg Dec 13 '11 at 13:22
  • can you please take a quick look at my new question, ans see if you can find the problem?? Thanks [link](http://stackoverflow.com/questions/8500878/jqgrid-cant-select-rows-cannot-call-method-indexof-of-undefined) – Ovi Dec 14 '11 at 07:36
  • @Oleg what are the output parameters from server to jQGrid during server side processing ? – Shamseer K Sep 08 '17 at 11:29
  • @ShamseerKSmr: The server response can have *different* format. `jsonReader` (or `xmlReader`) should inform jqGrid where the required information fields are exactly. The server have to return information about `page` (the page number of returned data), `rows` (the page size), `total` (the total number of pages of the size `rows`), `records` (the total number of rows/records) and the `rows` (the *array* of items with the data, inclusive the rowids). – Oleg Sep 08 '17 at 11:35
  • @Oleg rows means page size or item array ? – Shamseer K Sep 08 '17 at 11:46
  • @ShamseerKSmr: I have to correct the previous comment: the `rows` of the server *response* contains the array of items with the data, inclusive the rowids. – Oleg Sep 08 '17 at 11:54
  • @Oleg thanks, Is there any official documentation describing jqgrid operators and short form(send to server side). eg ; contains -> cn – Shamseer K Sep 08 '17 at 12:22
  • 1
    @ShamseerKSmr: The "standard" operators are defined [here](https://github.com/free-jqgrid/jqGrid/blob/master/js/i18n/grid.locale-en.js#L61-L76). [free jqGrid]() fork, which I develop, allows to define custom searching operation. The description of the feature and more information about the "standard" search operators can be found in [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Custom-filtering-searching-Operation). – Oleg Sep 08 '17 at 12:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153955/discussion-between-shamseer-k-smr-and-oleg). – Shamseer K Sep 08 '17 at 12:41