Search code examples
jqgrid

Why is free-jqgrid not resetting row ids after GridUnload?


The Problem

I have a very complex, rather outdated jqgrid implementation that I'm upgrading to the latest free-jqgrid and we're having an issue in that after clearing the grid with unload, when we re-initialize the grid, the row ids are higher than they should be to start.

They seem to still be incrementing and not starting again. Are there some global counters I need to clear out to have the grid completely clear itself before we re-render it?

Grid Clear

        if ( this.timesheet_grid ) {
            this.timesheet_grid.grid.jqGrid( 'GridUnload' );
            this.timesheet_grid.grid = null;
            this.timesheet_grid = null;

Rows after GridUnload has been run

Shows an id like jqg56

Stuff I've Tried Since Posting

  • GridDestroy
  • clearGridBeforeUnload

SOLUTION

As @Oleg has suggested i simply needed to set the id attribute manually when processing the grid data.


Solution

  • I see that your incorrectly understand the meaning of rowids and the requirements for input data. I'm wonder especially about the suggestion of Tony to reset $.jgrid.guid. I find the suggestion very bad because it looks like a solution, but it can produce more seriously problems as the problems, which are solved!

    First of all, it's important to understand what is rowid. Rowid is the values of id attribute of the grid rows: the id values of <tr> elements of the grid's body (see here). The rowid are used as a parameter of almost all callbacks and events of jqGrid. Because of that jqGrid must assign id attribute to every row and the input data of the grid have to contain rowid information! The existence of rowids is critical for working jqGrid. Only because of that jqGrid generates unique rowids in case of wrong input data. Alternative for generation of ids would be preventing creating the grid at all.

    jqGrid tolerates the error with specifying the rowids in input data only for some simple grids, where one need just display some simple data and one never need to use rowids. If you use ids then you should fix the problem only in one way: you have to fix your input data. You can for example include id property with unique value in every element of input data. You should take in consideration that no id duplicates exist not only in the grid but on the whole HTML page. It's strictly recommended to use idPrefix option with unique value for every grid always if your HTML page have more as one grids (inclusive grid with subgrids).

    Resetting of $.jgrid.guid is very dangerous and it can break functionality of jqGrid because of creating elements with duplicate id values. The property $.jgrid.guid is global. It's common over all grids and subgrids on the page. Simple subgrids scenario will produces id duplicates after you manually reset $.jgrid.guid. It will be used inside of $.jgrid.randId() method used in many places of jqGrid code. For example, the method addRow used by inlineNav uses the method.

    I repeat my suggestion more clear once more. If you uses rowids or if the values of rowids are important for you in some way then you have to include id property to every item of input data. If some other property has already an unique value then you can add key: true in the corresponding column of colModel (one can use key: true in only one column). Alternatively, you can add prmNames: { id: "myId" } if myId property of input data contains unique rowid instead of default id property.