jqGrid postData.filters未定义

时间:2014-08-28 09:53:26

标签: javascript filter jqgrid postdata

我需要自定义jqGrid。但postData.Filters未定义。由于我是jquery和javascript的新手,即使经过一天的努力,我也无法找出究竟是什么问题。

我正在使用以下javascript。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/redmond/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/css/ui.jqgrid.css" />
    <link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery-ui-multiselect/1.13.6/jquery.multiselect.css" />

    <style type="text/css">
        html, body {
            font-size: 75%;
        }

        .ui-multiselect-menu {
            font-size: 1.2em;
        }

        .ui-jqgrid .ui-jqgrid-htable .ui-search-toolbar th {
            height: auto;
        }
    </style>

    <script src="js/jquery-1.11.0.min.js"></script>
    <script src="js/jquery.jqGrid.min.js"></script>
    <script src="js/jquery.jqGrid.src.js"></script>
    <script src="js/i18n/grid.locale-en.js"></script>

    <script type="text/javascript">
        $.jgrid.no_legacy_api = true;
        $.jgrid.useJSON = true;
    </script>

    <script type="text/javascript">
        $(function () {
            'use strict';

            var jsonData = {
                "Employees": [
                            {
                                "Emp ID": 1.0,
                                "Name": "Anubhav",
                                "Gender": "Male",
                                "Department": "DEVELOPMENT",
                                "Company": "SOPRA",
                                "Salary": 100000
                            },
                            {
                                "Emp ID": 2.0,
                                "Name": "Shailesh",
                                "Gender": "Male",
                                "Department": "DEVELOPMENT",
                                "Company": "SOPRA",
                                "Salary": 10000
                            },
                            {
                                "Emp ID": 3.0,
                                "Name": "Deepak",
                                "Gender": "Male",
                                "Department": "SALES",
                                "Company": "ROQUETTE",
                                "Salary": 20000
                            },
                            {
                                "Emp ID": 4.0,
                                "Name": "Seema",
                                "Gender": "Female",
                                "Department": "PURCHASE",
                                "Company": "ROQUETTE",
                                "Salary": 25000
                            }
                ]},

            $grid = $("#list2"),
            myDefaultSearch = 'cn',

            getColumnIndexByName = function (columnName) {
                var cm = $(this).jqGrid('getGridParam', 'colModel'), i, l = cm.length;
                for (i = 0; i < l; i += 1) {
                    if (cm[i].name === columnName) {
                        return i; // return the index
                    }
                }
                return -1;
            },
                modifySearchingFilter = function (separator) {                    
                    var i, l, rules, rule, parts, j, group, str, iCol, cmi, cm = this.p.colModel;
                    window.alert("Hello");
                    window.alert(this.p.postData.filters);
                    var filters = $.parseJSON(this.p.postData.filters);
                    window.alert("Hello 2");
                    if (filters && filters.rules !== undefined && filters.rules.length > 0) {
                        rules = filters.rules;
                        for (i = 0; i < rules.length; i++) {                            
                            rule = rules[i];
                            iCol = getColumnIndexByName.call(this, rule.field);
                            cmi = cm[iCol];                            
                            if (iCol >= 0 &&
                                    ((cmi.searchoptions === undefined || cmi.searchoptions.sopt === undefined)
                                        && (rule.op === myDefaultSearch)) ||
                                    (typeof (cmi.searchoptions) === "object" &&
                                        $.isArray(cmi.searchoptions.sopt) &&
                                        cmi.searchoptions.sopt[0] === rule.op)) {
                                // make modifications only for the 'contains' operation
                                parts = rule.data.split(separator);
                                if (parts.length > 1) {
                                    if (filters.groups === undefined) {
                                        filters.groups = [];
                                    }
                                    group = {
                                        groupOp: 'OR',
                                        groups: [],
                                        rules: []
                                    };
                                    filters.groups.push(group);
                                    for (j = 0, l = parts.length; j < l; j++) {
                                        str = parts[j];
                                        if (str) {
                                            // skip empty '', which exist in case of two separaters of once
                                            group.rules.push({
                                                data: parts[j],
                                                op: rule.op,
                                                field: rule.field
                                            });
                                        }
                                    }
                                    rules.splice(i, 1);                                   
                                    i--; // to skip i++
                                }
                            }
                        }
                        this.p.postData.filters = JSON.stringify(filters);
                    }
                },
                dataInitMultiselect = function (elem) {
                    setTimeout(function () {
                        var $elem = $(elem), id = elem.id,
                            inToolbar = typeof id === "string" && id.substr(0, 3) === "gs_",
                            options = {
                                selectedList: 2,
                                height: "auto",
                                checkAllText: "all",
                                uncheckAllText: "no",
                                noneSelectedText: "Any",
                                open: function () {
                                    var $menu = $(".ui-multiselect-menu:visible");
                                    $menu.width("auto");
                                    return;
                                }
                            },
                            $options = $elem.find("option");
                        if ($options.length > 0 && $options[0].selected) {
                            $options[0].selected = false; // unselect the first selected option
                        }
                        if (inToolbar) {
                            options.minWidth = 'auto';
                        }
                        $elem.multiselect(options);
                        $elem.siblings('button.ui-multiselect').css({
                            width: inToolbar ? "98%" : "100%",
                            marginTop: "1px",
                            marginBottom: "1px",
                            paddingTop: "3px"
                        });
                    }, 50);
                };

            $grid.jqGrid({
                datatype: "json",
                data: jsonData,
                colNames: ["Emp ID", "Name", "Department", "Company", "Salary"],
                colModel: [
                    { name: "Emp ID", sorttype: 'integer' },
                    { name: "Name" },
                    {
                        name: "Department", width: 100, align: 'center', formatter: 'select', edittype: 'select', stype: 'select',
                        editoptions: { value: 'DEVELOPMENT;SALES;PURCHASE', defaultValue: 'DEVELOPMENT', multiple: true },
                        searchoptions: { sopt:['eq', 'ne'], value: 'DEVELOPMENT;SALES;PURCHASE', attr: { multiple: 'multiple', size: 4 }, dataInit: dataInitMultiselect }
                    },
                    { name: "Company" },
                    { name: "Salary", sorttype: 'integer' }
                ],
                cmTemplate: { editable: false },
                rowNum: 10,
                mtype: "GET",                
                rowList: [10, 20, 100],
                pager: "#pager2",
                height: "auto",
                viewrecords: true,
                gridview: true,
                ignoreCase: true,
                rownumbers: true,
                cellEdit: true,
                cellsubmit: "clientArray",
                editurl: "clientArray",
                sortname: 'Emp ID',
                caption: "CDDI Sample Grid",
                beforeRequest: function () {                    
                   modifySearchingFilter.call(this, ',');
                }
            });
            $grid.jqGrid('filterToolbar', {search:true, searchOperators: true, stringResult: true, searchOnEnter: true, defaultSearch: 'cn' });            
            $grid.jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false, search: true }, {}, {}, {}, {
                multipleSearch: true,
                multipleGroup: true,
                recreateFilter: true
            });
        });
    </script>

</head>
<body>
    <table id="list2"></table>
    <div id="pager2"></div>    
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我看到你使用my old answer中的代码。

错误&#34; jqGrid postData.filters未定义&#34;通过添加行

可以很容易地修复undefined
if (postData.filters == null) {
    return;
}
在行modifySearchingFilter之前的var filters = $.parseJSON(postData.filters);内部

代码中的其他重要错误是datatype: "json"而非datatype: "local"的使用。选项datatype: "json"表示网格中的数据将通过Ajax请求加载到url参数指定的URL。

另一个错误是使用data: jsonData而不是data: jsonData.Employees。了解data参数的值必须是数组非常重要。

还有一句话:我严格建议您不要在name的{​​{1}}属性中使用任何特殊字符(目前您有colModel)。 name: "Emp ID"属性的值将用作网格的某些内部元素的name,但空间将由jQuery和CSS用作分隔符和元字符(请参阅here例如)。因此,我建议您将id更改为name: "Emp ID",以便将来减少问题。

您将被罚款here的固定演示。它使用以下代码

name: "EmpId"