Search code examples
jqgridrowincrementidentifier

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 ?


Solution

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