0

I have a jqGrid and want to apply user's sort values after it loads. These values are saved and retrieved in jquery cookies. I am storing data in cookies because user will go to another url and come back which initiates page load and they want to be in same spot they left.

I have a loadPreferences function being called within loadComplete. See code snippet below(I left several jqrid properties out to keep posting short).

        // Set up the jquery grid
        $("#jqGridTable").jqGrid(
            {
                // Ajax related configurations
                url: jqDataUrl,
                datatype: "json",
                mtype: "GET",
                autowidth: true,

                // Configure the columns
                colModel: columnModels,

                // Grid total width and height
                height: "100%",

                // customize jqgrid post init
                gridComplete: function()
                {
                    CRef.updateJqGridPagerIcons("jqGridTable");
                },

                // Default sorting
                sortname: "LastName",
                sortorder: "asc",
                sorttype: "text",
                sortable: true,

                jsonReader:
                {
                    root: "rows",
                    page: "page",
                    total: "total",
                    records: "records",
                    repeatitems: false,
                    userdata: "userdata"
                },

                loadComplete: function (data)
                {

                    if (boolPreferencesLoaded == false)//page level variable
                    {
                        boolPreferencesLoaded = loadPreferences(this);
                        $("#jqGridTable").trigger("reloadGrid");
                    }

                    isGridSorting(this, false);
                 }
             ...the rest of grid properties...

and in the function call(which is located in another js file), I have...

function loadPreferences(gridId)
{
    if (typeof $.cookie("sortCol") !== "undefined")
    {
        $(gridId).jqGrid("sortGrid", $.cookie("sortCol"), true, $.cookie("sortOrd"));
    }

    return true;
}

function isGridSorting(gridId, sorting)
{
    $.cookie("sortCol", $(gridId).jqGrid('getGridParam', 'sortname'));
    $.cookie("sortOrd", $(gridId).jqGrid('getGridParam', 'sortorder'));

    $(gridId).jqGrid('setGridParam', { postData: { isSorting: sorting } });
}

My problem is when I go to apply them on page load(with jqGrid specified above), it does not exactly work. The icon moves to correct column and the arrow is pointing in correct direction, but the data in column does not sort.

I know there are a lot of posts dealing with similar issues, but not able to get solutions to work. What I am trying to do seems to easy, but it's driving me nuts. If you respond, please make comment easy to understand. Thank you.

MaxAx
  • 101
  • 2
  • 12
  • Please include in every question about jqGrid **which version of jqGrid and from which fork of jqGrid you use**. From you code it's unclear why you call `loadPreferences` and `isGridSorting` inside of `loadComplete` instead of getting the information **before** creating the grid and usage it as the parameter of grid already during creating the grid. Moreover you should never use `.trigger("reloadGrid")` inside of `loadComplete` to be sure that you have not infinite recursion. You use no `loadonce: true` then **the server makes sorting** and you first load wrong sorted data and then reload it. – Oleg Feb 08 '16 at 00:19
  • @Oleg I am using jqGrid 5.0.2. I like what you wrote 'getting the information before creating the grid and usage it as the parameter of grid already during creating the grid'. That sounds exactly what I want. Do you have a post that shows this? – MaxAx Feb 08 '16 at 00:27
  • For example [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/ColumnChooserAndLocalStorage2_singleSelect.htm) created for [the answer](http://stackoverflow.com/a/31663268/315935), but I develop [free jqGrid](https://github.com/free-jqgrid/jqGrid), which is **alternative fork** to [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) which you use. The current released version of free jqGrid is 4.12.1. I have no examples for Guriddo jqGrid JS 5.0.2. – Oleg Feb 08 '16 at 00:53
  • @Oleg I used your example as a guide and was able to implement what I wanted. Thanks. Now I want to retain the filter that a user creates, so I think I can do the samething. I saw another post that might work. – MaxAx Feb 08 '16 at 02:28
  • [The demo](http://www.ok-soft-gmbh.com/jqGrid/OK/ColumnChooserAndLocalStorage2_singleSelect.htm) from my previous comment saves the filter, the sort order. the current page number, the column order (columns can be reordered using drag & drop of column headers or by columnChooser), selection and so on. – Oleg Feb 08 '16 at 09:35

0 Answers0