3

I have a jQgrid that loads data initially via an ajax call from backend(java struts). Again, this is one time load and once loaded, the jqGrid should operate on the data available locally. Initially, datatype:json and once loadcomplete, set datatype:local.

Now is there a way to use filterToolbar for local datatype with the following options in free jqgrid;

  1. autocomplete enabled in the toolbar
  2. excel like filtering options

Jqgrid Options:

jQuery("#listTable").jqGrid({
    url:'/WebTest/MainAction.do',
    datatype: "json",
    colNames: ['Label','Value'],
    colModel: [
        {name:'label',index:'label',width: 40,search:true, stype:'text',sorttype:'int'},
        {name:'value',index:'value',width: 56,search:true, stype:'text',sorttype:'text'}
    ],
    autowidth: true,
    autoResizing: { compact: true, widthOfVisiblePartOfSortIcon: 13 },
    rowNum: 10,
    rowList: [5, 10, 20, "10000:All"],
    viewrecords: true,
    pager: true,
    toppager: true,
    rownumbers: true,
    sortname: "label",
    sortorder: "desc",
    caption: "Test 235",
    height: "200",
    search: true,
    loadonce: true,
    loadComplete: function (data) {
    },
    gridComplete: function(){ 
        jQuery("#listTable").jqGrid('setGridParam', { datatype: 'local' });
    }
}) .jqGrid("navGrid", { view: true, cloneToTop: true})
.jqGrid("filterToolbar")
.jqGrid("gridResize");
Oleg
  • 220,925
  • 34
  • 403
  • 798
Faz
  • 534
  • 1
  • 9
  • 27
  • All the features are enabled by default if I understand you correctly. The server just have to return *all data* instead of one page of data to make `loadonce: true` property work correctly. You need just call `filterToolbar` after creating the grid. All will work like with local data. You should consider to set `sorttype` property for correct local sorting and `stype` and `searchoptions` for correct filtering of data. – Oleg May 25 '15 at 19:36

1 Answers1

3

All the features are enabled by default if I understand you correctly. The server just have to return all data instead of one page of data to make loadonce: true property work correctly. You need just call filterToolbar after creating the grid. All will work like with local data. You should consider to set sorttype property for correct local sorting and stype and searchoptions for correct filtering of data.

To have "autocomplete" and "excel like filtering options" you need additionally to follow the answer which set autocomplete or stype: "select", searchoptions: { value: ...} properties based on different values of input data. You can do this inside of beforeProcessing callback. The code from the answer use this.jqGrid("getCol", columnName) which get the data from the grid. Instead of that one have access to data returned from the server inside of beforeProcessing callback. So one can scan the data to get the lists with unique values in every column and to set either autocomplete or stype: "select", searchoptions: { value: ...} properties.

UPDATED: I created JSFiddle demo which demonstrates what one can do: http://jsfiddle.net/OlegKi/vgznxru6/1/. It uses the following code (I changed just echo URL to your URL):

