1

我有一个 jqGrid,其中有几个字段可以动态填充 filterToolbar 中的下拉框。我希望在“添加记录”对话框中显示相同的结果,为产品和环境字段提供下拉选择器而不是文本输入字段。如您所见,我正在尝试在 beforeShowForm 事件中执行此操作。那是合适的地方吗?将值设置为先前定义的 prodValues 和 envValues 变量会很好,但如有必要,我可以再次发出 ajax 请求(我已经尝试过但也失败了)。

照原样,代码仍然会为 Product 和 Environment 字段生成带有文本输入的表单。如何将它们更改为选择器?

$(function () {
            var grid = $("#PSGrid");

            var prodValues = $.ajax({
                url: "jqGridHandler.ashx?oper=pVals",
                async: false,
                success: function (data) {

                }
            }).responseText;

            var envValues = $.ajax({
                url: "jqGridHandler.ashx?oper=eVals",
                async: false,
                success: function (data) {

                }
            }).responseText;

            var lastsel = -1;

            // build the grid
            grid.jqGrid({
                url: 'jqGridHandler.ashx',
                editurl: 'jqGridEditor.ashx',
                datatype: 'json',
                height: 550,
                width: 'auto',
                colNames: ['ID', 'Product', 'Environment', 'Hostname', 'IP', 'Description', 'Type', 'Ports Used'],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, sortable: true, hidden: false, editable: false, key: true },
                    {
                        name: 'Product', index: 'Product', width: 125, sortable: true, editable: true,
                        stype: 'select', searchoptions: { value: prodValues, sopt: ['eq'] }
                    },
                    {
                        name: 'Environment', index: 'Environment', width: 100, sortable: true, editable: true,
                        stype: 'select', searchoptions: { value: envValues, sopt: ['eq'] }
                    },
                    { name: 'Hostname', index: 'Hostname', width: 200, sortable: true, editable: true },
                    { name: 'IP', index: 'IP', width: 125, sortable: false, editable: true },
                    { name: 'Description', index: 'Description', width: 200, sortable: true, editable: true },
                    { name: 'Type', index: 'Type', width: 75, sortable: true, editable: true },
                    { name: 'Ports Used', index: 'Ports Used', width: 80, sortable: false, editable: true }
                ],
                rowNum: 1000,    // hack to show everything; there's probably a better property to use than this
                pager: '#PSGridPager',
                sortname: 'ID',
                pgbuttons: false,
                pgtext: null,
                viewrecords: false,
                sortorder: 'asc',
                ignoreCase: true,
                caption: 'Click a row to edit.  [Enter] to save, [Esc] to cancel.',
                loadonce: true,
                onSelectRow: function (id) {
                    if (id && id !== lastsel) {
                        grid.jqGrid('restoreRow', lastsel);
                        grid.jqGrid('editRow', id, true);
                        lastsel = id;
                    }
                }
            });

            grid.jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: "cn" });
            grid.jqGrid('navGrid', '#PSGridPager', { edit: false, add: true, del: true, search: false, refresh: true, paging: false },
                { /* edit options */ },
                { /* add options */
                    closeOnEscape: true,
                    width: 400,
                    beforeShowForm: function (formid) {
                        $(this).setColProp('Product', { editoptions: { value: prodValues } });
                        $(this).setColProp('Environment', { editoptions: { value: envValues } });
                    }
                });
            grid.jqGrid('navButtonAdd', '#PSGridPager', {
                caption: "Export to Excel",
                onClickButton: function () {
                    grid.jqGrid('excelExport', { url: "jqGridHandler.ashx" });
                }
            });
        });
4

1 回答 1

0

将其配置为 colModel 中的选择:

formatter:'select', edittype:"select", editoptions:{ value: prodValues }

然后删除您的 beforeShowForm 代码。

于 2013-06-26T17:58:55.857 回答