动态填充Jqgrid下拉过滤器

时间:2018-11-27 13:18:34

标签: javascript json jqgrid

我已经修改了现有的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
            }
        })
    }   

但是,即使提供的列名(“ PropertyType”)存在,也不会在Json数据中找到。原始函数具有明确提到的列名,并且可以正常工作:

symbol = rows[i].PropertyType;

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

样本数据:

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

谢谢

1 个答案:

答案 0 :(得分:0)

在对象中将字符串参数作为属性传递的方式将无效。

在运行时等效于:

符号=行[i]。“ PropertyType”

这是不正确的。

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

符号=行[i] [columnName];

在运行时会产生

符号=行[i] [“ PropertyType”];

这是正确的