Showing combobox option instead of value in editor grid for extjs

While working with Editor Grid of ext js, I noticed that if a combobox is used in some column for editing, then it shows the option value instead of label. This forces you to keep option label and value same. But this is not possible always.

Searching the extjs forum for this problem, I got a hint from one of the post to use renderer in this situation.

To make this work, you first have to fetch all the options of the combobox and create a Javascript array through it which can then be used in renderer function.

JavaScript:
  1. var comboVals = document.getElementById('combo-id').options;
  2.  
  3. var comboArr = new Array;
  4.  
  5. for (var i = 0; i <comboVals.length; i++) {
  6.     comboArr[comboVals[i].value] = comboVals[i].text;
  7. }

Now write a renderer function like,

JavaScript:
  1. var comboRenderer = function (val) {
  2.     if (comboArr[val] != undefined) {
  3.         return comboArr[val];
  4.     } else {
  5.         return val;
  6.     }
  7. }

The above function will return the label for the selected value if found the array otherwise it will simply return the value itself.

6 Responses to Showing combobox option instead of value in editor grid for extjs

  1. Joshua August 22, 2009 at 7:56 pm #

    Hi, I test your example but it show a error, it can't find document.getElementById('combo-id'), maybe extjs assign a different id???

    thanks

  2. Aditya Mooley August 24, 2009 at 8:18 am #

    @Joshua - 'combo-id' is just a placeholder. Replace it with the actual id of the drop-down you are working with.

  3. Silvio September 1, 2009 at 10:18 pm #

    Great work, thanks a lot.

    There`s one issue, you should mention. If you use transformation of an existing HTML select box into an ExtJS one, the original select box will be lost after this process. So you have to perform your code before the transformation.

  4. pooja October 7, 2010 at 5:34 pm #

    where to add this code ? after declaring combobox?

  5. Jeff Thompson December 4, 2010 at 12:35 pm #

    I have the same question. My combo box is gone. Can you be specific about the order here? Just won't for me if i I loose the combo.

  6. Vlad May 11, 2011 at 11:20 pm #

    Here's a more elegant solution:

    function(val) {
    return theComboBox.getStore().getById(val).data.nameOfDisplayField;
    }

    Replace "theComboBox" with your combo object and "nameOfDisplayField" with the field that you want to be displayed.

    The data is already in an array, inside the combo's store, so there is no need to create an additional array.

Leave a Reply