I am working with Coldfusion and jqgrid.
Is there a way to populate the colModel editoptions dynamically from query results?
If you need to set dynamically the options like 1:female;2:male
, then I suppose that you fill grid with data which contains 1 or 2 in the specified column. So you have to use formatter: "select"
(see the documentation). Thus you should be able to set the properties like below
{name: "sex", formatter: "select", editoptions: { value: "1:female;2:male" }}
If you need to set such options dynamically, that you want to load the information about editoptions.value
from the server.
The most native callback which you can use for it is beforeProcessing
. The callback will be processed before the data returned from the server will be displayed using the formatter. So one can make any changes of formatoptions
or editoptions
.
If the server response looks now like
{
"rows": [
{"id": 123, "name": "John", "sex": "2"},
{"id": 456, "name": "Mary", "sex": "1"}
]
}
then you can extend the data of the server response to the following
{
"rows": [
{"id": 123, "name": "John", "sex": "2"},
{"id": 456, "name": "Mary", "sex": "1"}
],
"colModelExtentions": {
"sex": {"formatter": "select", "editoptions": {"value": "1:female;2:male"}}
}
}
In the way you specify some changes of specific items of the colModel
directly inside of the server response. The implementation of the beforeProcessing
callback which process such data can looks like
beforeProcessing: function (data) {
var cmName, cmExt = data.colModelExtentions, $self = $(this),
p = $self.jqGrid("getGridParam");
for (cmName in cmExt) {
// enumerate properties of colModelExtentions like sex
if (cmExt.hasOwnProperties(cmName)) {
$self.jqGrid("setColProp", cmName, cmExt[cmName]);
}
}
}
and to define the "sex"
columns in colModel
like
{name: "sex", width: 50}
You will find more details about the scenario in the answer and this one. You can extend the information which return the server. In the case you can need to call setColWidth
or setLabel
additionally to apply dynamically the column width and the text of the column header.