自定义添加/编辑对话框中的jqGrid自定义添加/编辑对话框中的jqGrid(Customizing

2019-05-12 06:49发布

对不起,我不能发表图片,我太新。

在jqGrid的添加/编辑对话框我想加载基于早期所做的选择可选项目的列表。 在上面的图像,则该值的选择应基于在条件选择所选择的值来加载。 我相信路线在editoptions对象是使用dataurl去,但我有在这方面的问题。 这是令人不安的第一个问题是基于文档在这里似乎并不像有可用来触发一个事件时,标准的变化,让我来更新值列表中的值。

另外我对如何将数据应该从Ajax请求返回困惑。 在本文档中,它说:

设置editoptions dataUrl参数的editoptions dataUrl参数只适用于edittype的元素:选择。 该dataUrl参数代表从哪里HTML选择元素应该得到的URL。 当这个选项被设置时,元件将被填充来自AJAX请求的值。 该数据应与所需的选项有效的HTML选择元素”

这是不是意味着我将需要生成HTML和返回这是响应的一部分? 以前我一直在通过所有使用JSON我的数据。

Answer 1:

的jqGrid没有简单支承在从属选择的editoptions 。 因此,要实现一个必须使用change事件的主要选择手动更新的第二(依赖)选择的选项列表。

在演示你会发现你如何实现取决于选择。 我在试玩“本地”数据类型的使用,因此设定value的财产editoptions代替dataUrl ,但主要架构应该做什么保持不变。 此外,在演示我不仅使用表格编辑,但行内编辑了。 在这两种情况下的代码工作。 因为jqGrid的不支持的格式编辑模式的本地编辑的形式提交行不通。 原因我可以用我描述的技巧在这里 ,但代码将是更长的时间,将包含很多事情远非你的主要问题。 所以我决定张贴代码在提交不工作的形式。

下面你可以看到从演示的代码:

var countries = { '1': 'US', '2': 'UK' },
    states = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii', '5': 'London', '6': 'Oxford' },
    statesOfUS = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
    statesOfUK = { '5': 'London', '6': 'Oxford' },
    // the next maps contries by ids to states
    statesOfCountry = { '1': statesOfUS, '2': statesOfUK },
    mydata = [
        { id: '0', Country: '1', State: '1', Name: "Louise Fletcher" },
        { id: '1', Country: '1', State: '3', Name: "Jim Morrison" },
        { id: '2', Country: '2', State: '5', Name: "Sherlock Holmes" },
        { id: '3', Country: '2', State: '6', Name: "Oscar Wilde" }
    ],
    lastSel = -1,
    grid = $("#list"),
    resetStatesValues = function () {
        // set 'value' property of the editoptions to initial state
        grid.jqGrid('setColProp', 'State', { editoptions: { value: states} });
    };

grid.jqGrid({
    data: mydata,
    datatype: 'local',
    colModel: [
        { name: 'Name', width: 200 },
        {
            name: 'Country',
            width: 100,
            editable: true,
            formatter: 'select',
            edittype: 'select',
            editoptions: {
                value: countries,
                dataInit: function (elem) {
                    var v = $(elem).val();
                    // to have short list of options which corresponds to the country
                    // from the row we have to change temporary the column property
                    grid.jqGrid('setColProp', 'State', { editoptions: { value: statesOfCountry[v]} });
                },
                dataEvents: [
                    {
                        type: 'change',
                        fn: function (e) {
                            // build 'State' options based on the selected 'Country' value
                            var v = $(e.target).val(),
                                sc = statesOfCountry[v],
                                newOptions = '',
                                stateId,
                                form,
                                row;
                            for (stateId in sc) {
                                if (sc.hasOwnProperty(stateId)) {
                                    newOptions += '<option role="option" value="' + stateId + '">' +
                                        states[stateId] + '</option>';
                                }
                            }

                            resetStatesValues();

                            // populate the subset of contries
                            if ($(e.target).is('.FormElement')) {
                                // form editing
                                form = $(e.target).closest('form.FormGrid');
                                $("select#State.FormElement", form[0]).html(newOptions);
                            } else {
                                // inline editing
                                row = $(e.target).closest('tr.jqgrow');
                                $("select#" + $.jgrid.jqID(row.attr('id')) + "_State", row[0]).html(newOptions);
                            }
                        }
                    }
                ]
            }
        },
        {
            name: 'State',
            width: 100,
            editable: true,
            formatter: 'select',
            edittype: 'select',
            editoptions: { value: states }
        }
    ],
    onSelectRow: function (id) {
        if (id && id !== lastSel) {
            if (lastSel !== -1) {
                resetStatesValues();
                grid.jqGrid('restoreRow', lastSel);
            }
            lastSel = id;
        }
    },
    ondblClickRow: function (id) {
        if (id && id !== lastSel) {
            grid.jqGrid('restoreRow', lastSel);
            lastSel = id;
        }
        resetStatesValues();
        grid.jqGrid('editRow', id, true, null, null, 'clientArray', null,
            function () {  // aftersavefunc
                resetStatesValues();
            });
        return;
    },
    editurl: 'clientArray',
    sortname: 'Name',
    ignoreCase: true,
    height: '100%',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    pager: '#pager',
    caption: "Demonstrate dependend select/dropdown lists (edit on double-click)"
}).jqGrid('navGrid', '#pager', { edit: true, add: true, del: false, search: false, refresh: true },
    { // edit options
        recreateForm: true,
        viewPagerButtons: false,
        onClose: function () {
            resetStatesValues();
        }
    },
    { // add options
        recreateForm: true,
        viewPagerButtons: false,
        onClose: function () {
            resetStatesValues();
        }
    });

更新日期 :“更新2”的部分答案上演示最新版本。



文章来源: Customizing the Add/Edit Dialog in jqGrid