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

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.

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.

jqGrid uses common counter in order to be a sure that there will be no duplicate values when a build in id generator is used.
To reset the value you will need to set the guid parameter to 1 - i.e
$.jgrid.guid =1;
after you unload the grid

If the solution is based on the knowing the behavior of the product, then this is a good solution.
If we try to solve the problem, without to knowing the behavior - it is a dangerous solution.
It is very easy to say - this is bad solution and it is very very dangerous to use this solution - so my question is:
What is happen with the counter if the user reload the page under some conditions?
The answer is: the counter is reset and we go to the same situations which are described in the above answer.

Related

GWT (smartgwt) grid sort selected records top

I have a grid where the user can select records via checkbox in front of every record. Now I have a requirement to sort the records based on their selection, so that all selected records should be placed top, followed by the not selected ones.
Is there any standard function to achieve this? As an alternative I thought of saving the selection state as an attribute on every record and sort based on the attribute.
The code for the column is:
gridRealmDt.setSelectionType(SelectionStyle.SIMPLE);
gridRealmDt.setSelectionAppearance(SelectionAppearance.CHECKBOX);
I try to describe the code I use as the affected code is deeply nested in our own framework classes.
gridRealmDt is a subclass of smartgwt ListGrid. In my Dialog a create an instance of the grid which creates an instance of a database bound datasource. When the dialog is loaded the records are fetched from the database and after that a registered an dataArrivedHandler where I select the records which match records from another table.
I tried to place the selection attribute in an extra field and use that for sortig before my other sort criteria, but this does not work. Here is the code for the field I am using.
ListGridField txtSelected = new ListGridField(SELECTED, "");
txtSelected.setHidden(true);
txtSelected.setSortByDisplayField(true);
txtSelected.setCanSortClientOnly(true);
When I do not set the canSortClientOnly property the order by is sent to my database resulting in an error, as the table does not contain that field, so I set the property. Now I get following error
Removing field from the sort Specifier array because it specifies canSort Client Only: true and all data is not yet client-side.
I also tried to use a sortNormilizer on the Name field which is my main sort criteria, but the normalizer is called before the selection value is set to the record.
record.setAttribute(CARealmDS.SELECTED,selected ? "a" : "b");
I also cannnot find a way to call the normalizer when selection changes.
Currently we are using Smart GWT Version 6.0p.
I don't think there is any standard function. Just use grid store update. Place checked items first.

Sorting on hidden data

Is it possible to have data in a Handsontable sorted by a field which is not displayed? I have a grid of data which I would like to display that contains a column called "sortOrder", but I don't want to display this.
The sorting needs to be done client side because events are coming in over web sockets and need to be reflected in the table.
If you're not showing the column then I assume you're not expecting the user to be able to manually sort by this hidden column. Therefore, why don't you simply sort your data array with native JS? At any point during execution you could have a function which sorts by this hidden column and then just don't render this in your Handson definition.
So yes, the answer is it is possible. The not showing of a column is as simple as defining the columns option and not including a column for this hidden value.

Remove all the rows of handson table

I am new to handsontable.
My handsontable rows are readonly.
I wanted to remove all rows of handsontable on a button click.
Please help me.
Do update with empty dataset:
handsontableInstance.updateSettings({
data : []
});
This removes all rows (and leave header if there is some).
There are many ways to "remove" all rows. For example, one, and the easiest, would be to empty your data array. So say that you initialized your HOT instance with the data field as array dataArray. Your button would only need do:
$("#buttonId").click(function() { dataArray = [];})
That would be the easiest way but of course you'd be bypassing HOT. This means that if your application gets more complex and you rely on handlers such as afterRemoveRow, then this method will bypass them. In this latter case, you'd want to use the hot.alter() method as follows:
hot.alter('remove_row', 0); // would remove the row at index 0;
With this I am assuming you know how to use a for loop that could iterate through all rows and remove them, one by one. An expensive operation but it would ensure all the proper handlers get called.
tableInstance.clear()
Clears the data from the table (the table settings remain intact).
refer to the
doc

jqGrid plugin - no auto row ID?

I use jqGrid javascript plugin.
I use datatype:json, and everything works fine, beside the fact I don't want jqGrid to give IDs to my rows.
I don't want to set IDs at all, and if I don't set them, jqGrid automatically gives IDs to rows : 1, 2, 3.. (auto-increment numbers). This is a problem since I create a few grids , and then I have the same ID multiple times.
Though I haven't had any problems with it so far, I don't want my document to be invalid. Is there any way to disable jqGrid auto row ID ?
Every row of data of the grid must have id attribure. It will be used in many callbacks of jqGrid. So I recommend you to assign some value. If you get the data from the server the data will be typically get from some table of the database which do have unique id. In the case I recommend you to use the value as the id. It will simplify the implementation of editing of the data it you will decide to implement the feature in the future.
To assign unique id value on the client side I can suggest you two approaches: 1) usage of idPrefix 2) usage of beforeProcessing to assign unique id attribute to every item returned from the server on the client side.
The first approach is very easy. The rowid will be constructed from the value of idPrefix option of the grid and the id from input data or 1,2,3,... values generated by jqGrid if no ids are found in the input data. For example you can use idPrefix: "a" for the first grid and idPrefix: "b" for the second grid. In the case ids of the first grid will be "a1", "a2", "a3", ... and ids of the second grid will be "b1", "b2", "b3", ...
In case of usage the second approach you need to implement beforeProcessing callback which will be called by jqGrid after response from the server are get, but before the data will be processed by jqGrid. So you will be able to pre-process the data. For example you can make a loop over the items of data and assign id attribute to every item with $.jgrid.randId() for example. The method $.jgrid.randId() will generate unique id value. So you will have no id dupplicates more.

how can I get the count of matches for filters of jqgrid

I have pulldown select box for filtering the jqgrid (not using the builtin navbar, and data is local). I'd like to grey out the ones that have no matches in the grid.
Is there a way to perform the query to just get the count without updating the ui rows so I can initialize/update the pulldown to enable only those with matches (or even better to supply the number of matches in the menu)
EDIT:
To clarify I want to disable/greyout my filter menu items not the table row items
Internally jqGrid uses $.jgrid.from method to apply the filter to the data. What you want to implement it's probably not just getting the counts because you wrote:
I'd like to grey out the ones that have no matches in the grid.
Nevertheless all want you want is possible to implement. You will have to write some JavaScript code which uses $.jgrid.from($("#list")[0].p.data) for initialization. Then you have to construct the query using methods like contains, lessOrEquals, andNot, orNot and so on. Then you should apply the query with respect of var queryResults = query.select();. If you examine ids in the queryResults you can make gray all items which are not in the set.
I recommend you to read and to debug the addLocalData method of jqGrid and which contain all what you need. Moreover I recommend you to set breakpoint on the line and examine match and results variables. It this is not simple of cause, but if you need some individual solution you have to invest time in it.
UPDATED: I though more about the described problem. My recent answer with the demo demonstrate how you can solve your problem.

Resources