jqGrid colModel 动态搜索选项



是否可以为 jqGrid 列提供动态(非硬编码)搜索过滤器值?

所以在示例中,例如:

 $('#my-grid').jqGrid({
            datatype: 'json',
            loadonce: true,
            jsonReader: common.jqgrid.jsonReader('Workorder'),
            mtype: 'POST',
            colNames: ['Project', 'PO Number', 'Type', 'Folder'],
            colModel: [
                { name: 'Project', index: 'Project', width: 80, sortable: false, search:false},
                { name: 'PONumber', index: 'PONumber', width: 60, sortable: false, search: true },
                { name: 'Type', index: 'Type', width: 60, sortable: false, search: true},
                { name: 'Folder', index: 'Folder', width: 60, sortable: false, search: false },
                           ],
            scroll: true,
                   });

我希望该类型有一个下拉过滤器,其值是返回的数据子集中的不同值数组。

我将如何实现这一目标?

编辑

jqGrid 数据可以直接访问吗?我正在寻找类似的东西 Data.Cols[2].Distinct,这将为我提供第 3 列(在本例中)中不同的值数组。这可能吗?

编辑 2

这是代码:

onLoadComplete: function (data) {
        var $this = $('#gridReport');
        if ($this.jqGrid("getGridParam", "datatype") === "json") {
           
            // first loading from the server
            // one can construct now DISTINCT for 'Type' column and
            // set searchoptions.value
            $this.jqGrid("setColProp", "Type", {
                stype: "select",
                searchoptions: {
                    value: buildSearchSelect(getUniqueNames("Type")),
                    sopt: ["eq", "ne"]
                }
            });            
        }
    },

我不确定我是否正确理解了你。可能您想使用带有下拉列表(stype: 'select')的高级搜索对话框,其中包含网格的3-d列的不同值?我建议您阅读答案,该答案显示了如何动态设置searchoptions.value的主要思想。您使用loadonce: true .因此,您可以在第一次从服务器加载数据时在loadComplete内部调用setColProp。您可以包括对 datatype 值的其他测试。从服务器加载时,该值为 "json" 。稍后将更改为 "local" .因此,代码可能如下所示:

loadComplete: function () {
    var $this = $(this);
    if ($this.jqGrid("getGridParam", "datatype") === "json") {
        // first loading from the server
        // one can construct now DISTINCT for 'Type' column and
        // set searchoptions.value
        $this.jqGrid("setColProp", "Type", {
            stype: "select",
            searchoptions: {
                value: buildSearchSelect(getUniqueNames("Type")),
                sopt: ["eq", "ne"]
            }
        });
    }
}

这就是我最终这样做的方式,我最终使用 jquery 直接填充下拉列表,因为 jqgrid 过滤器在任何时候都不可用(如果是,我很想看到一个记录在案的例子):

 onLoadComplete: function (data) {
        if ($('#mygrid').jqGrid("getGridParam", "datatype") === "json") {
            //get the distinct types from the data set coming back
            var length = data.length;
            var types = [];
            for (var i = 0; i < length; i++) {
                types.push(data[i]['Type']);
            }
            var uniqueTypes = getUnique(types);
            $('#gridId select[name="Type"]').empty().html('<option value="">All</option>');
            for (var i = 0; i < uniqueTypes.length; i++) {
                  $('#gridId select[name="Type"]').append($('<option value="' + uniqueTypes[i] + '">' + uniqueTypes[i] + '</option>'));
            }
        }

最新更新