I am using the open source jqGrid plugin with EF4 and ASP.NET Web Forms. I need to set an input element in an inline-editable grid row based on a column value from the DB. For example, the first row could contain a DDL, the second row could contain a checkbox, etc.
I'm trying to achieve this using the custom_element and custom_values, like so:
$("#grid1").jqGrid({
url: 'Default.aspx/getGridData',
datatype: 'json',
...
colModel: [
...
//contains the input type ('select', etc.)
{ name: 'InputType', hidden:true },
...
//may contain a string of select options ('<option>Option1</option>'...)
{
name: 'Input',
editable:true,
edittype:'custom',
editoptions:{
custom_element: /* want cell value from InputType column here */ ,
custom_value: /* want cell value from Input column here */
}
},
...
]
});
The jqGrid docs say that I can call custom functions to set custom_element and custom_values, but I don't see how I can capture column values and pass them into my custom functions.
For setting custom_values, I did notice Oleg's nice solution using the list: parameter, but that appeared to involve an extra Ajax call. I want to avoid this, as I already have the all data I need from the initial Ajax call for the grid.
In summary, I need to do the following while in inline-edit mode:
- dynamically assign an input type from a DB value
- dynamically assign input values (for DDL or checkboxes) from a DB string
I am also open to skipping the use of custom_element and custom_values, but then I still face the same problem of dynamically setting the edittype and editoptions:{value:} parameters.
Any ideas on how to do this? Is there a different approach that I should be taking?
UPDATE: Thanks for your efforts to help me out. Per request, here is an abbreviated example of my JSON response:
{"d":[
{"Input":null,"InputType":"select"},
{"Input":"From downtown, proceed west on Interstate 70.", "InputType":"text"}
]}
With this data, I would want to show an empty select in one row, and a populated text field in the next row. Both would be editable inline.
SOLUTION: I have returned to this problem in order to find a solution that does not involve using custom_element and custom_values. Here is my solution (based on the accepted answer below) to changing edittype and editoptions :
loadComplete: function () {
var rowIds = $("#grid1").jqGrid('getDataIDs');
$.each(rowIds, function (i, row) {
var rowData = $("#grid1").getRowData(row);
if (rowData.InputType == 'select') {
$("#grid1").jqGrid('restoreRow', row);
var cm = $("#grid1").jqGrid('getColProp', 'Input');
cm.edittype = 'select';
cm.editoptions = { value: "1:A; 2:B; 3:C" };
$("#grid1").jqGrid('editRow', row);
cm.edittype = 'text';
cm.editoptions = null;
}
});
}
Nota Bene: One important thing for me was remembering to set the editoptions back to null, after calling editrow. Also, as Oleg mentioned in the comments, avoiding the use of custom elements allows me to implement datepicker inputs without extra trouble. This was important for my app, so I ended up accepting Oleg's answer, but I still upvoted Walter's answer, as well. If this is bad form, I sincerely apologize. I simply wanted to reward the solution that worked best for me.