0

I'm struggling on how to configure a jqGrid past the simple paged examples given in demo code.

My needs are such:

1) don't need paging, I want the grid to be a viewport and scroll all the items (which are limited already to under 400 rows)

2) use the response from a ajax call load the json data.

3) google like single text box that I can use to filter the data.

All of the samples that I come across are always using paging, and I know that that is the most common use case.

Regards, Stephen

BTW

jqGrid is the most documented grid I've come across, it rocks in my book, and the fact that Oleg and Co. are very aggressive in getting responses back to questions makes the learning curve and hence adoption quite smooth.

EDIT 1

 var myGrid = $('#favoriteGrid'),
            decodeErrorMessage = function (jqXHR, textStatus, errorThrown) {
                var html, errorInfo, i, errorText = textStatus + '\n' + errorThrown;
                if (jqXHR.responseText.charAt(0) === '[') {
                    try {
                        errorInfo = $.parseJSON(jqXHR.responseText);
                        errorText = "";
                        for (i = 0; i < errorInfo.length; i++) {
                            if (errorText.length !== 0) {
                                errorText += "<hr/>";
                            }
                            errorText += errorInfo[i].Source + ": " + errorInfo[i].Message;
                        }
                    }
                    catch (e) { }
                } else {
                    html = /<body.*?>([\s\S]*)<\/body>/.exec(jqXHR.responseText);
                    if (html !== null && html.length > 1) {
                        errorText = html[1];
                    }
                }
                return errorText;
            };


    myGrid.jqGrid({
        url: '/Buyer/Favorite/ProductGroupService/2976171424',
        datatype: 'json',
        mtype: 'GET',
        height: '100%',
        colNames: ['Row No', 'Qty', 'Features', 'Item Nbr', 'Brand', 'Product', 'Supplier', 'Price', 'UOM', 'Case Pack', 'Remarks', 'Ave Weight', 'Par', 'Last Purchase', 'Sort'],
        colModel: [
            { name: 'Id', index: 'Id', hidden: true },
            { name: 'Quantity', index: 'Quantity', width: 20, editable: true, edittype: 'text', editoptions: { size: 10, maxlength: 15} },
            { name: 'ProductAttributes', index: 'ProductAttributes', width: 50 },
            { name: 'ItemNum', index: 'ItemNum', width: 60, align: "right" },
            { name: 'BrandName', index: 'BrandName', width: 100, align: "left" },
            { name: 'ProducName', index: 'ProducName', width: 150, align: "left" },
            { name: 'SupplierName', index: 'SupplierName', width: 100, align: "left" },
            { name: 'Price', index: 'Price', width: 80, align: "right" },
            { name: 'UOM', index: 'UOM', width: 80, align: "left" },
            { name: 'CasePack', index: 'CasePack', width: 80, align: "left" },
            { name: 'PackageRemarks', index: 'PackageRemarks', width: 80, align: "left" },
            { name: 'AveWeight', index: 'AveWeight', width: 80, align: "right" },
            { name: 'Par', index: 'Par', width: 80, align: "right" },
            { name: 'LastPurchaseDate', index: 'LastPurchaseDate', width: 80, align: "right" },
            { name: 'SortPriority', index: 'SortPriority', hidden: true }
        ],
        multiselect: true,
        //          cellEdit : true,
        //          cellsubmit : 'remote',
        pager: '#favoritePager',
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: 'desc',
        rownumbers: true,
        viewrecords: true,
        altRows: true,
        altclass: 'myAltRowClass',
        height: '100%',
        gridview: true,
        jsonReader: { cell: "" },
        loadonce: true,
        caption: "Products List",
        loadError: function (jqXHR, textStatus, errorThrown) {
            // remove error div if exist
            $('#' + this.id + '_err').remove();
            // insert div with the error description before the grid
            myGrid.closest('div.ui-jqgrid').before(
                '<div id="' + this.id + '_err" style="max-width:' + this.style.width +
                ';"><div class="ui-state-error ui-corner-all" style="padding:0.7em;float:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin-right: .3em;"></span><span style="clear:left">' +
                decodeErrorMessage(jqXHR, textStatus, errorThrown) + '</span></div><div style="clear:left"/></div>')
        },
        loadComplete: function () { 
            // remove error div if exist
            $('#' + this.id + '_err').remove();
        },
        ondblClickRow: function (rowid, ri, ci) {
            // 
        },
        gridComplete: function () { 
            $("#favoriteGrid").addClass("nodrag nodrop");
            $("#favoriteGrid").tableDnDUpdate();
        }
    });

EDIT 2 Implemented recommendations from Oleg re: json url and removal of drag n drop

EDIT 3 Added JSON response from server

