1

I have a grid that has a datepicker with a button and a checkbox to reload the grid when the date changes. The datepicker is initialized to today's. This works fine. However I can't figure how to override the default url in the grid. Whenever the grid which is a tree is loaded it reads the default from the url configuration. Only when I click the button then it reloads the treee with the selected date. Even worst when I click in the icons in the tree to expand the child elements it overrides the date and all the loaded data with the value of the default URL. Can somebody tell me how to set the url properly? Also I don't understand why clicking in the elements reloads the tree since I have property loadonce=true.

thanks

    $(document).ready(function(){
    $("#list").jqGrid({
            url : "/reconcile?unMatchedOnly=true",
            datatype : "json",
            mtype : 'GET',
            colModel : [
            {name : "data.key.busnDate", label : "Business Date", hidden:false, sorttype:"date", width : 100 },
            {name : "data.product", label : "Product", sorttype:"string", width : 50, editable : false},
            {name : "data.quantityBought", label : "Quantity Bought", sorttype:"int", width : 100, editable : false},
            {name : "matches", label : "match", "edittype":"checkbox","formatter":"checkbox", width : 25, editable : false}
            ],
            cmTemplate: { width: 70 },
            treeGrid: true,
            pager : '#pager',
            treeGridModel: "adjacency",
            ExpandColumn: "data.key.busnDate",
            rowNum : 25,
            height: 'auto',
            rowList : [ 25,50,100,200 ],
            loadonce:true,
    });
    jQuery("#list").jqGrid('navGrid', '#pager', {
            edit : false,
            add : false,
            del : false,
            view : true,
            search : true
    });
      $('#datePick').datepicker({
        onSelect: function (dateText, inst) {
            var e = $("#list").data("events");
            if (typeof (e) !== "undefined" && typeof (e.reloadGrid) !== "undefined") {
                $("#list").trigger("reloadGrid");
            }
        }
    }
    );
    $("#datePick").datepicker('setDate', new Date());
    jQuery("#list").setGridParam({url:'/reconcile?datePick=' + $("#datePick").val() + '&unMatchedOnly=' + $("#unMatchedOnly").val(),page:1});
    var url = '/reconcile?datePick=' + $("#datePick").val() + '&unMatchedOnly=' + $("#unMatchedOnly").val();
    $("#list").jqGrid('setGridParam', { url: url });

});

$('#showSelected').on('click', function () {
    var url = '/reconcile?datePick=' + $("#datePick").val() + '&unMatchedOnly=' + $("#unMatchedOnly").val();
    $("#list").jqGrid('setGridParam', { url: url });
    $("#list").trigger("reloadGrid");
});
Fabio
  • 555
  • 3
  • 9
  • 24
  • It's not full clear what you want to implement. The initial filling of the TreeGrid is especially unclear. You create the grid with `url : "/reconcile?unMatchedOnly=true"` making the corresponding request to the server and **immediately** try to change the `url` to another value (see `setGridParam` with `url` **twice** below). Are `#datePick`, `#unMatchedOnly` and `#showSelected` somewhere **outside** the grid? **Do you want that all requests to the server contains the values `$("#datePick").val()` and `$("#unMatchedOnly").val()`?** If it's so than your code could be much easier. – Oleg Jul 10 '17 at 15:36
  • Do you load *all the data* at once or you load children of some nodes on expanding the node (it's default behavior). `reloadGrid` will be used by TreeGrid for loading of **child nodes**. What you want to do inside of `$('#showSelected').on('click', ....)`? Do you want to reload *whole TreeGrid* or you want to force loading of children of some node? Which version of jqGrid you use and from which fork of jqGrid (free jqGrid, commercial Guriddo jqGrid JS or an old jqGrid in version <=4.7)? – Oleg Jul 10 '17 at 15:40
  • @Oleg yes I want any request to have the datePickand unMatchedOnly values always. I used "loadonce:true" because I send all the data parent and child. I don't want the grid to go back to the server unless the date or checkbox are changed. Basically I want to replace the url with a similar called to the $('#showSelected').on('click',...) but I can't figure how to tell the grid to use that function for all data loading. – Fabio Jul 10 '17 at 17:23
  • @Oleg the version I am using is jqGrid 4.15 with date Date: 2017-07-09 – Fabio Jul 10 '17 at 17:52
  • I could not reproduce the problem, which you describe. You can try [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/LocalAdjacencyTree-autoresized1__.htm) and examine the Network traffic, which you can see with respect of Developer Tools of Chrome or IE. Every reloading using "Reload" button in the toppager increase the value of `myparam` parameter. Click on `"Reload"` button under the grid reset the parameter and changes URL additionally. I see no problems with `trigger("reloadGrid")` – Oleg Jul 10 '17 at 19:10

1 Answers1

0

I can't reproduce the problem, which you reports. If you want to load the data of TreeGrid at once then you don't need to use loadonce: true option. Instead of that the items of data should contains the property "loaded":true.

If you need to send some additional parameter to the server then you should use postData parameter with properties defined as functions (see the old answer). You can just use

url: "/reconcile",
postData: {
    datePick: function () { return $("#datePick").val(); },
    unMatchedOnly: function () { return $("#unMatchedOnly").val(); }
}

As the result the current data from #datePick and #unMatchedOnly will be sent to the server on every loading of the grid. To reload the TreeGrid you will need just use $("#list").trigger("reloadGrid");.

If you do loads the data of the grid on demand then expanding of nodes adds nodeid, n_level and parentid parameters to postData and the value of treeANode parameter will be changed from -1 to the rowid of expanding node. To reset the parameters before reloading you will need to reset treeANode parameter to -1.

I don't know the format of the data, which returns url: "/reconcile", but I suppose that you can use the following code

$(document).ready(function(){
    var $grid = $("#list"),
        fullReloadOfTreeGrid = function () {
            var p = $grid.jqGrid("getGridParam"); // get reference to parameters
            p.treeANode = -1; // be sure that we reload full grid
            $grid.trigger("reloadGrid");
        };

    $('#datePick').datepicker({
        onSelect: function (dateText, inst) {
            fullReloadOfTreeGrid();
        }
    });
    $("#datePick").datepicker('setDate', new Date());

    $grid.jqGrid({
        url: "/reconcile?unMatchedOnly=true",
        datatype: "json",
        postData: {
            datePick: function () { return $("#datePick").val(); },
            unMatchedOnly: function () { return $("#unMatchedOnly").val(); }
        },
        colModel: [
            {name: "data.key.busnDate", label: "Business Date",
              sorttype:"date", width : 100 },
            {name: "data.product", label: "Product", width: 50},
            {name: "data.quantityBought", label: "Quantity Bought",
              sorttype:"int", width: 100},
            {name: "matches", label: "match",
             "template: "booleanCheckbox", width : 25}
        ],
        cmTemplate: { width: 70 },
        treeGrid: true,
        pager: true,
        treeGridModel: "adjacency",
        ExpandColumn: "data.key.busnDate"
    }).jqGrid('navGrid', {
        edit : false,
        add : false,
        del : false,
        view : true
    });

    $('#showSelected').on('click', function () {
        fullReloadOfTreeGrid();
    });
});

As I wrote before, resetting of treeANode to -1 will be not required if you really correctly loads all the nodes at once (with loaded:true property in all nodes).

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I basically copy your code and it all start working fine now. To my data I added a column loaded with a value of true and now it does not go back to the server. Thanks – Fabio Jul 12 '17 at 13:24