2

The grid defines the field as:

colModel: [
    {name:'Catalogue', index:'catalogue.Catalogue', width:100, align:"left",
        editable:true, edittype:'select',
        editoptions: {dataUrl:'xtras/selectOptions.php?optionFunction=CatalogueSelect'}
    },

The build grid with inline edit html code shows:

<select class="editable" id="23368_Catalogue" name="Catalogue" size="1">
    <option value="0">Select</option>
    <option value="2064">KSCOPE370</option>
    <option value="2063">KSCOPE369</option>
...
</select>

I'm expecting the grid to pass to the server values of the select such as [Catalogue] => 2064, yet the grid sends to server:

[Catalogue] => KSCOPE370

If I add to column definition formatter:'select', - it sends the correct value of 2064, however if I attempt the inline edit, then cancel it - the visible value of the Catalogue field disapears: Was:

Catalogue    ISRC   ...
KSCOPE370   GBCQV0200197   .....

After cancel editing, becomes:

Catalogue    ISRC   ...
            GBCQV0200197   .....

What is the correct way to handle this?

Elen
  • 2,345
  • 3
  • 24
  • 47

1 Answers1

0

It's important to understand that the format of data in the grid have to corresponds the format of editing. The input data, which you use for the column Catalogue are the strings like "KSCOPE370" and "KSCOPE369" instead of ids 2064 or 2063. It means that jqGrid hold "KSCOPE370", but it means that the new modified data have to be in the save format: strings and not ids.

Your current code would fail if it would try try to save the value 2064 in the grid after editing. It saves KSCOPE370 locally and send Catalogue=KSCOPE370 to the server because you don't specified formatter: "select" to the column Catalogue. If one debug the code one can see that the new value will be correctly saved in the grid, but one have the problem on the server side. It seems that your server code expect ids instead of the text and can't process the data Catalogue=KSCOPE370. You use .trigger("reloadGrid", [{current: true}]) in aftersavefunc of inline editing to reload the grid. The empty text will be displayed after reloading the grid.

jqGrid provide two main options: the usage of texts and the usage of ids. Typically one have database table which holds the lookup data. I personally create lookup tables always by usage IDENTITY (or AUTO_INCREMENT) column for id and the column with the text. Additionally I create always UNIQUE CONSTRAINT on the text. It allows to be sure that one can not only get the text by id, but id by the text if it would be required. Because of that one can easy use the texts during filling and editing the grid. The id value can be resoled by name, whenever it's needed, on the server side.

If one really need to use ids during instead of text then you should

  1. fill the grid with ids in the Catalogue instead of the texts.
  2. one should use formatter: "select" to display the texts instead of the ids in the grid.
  3. one can't use editoptions.dataUrl to provide the map between ids and the texts because dataUrl request will be processed using asynchronous Ajax request, but one need to have the map already for filling the grid. The formatter: "select" have to have the values. Thus one have to use editoptions.value instead of editoptions.dataUrl. The solution of the problem is described in the answer or this one. It suggests to return the editoptions.value value together with the main data of the grid and to use beforeProcessing to set editoptions.value before the value will be used by formatter: "select". Such approach reduces additionally the HTTP traffic because one don't need to make request to dataUrl every time on editing of row.

UPDATED: I posted the changes to GitHub to send value of <option> to the server even if no formatter: "select" is defined. It will still used the text for local editing. Such changes should improve the compatibility of free jqGrid to old versions of jqGrid.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I meet all the suggested criteria in my database: Use of unique `ID` with autoincrement, `Catalogue` field is unique too. So in other words interpreting your post - the grid only sends "visible" value of the field to the server. I can either use `editoptions.value` to send the select values to the grid while it is being build, where grid is going to store it. OR I have to lookup the `ID` of corresponding `Catalogue` at the server side. Correct? – Elen Dec 17 '15 at 13:03
  • @Elen: I mean that if you use no `formatter: "select"` and fill the grid with the data having `"KSCOPE370"` for the first column then `dataUrl` should generate select with the same `value` as the text: `` instead of `` The server code, which corresponds `xtras/Records.php` should expect the text and not the id. During saving in the table you should just use `SELECT` in lookup table which convert the text `KSCOPE370` to the id `2064` which will be saved finally. – Oleg Dec 17 '15 at 14:19
  • @Elen: The second way, which I described, is the way of filling the grid with ids like `2064` and editing the ids too. – Oleg Dec 17 '15 at 14:21
  • Understood. With all the huge respect I have for you and your enormous work... why would you suppress the default `select` behaviour, where in normal html form `select` will pass the value of the selected option, not it's name? – Elen Dec 17 '15 at 14:26
  • @Elen: The old code of jqGrid is dirty. I rewrote of many parts and continue the work. For example it was many places in jqGrid (cell editing and inline editing), where one get texts or values from editing fields. I separated the method [getEditedValue](https://github.com/free-jqgrid/jqGrid/blob/v4.11.1/js/grid.base.js#L1231-L1300) which do this. It has `useTextInSelects` parameter and I set `selectMethod = useTextInSelects ? "text" : "val"` at the beginning of the code and use [the line](https://github.com/free-jqgrid/jqGrid/blob/v4.11.1/js/grid.base.js#L1263) to get the value or the text. – Oleg Dec 17 '15 at 14:32
  • right! so how can I force `useTextInSelects` to be `false` for a specific column? – Elen Dec 17 '15 at 14:35
  • @Elen: The parameter `useTextInSelects` will be set to `true` or `false` based of the existence of `formater` (typically `formatter: "select"`) in the corresponding column having `edittype:'select'`. What you write will mean the usage of `$.val()` method always instead of usage either `$.text()` or `$.val()`. Probably it would be correct too. One should just take in consideration all different use cases, which is not so simple. – Oleg Dec 17 '15 at 14:38
  • If I may suggest... As non-experienced user I would expect the behaviour of `select` to be default as for simple HTML form. And in cases when I want to retrieve the text of the `select` - I should specify this explicitly... – Elen Dec 17 '15 at 14:41
  • @Elen: It would mean in your case that jqGrid would save `2064` instead of `KSCOPE370` in the cell after editing and one would be display correct value only after reloading the grid. – Oleg Dec 17 '15 at 14:44
  • @Elen: The old code in jqGrid 4.6 from inline editing did [the following](https://github.com/free-jqgrid/jqGrid/blob/v4.6.0/js/grid.inlinedit.js#L190-L204) then it used in [the line](https://github.com/free-jqgrid/jqGrid/blob/v4.6.0/js/grid.inlinedit.js#L283) and in [the lines](https://github.com/free-jqgrid/jqGrid/blob/v4.6.0/js/grid.inlinedit.js#L328-L329). I have to look in the old code very carefully to understand what exactly will be send to the server and what should be used locally. The code just contains special hack for very special case: usage of ` – Oleg Dec 17 '15 at 14:46
  • would be great! thank you! UPD: or both text and value can be sent to the server for user to choose – Elen Dec 17 '15 at 14:48
  • @Elen: You are welcome! One can't send both the text and the value to the server because the format of the value is `colName=newValue`. I will look in the code one more and probably will implement the sending to the server the value and the usage of text locally if no formatter are specified. I will inform you about it if I'll do the changes. Thank you for the discussion about the subject. – Oleg Dec 17 '15 at 14:57
  • 1
    @Elen: I posted new changes to free jqGrid repository on GitHub. Now your demo works without any changes on the backend. See **UPDATED** part of my answer. – Oleg Dec 17 '15 at 23:45
  • wouldn't it be better to write the code rather than having paragraphs? – reaz Apr 11 '21 at 09:24