{
"total":321,
"page":1,
"records":1000,
"rows":[
{"Id":0,"Selected":false,"Quantity":1,"ProductAttributes":null,"ItemNum":"6199335","BrandName":"KELLOGG","ProducName":"CEREAL ASST FAMILY PACK","SupplierName":"SYSCO","Price":33.89,"UOM":"CA","CasePack":"72 PK","PackageRemarks":"INDIV","AveWeight":"4.59 LB","Par":null,"SortPriority":19,"LastPurchaseDate":null,"GLCode":"7115-10","OldProductId":null,"CategoryName":"Food-Canned Goods","ProductID":2285668889,"ImageInfo":null},
{"Id":1,"Selected":false,"Quantity":1,"ProductAttributes":null,"ItemNum":"6199335","BrandName":"KELLOGG","ProducName":"CEREAL ASST FAMILY PACK","SupplierName":"SYSCO","Price":34.19,"UOM":"CA","CasePack":"72 PK","PackageRemarks":"INDIV","AveWeight":"4.59 LB","Par":null,"SortPriority":19,"LastPurchaseDate":null,"GLCode":"7115-10","OldProductId":null,"CategoryName":"Food-Canned Goods","ProductID":2285668889,"ImageInfo":null}
]}
Stephen Patten
  • 6,333
  • 10
  • 50
  • 84
  • What problem has the current code? Are the grid data loaded? If not you should post the JSON data or a part of server code (the MVC action which return JSON). Another question: why you use `tableDnDUpdate`? Is it for sorting rows? You can use existing jqGrid functionality. See [sortableRows](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#sortable_rows) or [gridDnD](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#drag_and_drop_rows_between_grids) – Oleg Dec 10 '11 at 11:19
  • 1
    Another remark to the code: you should never use `http://hostname` prefix in the `url` because the URL will be used in Ajax call which can access only the same server on the same port as the current `window.location`. So you should use `url: '/Com.BuyEfficient.Web/...'`. `align: "leftt"` is *default* value of the property (see **Default** column on [the table](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options)). – Oleg Dec 10 '11 at 11:56
  • @Oleg Implemented small fixes based on your advice, but the grid does not show the data, although it looks like it bound to it. – Stephen Patten Dec 10 '11 at 17:41
  • 1
    You use wrong `jsonReader: { cell: "" }` you should replace it to `jsonReader: { repeatitems: false }`. It will be correspond to the data which you send. If you use `loadonce: true` the data will be loaded *once*. So you can reduce the JSON to `[{"Id":0,"Selected":false,"Quantity":1,...},{"Id":1,"Selected":false,"Quantity":1,...}]`, use `jsonReader: { repeatitems: false, id: "Id", root: function (obj) { return obj; }, page: function () { return 1; }, total: function () { return 1; }, records: function (obj) { return obj.length; }`. Additionally you can remove `Id` column from the grid. }` – Oleg Dec 10 '11 at 18:03
  • My email is: oleg.kiriljuk@ok-soft-gmbh.com – Oleg Dec 10 '11 at 22:39
  • @Oleg I emailed you the info.. – Stephen Patten Dec 11 '11 at 15:55

1 Answers1

1

It you would use loadonce: true you can and should return all the data at once (but correct sorted data). After the first load of the grid the datatype will be automatically changed from 'json' to 'local', so no Ajax requests will be more done. You can additionally use local paging of data, sorting or filtering with respect of either searching filter or advanced searching dialog or ever both (see the demo from the answer). All will work without need to write any server code.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg. That has cut down on the traffic across the wire. Now how do I turn off the the xtra paging meta data in the grid and json payload, is that really needed? – Stephen Patten Dec 10 '11 at 00:51
  • Also, I'm getting the data, the grid is populating, but the calls are all empty. Ideas? – Stephen Patten Dec 10 '11 at 00:54
  • 1
    @SPATEN: I am not sure that I understand you correct, but the server should return all the data back. If you don't want to use local paging the user will have to scroll through the data. Instead of scrolling the user can use paging which work more quickly. If you do need to see all data without paging you can just set `rowNum: 10000` instead of default 20. – Oleg Dec 10 '11 at 00:57
  • Yes. The server is returning all the data in one call, but does that service call still have to include total, page and record? same thing on client, does the grid need to be configured to use options that are not relevant? – Stephen Patten Dec 10 '11 at 01:03
  • 1
    @SPATEN: If the cells are empty you has either formatted the data wrong or not used correct `jsonReader` or it was some error which you not displayed. I recommend you *always* to define `loadError` (see [here](http://stackoverflow.com/a/6969114/315935) or UPDATED part of [the answer](http://stackoverflow.com/a/5501644/315935)). You can also post the definition of the grid which you use and the JSON data returned from the server (use [Fiddler](http://www.fiddler2.com/fiddler2/) or [Firebug](http://getfirebug.com/) to catch HTTP trafic) – Oleg Dec 10 '11 at 01:04
  • 1
    @SPATEN: You don't need returns `total`, `page` and `record` if you would use correct [jsonReader](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#jsonreader_as_function). – Oleg Dec 10 '11 at 01:05
  • Can you shed any light on how to implement a single text box above the grid (e.g. Outlook Search) and have it filter data on the client side? – Stephen Patten Dec 15 '11 at 22:54
  • @SPATEN: Is [the old demo](http://www.ok-soft-gmbh.com/jqGrid/SingleSearchFilter.htm) from [the answer](http://stackoverflow.com/a/4509018/315935) what you are need? You can search in my old answers with the URL like [this](http://stackoverflow.com/search?q=user%3A315935+%5Bjqgrid%5D+searching) or [this](http://stackoverflow.com/search?q=user%3A315935+%5Bjqgrid%5D+search). – Oleg Dec 15 '11 at 23:00