$("#grid").jqGrid({
    url: "/WebTest/MainAction.do",
    datatype: "json",
    colNames: ["Label", "Value"],
    colModel: [
        {name: "label", width: 70, template: "integer" },
        {name: "value", width: 200 }    
    ],
    loadonce: true,
    pager: true,
    rowNum: 10,
    rowList: [5, 10, "10000:All"],
    iconSet: "fontAwesome",
    cmTemplate: { autoResizable: true },
    shrinkToFit: false,
    autoResizing: { compact: true },
    beforeProcessing: function (data) {
        var labelMap = {}, valueMap = {}, i, item, labels = ":All", values = [],
            $self = $(this);

        for (i = 0; i < data.length; i++) {
            item = data[i];
            if (!labelMap[item.label]) {
                labelMap[item.label] = true;
                labels += ";" + item.label + ":" + item.label;
            }
            if (!valueMap[item.value]) {
                valueMap[item.value] = true;
                values.push(item.value);
            }
        }

        $self.jqGrid("setColProp", "label", {
            stype: "select",
            searchoptions: {
                value: labels,
                sopt: ["eq"]
            }
        });
        $self.jqGrid("setColProp", "value", {
            searchoptions: {
                sopt: ["cn"],
                dataInit: function (elem) {
                    $(elem).autocomplete({
                        source: values,
                        delay: 0,
                        minLength: 0,
                        select: function (event, ui) {
                            var grid;
                            $(elem).val(ui.item.value);
                            if (typeof elem.id === "string" && elem.id.substr(0, 3) === "gs_") {
                                    grid = $self[0];
                                    if ($.isFunction(grid.triggerToolbar)) {
                                        grid.triggerToolbar();
                                    }
                            } else {
                                // to refresh the filter
                                $(elem).trigger("change");
                            }
                        }
                    });
                }
            }
        });

        // one should use stringResult:true option additionally because
        // datatype: "json" at the moment, but one need use local filtreing later
        $self.jqGrid("filterToolbar", {stringResult: true });
    }
});
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks, I will give that a try.. However, I updated the jqgrid options that have used. For some reasons, the filter option isn't working. I'm sure I'm missing something obvious.. If you find time, please direct me – Faz May 25 '15 at 20:10
  • No worries, got it.. again, a very simple mistake.. rectified it.. works fine.. :) – Faz May 25 '15 at 20:13
  • @Faz: What you mean "the filter option isn't working"? You should remove unneeded `gridComplete` from your code. You wrote about `autocomplete` and "excel like filtering options" (I think it's the usage of selects in the filter toolbar). You use only `sorttype: 'int'`, but no `stype: "select", searchoptions`. You should calculate different values for the columns either on the server side and include there in the response or to do the same on the client side. You should make the choice where (in which columns) to use select and where to use autocomplete. – Oleg May 25 '15 at 20:16
  • @Faz: I recommend you to read [the answer](http://stackoverflow.com/a/19427444/315935) which shows how to use `beforeProcessing` and set the column properties by `setColProp`. You should don't forget to create the searching toolbar by call of `filterToolbar` **after** setting all the properties. – Oleg May 25 '15 at 20:20
  • Sure Oleg, I will go through that link to get further options. Ok, if you want me to remove gridComplete, then where do I set my datatype to local after the data is completely loaded.. Also, when I try the Search option (the magnifier icon), I get the below error : ReferenceError: label is not defined (String(jQuery.jgrid.getAccessor(this,'label')).toUpperCase().indexOf("55",0) > ... – Faz May 25 '15 at 20:23
  • I will try a few options, and if I still get an error I will get back to you! – Faz May 25 '15 at 20:25
  • @Faz: Setting of `datatype: 'local'` inside of `gridComplete` is wrong starting with jqGrid 3.7. jqGrid make the setting self internally *after* the first load and *after* the first call of `loadComplete`. You can test `datatype` for `"json"` inside of `loadComplete` to do some actions based on new loaded data. – Oleg May 25 '15 at 21:02
  • @Faz: I prepared the demo for you which do what you need. See **UPDATED** part of my answer. – Oleg May 25 '15 at 21:45
  • Thanks a ton Oleg, one quick question though... you mean to say just setting loadonce:true will take care of setting the datatype to local right?? – Faz May 26 '15 at 11:37
  • @Faz: Yes! It's the main goal of `loadonce:true`: setting of `datatype: "local"` after the first loading of data. Only because of that the data of the grid can be sorted and filtered locally. – Oleg May 26 '15 at 11:41
  • Got you!! Btw, I tried to set-up the search functionality in your fiddle code, but for some reasons it isn't working.. Is there anything specific am missing? Added - **$self.jqGrid("navGrid", { view: true, search:true})** – Faz May 26 '15 at 11:47
  • @Faz: `navGrid` method can be called only once. So it's better to all `.jqGrid("navGrid", { view: true, search:true})` directly after creating the grid. Moreover you should think about the place for all navigator icons which you display. You should consider to increase the width of columns or to add `width` of grid to make improve results of navigator bar. (The icons will be wrapped to new lines if there are not enough place) – Oleg May 26 '15 at 11:56
  • Yep, this is just a test so I dint concentrate much on the alignment. However, do you see any issues with the Search functionality? – Faz May 26 '15 at 12:01
  • @Faz: Sorry, but I don't understand your question. What issues you mean? Bugs? I don't know any bugs. You need just add the configurations which corresponds to searching functionality which you want to have. – Oleg May 26 '15 at 12:04
  • sorry if I was unclear.. I meant to say that I added the search related functionality in your fiddle : [link](http://jsfiddle.net/vgznxru6/8/) , but when I perform the Search functionality it doesn't return any results. Could you please check the updated fiddle code? Let me know if you need any other inputs. – Faz May 26 '15 at 17:11
  • @Faz: The reason is the following: one can fix the problem by adding `searching: { stringResult: true }` option to the grid see http://jsfiddle.net/OlegKi/vgznxru6/9/. I will fix the code of free jqGrid later so that the value will be default in your scenario. Till the time you can just add `searching: { stringResult: true }` option. – Oleg May 26 '15 at 18:40
  • Thanks Again Oleg! One last thing, can I also try excel-like filter mentioned [here](http://www.ok-soft-gmbh.com/jqGrid/SimpleLocalGridWithSearchingToolbarMultilpe.htm) with the current version of free jqGrid or is there any default behavior that you added? – Faz May 26 '15 at 18:56
  • @Faz: There are a lot of existing plugins which can be used in combination with jqGrid (or free jqGrid). One can use for example jQuery UI MultiSelect Widget written by Eric Hynds or select2 plugin, which both allowed to make "Excel-like filter". Look at [the answer](http://stackoverflow.com/a/19564404/315935) which contains references to some demos. One can uses some additional integration code for the usage of the plugins in jqGrid. You can find the corresponding examples in my old answers. Free jqGrid don't contains currently any simplifications of the integration. – Oleg May 26 '15 at 20:45
  • Sure ,I will take a look at it. Thanks! – Faz May 27 '15 at 11:17