2

I have utilised the example code at Example Code at this link

and I have got my grid to show a dynamically constructed select dropdown on add and edit. However when it is just showing the data in the grid it shows the dropdown index instead of its associated data. Is there a way to get the grid to show the data associated with the index instead of the index itself.

e.g. the data on my select could be "0:Hello;1:World"; The drop down on the edit/add window is showing Hello and World and has the correct indexes for them. If the cell has a value of 1 I would expect it to show World in the grid itself but it is showing 1 instead.

Here is the row itself from my grid:

{ name: 'picklist', index: 'picklist', width: 80, sortable: true, editable: true,
  edittype: "select", formatter: "select", editrules: { required: true} },

I am filling the dynamic data content in the loadComplete event as follows:

$('#mygrid').setColProp('picklist', { editoptions: { value: picklistdata} });

picklist data is a string of "0:Hello;1:World" type value pairs.

Please can anyone offer any help. I am fairly new to JQGrids so please could you also include examples.

1

2 Answers 2

1

I know you have already solved the problem but I faced the same problem in my project and would like to offer my solution.

First, I declare a custom formatter for my select column (in this case, the 'username' column).

$.extend($.fn.fmatter, {
    selectuser: function(cellvalue, options, rowdata) {
        var userdata;
        $.ajax({
            url:'dropdowns/json/user',
            async:false,
            dataType:'json',
            cache:true,
            success: function(data) {
                userdata = data;
            }
        });
        return typeof cellvalue != 'undefined' ? userdata[cellvalue] : cellvalue ;
    }
});

This formatter loads up the mapping of id and user in this case, and returns the username for the particular cellvalue. Then, I set the formatter:'selectuser' option to the column's colModel, and it works.

Of course, this does one json request per row displayed in the grid. I solved this problem by setting 10 seconds of caching to the headers of my json responses, like so:

private function set_caching($seconds_to_cache = 10) {
    $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
    header("Expires: $ts");
    header("Pragma: cache");
    header("Cache-Control: max-age=$seconds_to_cache");
}

I know this solution is not perfect, but it was adequate for my application. Cache hits are served by the browser instantly and the grid flows smoothly. Ultimately, I hope the built-in select formatter will be fixed to work with json data.

0

If you save in jqGrid ids of the select elements and want to show the corresponding textes then you should use formatter:'select' in the colModel (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter#formatter_type_select) together with the edittype: "select".

The Usage of stype: 'select' could be also interesting for you if you plan to support data searching.

1
  • Thank you for the information but I have already tried this. In the end I had to reformat my SQL to being back the data instead of the ID for the gird cell. I would of thought that a row of type select would use the ID in the datasource as an index into the list of the select but I guess it doesn't work like that after all, or maybe its a fault with the dynamically filled Select types. Jul 27, 2010 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.