jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Get json-based dropdownlist column value
Tagged: DropDownList, grid, json, value
This topic contains 1 reply, has 1 voice, and was last updated by benahod 9 years, 11 months ago.
-
Author
-
Hi,
I have a problem of getting the value from a json-based dropdownlist column type. After I submit the form, it returns only the label, not the value. But I don’t have any problem of getting the value from the array-based dropdownlist column type.
Here is my code of getting a json-based dropdownlist:
// define type dropdownlist var sourcetype = { datatype: "json", datafields: [ { name: 'value', type: 'int' }, { name: 'label', type: 'string' } ], id: 'value', url: '<?php echo some_json_code(); ?>', /* Produces something like [{"label":"Small","value":1},{"label":"Medium","value":2},{"label":"Large","value":3},{"label":"Very large","value":4},{"label":"Huge","value":5},{"label":"Gigantic","value":6},{"label":"Universe-like","value":7}] */ async: false }; var dataAdapterType = new $.jqx.dataAdapter(sourcetype, { autoBind: true }); /*...*/ /* some jqxgrid parameters - begin */ columns: [ { text: 'Type', width: 180, datafield: 'totype_id', columntype: 'dropdownlist', createeditor: function (row, column, editor) { // assign a new data source to the combobox. editor.jqxDropDownList({ autoDropDownHeight: true, source: dataAdapterType, displayMember: 'label', valueMember: 'value' }); }, // update the editor's value before saving it. cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) { // return the old value, if the new value is empty. if (newvalue == "") return oldvalue; } } ] /* some jqxgrid parameters - end */ /*...*/
And finally the getrows method…
$('form').on('submit', function() { var gridtable = $('#jqxgrid').jqxGrid('getrows'); $('#gridtablehidden').val(JSON.stringify(gridtable )); return; });
the code above only posts something like:
Array ( [0] => Array ( [totype_id] => Large /* this is a label. It has to be a value instead */ ) )
How can I get the value instead?
Thank you.
Regards.
– Ben –I just realised that I forgot to define a displayfield object inside the columns object of the jqxgrid.
By modifying the code to something like…
/* some jqxgrid parameters - begin */ columns: [ { text: 'Type', width: 180, datafield: 'totype_id', columntype: 'dropdownlist', displayfield: 'typelabel', //<<-- This is the line I forgot to define. createeditor: function (row, column, editor) { // assign a new data source to the combobox. editor.jqxDropDownList({ autoDropDownHeight: true, source: dataAdapterType, displayMember: 'label', valueMember: 'value' }); }, // update the editor's value before saving it. cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) { // return the old value, if the new value is empty. if (newvalue == "") return oldvalue; } } ] /* some jqxgrid parameters - end */
We will got something like…
Array ( [0] => Array ( [totype_id] => 3 /* this is the value */ [typelabel] => Large /* this is the label */ ) )
Now we got the value as totype_id and the label as typelabel.
Case closed.
Thank you very much!
-
AuthorPosts
You must be logged in to reply to this topic.