【问题标题】:Dynamically populate Jqgrid drop-down filter动态填充 Jqgrid 下拉过滤器
【发布时间】:2018-11-27 13:18:34
【问题描述】:

我已经修改了一个现有的 Javascript 函数,以允许我填充多个 Jqgrid 下拉过滤器。代码是:

 jQuery("#jQGridDemo").jqGrid({
        url: 'http://localhost:58404/JQGridHandler.ashx',
        datatype: "json",
        colNames: ['Property ID', 'Property Ref', 'Short Address', 'Scheme Code', 'Scheme Name'],
        colModel: [
                    { name: 'PropertyID', index: 'PropertyID', width: 70, align: "left", stype: 'text', sortable: true},
                    { name: 'PropertyRef', index: 'PropertyRef', width: 75, align: "left", stype: 'text', sortable: true},
                    { name: 'ShortAddress', index: 'ShortAddress', width: 200,  align: "center", sortable: true},
                    { name: 'SchemeCode', index: 'SchemeCode', width: 80, align: "center", sortable: true },
                    { name: 'SchemeName', index: 'SchemeName', width: 80, align: "center",  sortable: true },
                    {name: 'PropertyType',width: 80},

        ],

        beforeProcessing: function (data) {

            getDropDownValues(data, "PropertyType")

        }

.jqGrid('destroyFilterToolbar')
                .jqGrid('filterToolbar', {
                    stringResult: true,
                    searchOnEnter: false,
                    defaultSearch: "cn"
                });
        } 

function getDropDownValues(data, columnName) {
        var propertyMap = {}, propertyValues = ":All", rows = data, i, symbol;
        for (i = 0; i < rows.length; i++) {
            symbol = rows[i].columnName;
            if (!propertyMap.hasOwnProperty(symbol)) {
                propertyMap[symbol] = 1;
                propertyValues += ";" + symbol + ":" + symbol;
            }
        }
        $(this).jqGrid("setColProp", 'columnName', {
            stype: "select",
            searchoptions: {
                value: propertyValues
            }
        })
    }   

但是,在 Json 数据中找不到提供的列名(“PropertyType”),即使它存在。原始函数具有明确提到的列名并且有效:

symbol = rows[i].PropertyType;

有人知道我应该如何引用作为变量提供而不是明确提及的列名吗?

样本数据:

 [{"PropertyID":1,"PropertyRef":"1","ShortAddress":"99 ROCK LANE,BODMIN,PL91 1NR","SchemeCode":"700000","SchemeName":"LODMIN","PropertyType":"HOU"} 

谢谢

【问题讨论】:

  • 请发布您的完整 jqGrid 设置和响应中的一些演示数据以获得帮助
  • 我已经更新了原帖。

标签: javascript json jqgrid


【解决方案1】:

你将字符串参数作为属性传递给对象的方式是行不通的。

在运行时这相当于:

symbol = rows[i]."PropertyType"

这是不正确的。

幸运的是,您可以将它作为数组元素传递。像这样

symbol = rows[i][columnName];

在运行时会产生什么

symbol = rows[i]["PropertyType"];

这是正确的

【讨论】:

  • 谢谢托尼。这样可行!非常感谢您的帮助。
  • @Sanxion 没问题。很高兴能帮助你。只是要注意,接受答案是一种好习惯,以便其他阅读此内容的人可以看到它解决了问题
猜你喜欢
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多