2

here is declarations of my subgrid:

    subGrid : true,
    subgridtype: 'json',
    subGridUrl: 'manuf_subgr.php',
    subGridModel: [{    name  : ['Package','Sticker','Manufacturer'], 
                        width : [85,50,100],
                        params: ['Catalogue'] 
                    } 
    ],
    gridComplete: function() {
        var timeOut = 50;
        var rowIds = $("#schedule").getDataIDs();
        $.each(rowIds, function (index, rowId) {
            if(rowId.row_cnt != 0){
                setTimeout(function() {
                    $("#schedule").expandSubGridRow(rowId);
                }, timeOut);
                timeOut = timeOut + 200;
            }
        });
    }

what I expect to happen is this line if(rowId.row_cnt != 0) preventing opening a subgrid if there is no data returned from json... yet all grids are open regardless...

can someone help to implement stop for opening empty subgrids?

full code:

jQuery("#schedule").jqGrid({
    url:'sched.php',
    datatype: "json",
    mtype:'GET',
    colNames:['Street_Date','Label','Catalogue', 'Artist', 'Title','UKDP','UPCEAN','format'],
    colModel:[
        {name:'Street_Date',index:'Street_Date desc, ID', sorttype:"date", formatter:'date', formatoptions: {newformat:'d/m/Y'}, width:75},
        {name:'label',index:'label', width:100,align:"center"},     
        {name:'Catalogue',index:'Catalogue', width:85},
        {name:'Artist',index:'Artist', width:120},
        {name:'Title',index:'Title', width:250},
        {name:'UKDP',index:'UKDP', width:35, align:"right", formatter:"number", sorttype:"float"},
        {name:'UPCEAN',index:'UPCEAN', width:120, align:"center"},      
        {name:'format',index:'format', width:70, sortable:false}
    ],
    height: "100%",
    rowNum:20,
    rowList:[10,20,30,50,100],
    sortname: 'id',
    viewrecords: true,
    sortorder: "desc",
    jsonReader : { 
        root: "rows", 
        page: "page", 
        total: "total", 
        records: "records", 
        repeatitems: true, 
        cell: "cell", 
        id: "id",
        userdata: "userdata", 
        subgrid: {root:"rows", repeatitems: true, cell:"cell" } 
    },
    pager: '#schedule_pager',
    caption:"Release Schedule",

    grouping:true,
    groupingView : {
        groupField : ['Street_Date']
    },


    subGrid : true,
    subgridtype: 'json',
    subGridUrl: 'manuf_subgr.php',
    subGridModel: [{    name  : ['Package','Sticker','Manufacturer'], 
                        width : [85,50,100],
                        params: ['Catalogue'] 
                    } 
    ],
    gridComplete: function() {
        var timeOut = 50;
        var rowIds = $("#schedule").getDataIDs();
        $.each(rowIds, function (index, rowId) {
            if(rowId.row_cnt != 0){
                setTimeout(function() {
                    $("#schedule").expandSubGridRow(rowId);
                }, timeOut);
                timeOut = timeOut + 200;
            }
        });
    },

    onSelectRow: function (rowId) {
        $("#schedule").jqGrid ('toggleSubGridRow', rowId);
    }
});
Elen
  • 2,345
  • 3
  • 24
  • 47
  • the syntax `rowId.row_cnt` is definitively wrong because `rowId` is a string. Could you include more code? Is `row_cnt` is the name of some column of the grid? Which `datatype` has the grid? etc. – Oleg Nov 23 '12 at 16:41
  • @Oleg, a famous jqgrid specialist ;) i was hoping you will see this post. funny enough i got this peace of code from jqgrid forum from Tony post, i search the web and couldn't find anything about row_cnt method. but it doesn't break the script! - http://www.trirand.com/blog/?page_id=393/feature-request/SubGrid%20and%20records==0/&value=change%20modal&type=1&include=1&search=126&ret=all but now i realise i took completely wrong aproach anyway... still couldn't find better... i mean - there should be a way to check if json return any data on subgrid? – Elen Nov 23 '12 at 16:54
  • In the post which you referenced there exist the line `var rowdata = getDataIDs(rowid);` before the usage of `if (rowdata.row_cnt) == 0`. So the `row_cnt` is the column name. Probably the column is hidden. You should better describe which scenario *you* have. You can't just analyse any general JSON data and decide whether the data could be better be displayed with subgrid or not. On the other side you can implement expending of items *having* subgrids if you provide the corresponding information in JSON data of the main grid. See [the answer](http://stackoverflow.com/a/4538656/315935) – Oleg Nov 23 '12 at 17:08
  • There are many different options. For example one can load subgrid data *together with the data of the main grid*. [The answer](http://stackoverflow.com/a/10178440/315935) uses `datatype: local`, but the idea is the same: *one* data source (`myGridData` in the demo) hold *all* data fro the main grid and all subgrids. The problems described in [another answer](http://stackoverflow.com/a/8049324/315935) could be very important for you. `expandOnLoad: true` option which you try to implement is incorrect implemented in jqGrid. So you have to implement it in another way. – Oleg Nov 23 '12 at 17:11
  • @Oleg, i was hoping to avoid one big query to several tables in db, hence to have separate query for subgrids... re your previous post - i have read it. but the thing is I OPEN all subgrids after load complete... it will look weird to open all empty and then they all disappear. see my updated code – Elen Nov 23 '12 at 17:14
  • @Oleg even if i change to `if(rowId.id > 0)` where id is definately returned if there is json data - the above statement is always false... – Elen Nov 23 '12 at 17:31
  • First of all you need remove `sortname: 'id'` because there are no column having `name: 'id'`. You can remove options which has * default* values like [jsonReader](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data). If you do need to change some properties of `jsonReader` you need include `jsonReader` with only *the changed properties*. The main problem which you has in the implementation of your requirement I will better write in my answer. It will not solve the problem, but explains the way to the solution. – Oleg Nov 23 '12 at 17:33
  • @Oleg, thank you for the tip! today is the second day i'm working with jqgrid and feel complete newbie =)) some things are natively understandable, others not so... – Elen Nov 23 '12 at 17:36

1 Answers1

0

You write in the comment that you are newbie and it's the second day when you use it. jqGrid is relatively complex and your first example which you try to implement seems to me too complex for newbie. You try to load fill jqGrid with the data from the database and do grouping and subgrid in one grid.

What you try to implement in your demo can be solved by usage of expandOnLoad: true property of the subGridOptions option:

subGridOptions: {expandOnLoad: true}

You can see the feature on the official demo disguised under "Hierarchy" / "Expand all Rows on load". There are one important problem which I described in the answer. The problem is shortly the following: internal implementation of Ajax requests used in jqGrid prevent (skip) Ajax requests when another pending (not yet answered by the server) are working (see .grid.hDiv.loading in the places of the code here, here in the subgrid module and here, here and here). Neither expandOnLoad: true nor your current implementation can grantee that new Ajax request will be started during previous one still not responded. So you can have not all subgrids opened. You main question: "how to prevent opening of empty subgrids or how to hide it?", I find the second (and less important) question. Even if you see currently that your web site opens all subgrids it can be that access to the same site from more far place from the internet will follow opening of one or some subgrids only.

So I think that you should either change the design of your web page (change what you want to display in subgrids) or change the implementation so that you construct a queue with the list of subgrids which should be opened and you will open the next grid only after the previous one will be opened.

Alternatively you can includes the data for all subgrids in the response for the main grid. In the case you should use subGridRowExpanded callback to fill the grids as subgrid as grid. You you would use no caption and no pager option in the subgrids you will be get the same look as with the standard subgrids. Additionally you will have much more flexibility in the options. I personally prefer to subgrid as grid.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • hi , thank you for broad answer. I have tried using expandOnLoad: true with no success as you mentioned before - the query doesn't get executed because of the main pending query still in place. Hence i have 'timeOut' delay on expanding subgrinds on gridComplete and this works fine. The website is for internal use only, so i'm not bothered about 'far' queries really... you suggesting either have a queue to run on subgrids after or have it all in one request - this still will not ensure that empty subgrids won't be build. Is there a way to check if ajax returns any data for a subgrid? – Elen Nov 27 '12 at 14:47
  • @Elen: Want I mean is the following: you included in the response to the main grid (`url:'sched.php'`) the data for *all* subgrids for the rows returned. You can use `userdata` for example. After loading the main grid you can open all subgrids (if the corresponding row contain not empty grid). Because you have already all the data already *locally* (for example inside of `$("#schedule").jqGrid("getGridParam", "userData")`) you can do this practically in the same way like in [the answer](http://stackoverflow.com/a/10178440/315935) which I referenced you before. – Oleg Nov 27 '12 at 15:28
  • @Elen: See [here](http://stackoverflow.com/a/3849513/315935) for example for details how to place `userdata` inside of the server response. If you don't used `userdata` before, it's very simple. The value of `userdata` property of the server response can be **any object of any format which you like**. The only advantage of usage of `userdata` is that the additional data will be saved associated with other grid and you have easy access to the data in the same way like with other data. – Oleg Nov 27 '12 at 15:31
  • hi Oleg, i think i understand this is the best way, but i need to look into this further... i'll let you know – Elen Nov 27 '12 at 16:52