3

I have a field/column in my jqGrid which is getting its value from a FlexBox (http://www.fairwaytech.com/flexbox) control. I use a form for editing/adding data to the grid locally and then I submit the complete data as a whole to the server. I have implemented the FlexBox control in my form as a custom control, complete with functions for control creation, getting values, etc. (editoptions:{ custom_element:myCustomElem, custom_value:myCustomValue, ...)

So, I have a code/value pair (much like in a normal HTML select), and in the grid I want to show the value, but I want to send the code to the server. When I load the data from the server, I get code/value pairs.

So I tried to write a custom formatter/unformatter, only to find out that the formatter seems to irreversibly modify the original data! So, once I return the value from the formatter (i.e. when displaying the data in the grid cell), the code is lost! And thus, when the unformatter is called (i.e. when sending the data to the server) the code is no longer there!

The grid is set with the following options:

cellsubmit: 'clientArray', datatype: 'clientSide', editurl: '/dummy'

On my navGrid options for add/edit I have a beforeShowForm function which constructs the FlexBox control. The FlexBox control has 2 fields, one normal input and one hidden. The hidden holds the code and the normal holds/shows the value.

The myCustomValue function is like this:

function myCustomValue (elem, action, val) {
      var value = val, code = val;
      if(action == 'get') {
         code = $('input[id="' + $(elem).attr('id') + '_div_hidden"]').val();
         value = $('input[id="' + $(elem).attr('id') + '_div_input"]').val();
      }
      else {
         $('input[id="' + $(elem).attr('id') + '_div_input"]').val(value);
      }
      return (code == value ? value : code + '||' + value);
}

So basically it's getting the code and the value and returning them as a pair separated by '||' (just a custom delimiter).

My custom formatter is like this:

function myFormatter(cellvalue, options, rowdata, action) {
   if(cellvalue == '')
      return cellvalue;
   var codeValuePair = cellvalue.split('||');
   if(codeValuePair && codeValuePair.length > 1)
      // I use a hidden span to store the code because otherwise I lose the code!!!
      // This is where the problem starts! If I just return codeValuePair[1] (description)
      // I lose the code forever!
      return '<span class="md-flexbox-code" style="display:none;">' + codeValuePair[0] + '</span>' + codeValuePair[1];
   else
      return cellvalue;
}

and the unformatter checks to see if the span with the hidden code is there and if it is, it returns that.

Am I missing something? To me the formatter is supposed to be just a way of showing the data, not modifying it!

AsGoodAsItGets
  • 2,886
  • 34
  • 49

1 Answers1

0

I am not use FlexBox personally. At the first look to use it in the jqGrid you need just us

editoptions: {
    dataInit : function (elem) {
        $(elem).flexbox(/*flexbox parameters which you need*/);
    }
}

in the corresponding column definition. But probably I miss something.

Which method you use to get local data from the grid? Do you use local data paging? Probably you have close problems as in the question?

The formatter are really only to show the values as a control inside the grid and unformatter is to read the data from the cell. For modifying the data in your case the correct implementation of custom_value important. The value returned by your myCustomValue will be saved in the grid.

It you continue to have the problem you should append your question with more code.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • The grid is set with the following options: – AsGoodAsItGets Jan 28 '11 at 12:36
  • @AsGoodAsItGets: Which options? Could you better modify the text of your question and append the additional information. – Oleg Jan 28 '11 at 12:40
  • The grid is set with the following options: cellsubmit: 'clientArray', datatype: 'clientSide', editurl: '/dummy' On my navGrid options for add/edit I have a beforeShowForm function which constructs the FlexBox control. The FlexBox control has 2 fields, one normal input and one hidden. The hidden holds the code and the normal holds/shows the value. I think your editoptions.dataInit suggestion is not relevant here, because I'm not doing inline editing. There is no separation between the data and the presentation in jqGrid (e.g. if you've ever worked with ExtJS, there is no "data store") – AsGoodAsItGets Jan 28 '11 at 12:44
  • Ok, just modified the original question. – AsGoodAsItGets Jan 28 '11 at 12:53
  • @AsGoodAsItGets: The option `dataInit` is **common** for cell, inline and form editing. If you use jqGrid with `datatype:'clientSide'` (or which is synonym `datatype:'local'`) there are internal object `data` accessible using `$("#list").jqGrid('getGridParam', 'data')`. It hold the data of the local grid. If you use local paging, the grid shows only current page and the object `data` hold **whole** grid data. So in case of the usage of local data jqGrid do has separation between the data and the presentation. I can repeat: all will be much more clear if you append your question with the code. – Oleg Jan 28 '11 at 12:56
  • @AsGoodAsItGets: From your description about 2 fields of FlexBox contro (one normal input and one hidden) I not really understand what you mean and how you use FlexBox. So the corresponding code example can explain all easer. – Oleg Jan 28 '11 at 12:58
  • @AsGoodAsItGets: Sorry, but all what you posted are small fragments of code. One can verify code of `myCustomValue` without the code of `myCustomElem` which you not included. I suppose that the code return wrong value in case of `action==='get'`. How you use FlexBox control (probably inside of `beforeShowForm`) it is also unclear. I can not verify whether you have any 'id' conflicts. I don't understand why `myCustomValue` return any data of `action==='set'`. I don't understand why you need place hidden value inside the same cell and not use an additional hidden column. – Oleg Jan 28 '11 at 13:28
  • @AsGoodAsItGets: It is too many things are unclear. Do you have an url where one could see the problem live? – Oleg Jan 28 '11 at 13:29
  • I could use an additional hidden column, but I don't want to do it unless I absolutely have to. I will play around with your getGridParam() data object and see if it keeps the original values, maybe this will solve my problem, thanks for your help so far. – AsGoodAsItGets Jan 28 '11 at 13:53
  • And sorry, there's no URL, the application is on our intranet. – AsGoodAsItGets Jan 28 '11 at 13:53
  • The myCustomElem function is irrelevant, it just builds the FlexBox control before the form is shown and returns the constructed element. If you want to see examples of the FlexBox control you can check their website. – AsGoodAsItGets Jan 28 '11 at 13:55
  • @AsGoodAsItGets: I wrote you about the code of `myCustomElem` function only to be able to verify the code of `myCustomValue` which can be very important. – Oleg Jan 28 '11 at 14:03