jqGrid plugin - no auto row ID? - jqgrid

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.

Related

Unable to make jqGrid to work for composite key

I still have problem defining composite key in jqGrid. Even though I set the 3 fields that are the composite key with attribute 'editable: true', the jqGrid does not behave correctly. I haven't used any 'key=true' in any of the fields as the jqGrid document mentions it that not more than one field can use this attribute. When I point to one of the rows in the List View, jqGrid high lights two rows, instead of one row. Is there any other attribute I need to use to specify more than one field for the composite key.
The usage of composite key is very easy. The property id if input items is used by default as the key (rowid). Thus you need just fill the input data with id properties, which constructed from the 3 fields and some separator, like _ for example, which is not a part of the field. If you will have some implementation problems, then you need just include an example of input data and the composite key, which you want to have.

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.
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.

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.

How to: jqGrid dependent select box display values in rows

I have implemented dependent dropdowns in jqGrid (similar to this example). The row data returned has values as well as display values. How can I show display values on grid rows? At grid initialization time I do not have list of values for dependent dropdowns, they get loaded from the server when user selects value from other dropdown. formatter: 'select' helps when I know the values of dependent dropdown but I am not sure how to use it when the values are loaded dynamically.
In case of loading the data from the server you need just set editoptions.value or formatoptions.value before formatter: "select" start to process the data. You can use beforeProcessing callback for example. It will be called at the moment which you need. You can place the information with editoptions.value in userdata part of the server response or in any other place. The data parameter (the first parameter) of beforeProcessing callback contains all the data from the server response. So you can easy get the required data and to use setColProp for example to change the options used by formatter: "select". I recommend you to read the answer for the corresponding code example.

Resources