0

When having multiple grids on the same page, where each of the grids is having the same name for the date column (eg 'start_date'), the datepicker choose only the first one.

I know this is the nature of DOM elements, and I was wondering if there is a way to overcome this without changing the colModel.name, as this will also force me to parse the JSON data coming from the server (I don't have any control of the data returned from the server).

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Tal
  • 391
  • 2
  • 11
  • What exactly you do? You wrote about datepicker, but you don't explain whether you have problems with searching (toolbar searching, searching dialog etc.) or editing (form editing, include editing and so on). You need include *more details* which describes what you do. You should include `colModel` and to write **which version of jqGrid you use**. The example of JSON data (2 rows of data will be enough) loaded from the server could be also helpful. – Oleg Mar 14 '15 at 12:08
  • Thanks, im using 4.7.1. This is for the filterbar search. . So I have 2 grids, one above the other on the same page. In each grid there is a colModel with name: start_date, index: start_date and everything actually works great. The problem is with the datepick - it only applies the date n the first grid, even uf the function is called by trying to enter a date on the second grid, this I presume is because both if the grids are using the same exact name for the date column and the datepick function gets only "element" as a reference. . Any way to overcome this? – Tal Mar 14 '15 at 12:16
  • Please **append the text of your question** by include all information which is important for understanding of your problem. There are much better formatting possibilities of the code in the text of the question. – Oleg Mar 14 '15 at 12:42

1 Answers1

1

If I correctly understand your problem then the most easy way to fix the problem would be to use different column names in multiple grids. If you load the data from the server you can use jsonmap property in colModel. For example

{ name: "grid1_column1", jsonmap: "propName", ... }

and

{ name: "grid2_column1", jsonmap: "propName", ... }

As the result you will have no conflicts in any searching of editing fields. The exact format of jsonmap can depend on the format of input data (the format of the server response) which you use.

I would recommend you to use additionally idPrefix with different values in multiple grids. For example idPrefix: "grid1_ and idPrefix: "grid2_. It will prevent conflicts in case of usage native id values for rows of both grids.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Yes, that will do the trick of course, but than I also need ti make sure the server returns such names in the json response (otherwise how jqGrid knows to map between the model and the data). Is there a way I can keep the server response untouched? – Tal Mar 14 '15 at 13:04
  • I thought idPrefix was an example. I now see there is such a parameter on the grid itself. I wasn't aware of it. I will try it. – Tal Mar 14 '15 at 13:15
  • @Tal: I asked you to append the text of your question with an example of JSON response of the server. If you use named properties then either `name` or `jsonmap` should exactly corresponds the properties of the server response. So I don't understand what you mean with your first comment. If you use array of strings in the server response that you are absolutely free in choosing of `name` property in the `colModel`. So you can just choose different names in two grids. – Oleg Mar 14 '15 at 14:03
  • Thanks, there is an example I placed at http://jsfiddle.net/tyanai/ngm5rhgp/ . It's local, but illustrate how my json server response looks like. . I was not aware for jsonmap as well. – Tal Mar 14 '15 at 14:26
  • @Tal: I see that you use items with named properties in the server response, so you can successfully use `jsonmap`. I would you strictly recommend to use column templates to reduce the size of `colModel`. the current `colModel` is not only difficult to read, but also difficult to maintain or to debug. For example the `colModel` contains `id` column which are missing in the server response. As the result jqGrid uses 1,2,3... as the values of `id` attribute of rows (`` elements). So you can have id duplicates if you use more as one grids of one HTML page. `idPrefix` can help here too. – Oleg Mar 14 '15 at 15:15
  • @Tal: You can read more about column templates [here](http://stackoverflow.com/a/6047856/315935) and [here](https://github.com/tonytomov/jqGrid/pull/631). I recommend you to take a look in [free jqGrid](https://github.com/free-jqgrid/jqGrid) - it's my fork of jqGrid, which I started after Tony changes licence agreements. See some demos at the end of readme and see more information in [wiki](https://github.com/free-jqgrid/jqGrid/wiki). – Oleg Mar 14 '15 at 15:23
  • Thanks a lot! I wasn't aware for the id issue. Actually I'm building the colModel dynamically by getting the json schema of the model from my server. This is because users can add custom fields to our servers, so de facto, it's all dynamic. I looked at 4.8.0 and was happy to see you added the ability to add custom search (I was looking for that as well), and the new icons. – Tal Mar 14 '15 at 16:12
  • @Tal: You are welcome! New possibilities of column template could be very good your in your case. You can defined `$.extend($.jgrid, {cmTemplate: {t1: {hidden:true,searchoptions:{sopt:["cn","eq","ne"]},editoptions:{sopt:["cn","eq","ne"]}},intTpl: {hidden:true,searchoptions:{sopt:["eq","ne","lt","le","gt","ge"]},editoptions:{sopt:["eq","ne","lt","le","gt","ge"]},searchrules:{"integer":true}}, ... });` nad then use `"template": "t1"` or `"template": "intTpl"` in `colModel`. You can remove `width:150`, all `index` and use the option `cmTemplate: { editable: true }` to make all columns editable. – Oleg Mar 14 '15 at 16:17
  • Absolutely. I got it. So I define a template to each type (like Date, Number or String) and then can only refer to the template based on the type, instead of keep repeating the same rules for each field from scratch, – Tal Mar 14 '15 at 17:01