当网格中的行数为零时过滤jqgrid时出现异常

时间:2013-07-11 10:57:04

标签: asp.net jqgrid filter

当行数为零并应用过滤器时,如何避免从jQgrid获取异常。我的代码看起来像这样

function filterGrid(grid, siteId, buildingId, cityId, selectedType) {   
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!

        var yyyy = today.getFullYear();
        if (dd < 10) { dd = '0' + dd } if (mm < 10) { mm = '0' + mm } today = mm + '/' + dd + '/' + yyyy;

        if (cityId == -1) {
            grid.setGridParam({ search: false });
        }
        else {
            var filter = { groupOp: "AND", rules: [{ field: "CityID", op: "eq", data: cityId}] };

            if (siteId >= 0)
                filter = { groupOp: "AND", rules: [{ field: "CityID", op: "eq", data: cityId }, { field: "SiteID", op: "eq", data: siteId}] }; // , { field: "BuildingID", op: "eq", data: buildingId}

            if (buildingId >= 0)
                filter = { groupOp: "AND", rules: [{ field: "CityID", op: "eq", data: cityId }, { field: "SiteID", op: "eq", data: siteId }, { field: "BuildingID", op: "eq", data: buildingId}] };

            if (selectedType == "Outstanding Books")
                filter = { groupOp: "AND", rules: [{ field: "CityID", op: "eq", data: cityId }, { field: "SiteID", op: "eq", data: siteId }, { field: "BuildingID", op: "eq", data: buildingId }, { field: "IsReturnDateNull", op: 'eq', data: true }, { field: "DueDate", op: 'gt', data: today}] };

            //For filtering
            grid.setGridParam({ search: true, postData: { filters: JSON.stringify(filter)} });

            //For searching   
            //grid.setGridParam({ search: true, postData: { searchOper: "eq", searchField: "Status", searchString: selectedVal} });
        }

        grid.trigger("reloadGrid");
    }

我编写的代码工作得很好但是如果数据源没有与网格绑定的记录然后我得到一个异常,我想把行计数放到服务器端和客户端的隐藏字段中如果隐藏字段中的行计数为零,则跳过搜索,但由于数据源在异步回调期间被分配给jqgrid,因此隐藏字段中的值不会更新(总是在客户端的hiddenfield中获取空字符串)所以当没有数据绑定到jqgrid时,任何正文都可以帮助我避免从jqgrid获取null异常。

1 个答案:

答案 0 :(得分:0)

我终于找到了这个问题的答案。我使用事件搜索jqgrid,在服务器端,我从包含当前行的隐藏字段获取行计数,如果行计数为0,则取消搜索操作。

protected void grdBookTransaction_Searching(object sender, Trirand.Web.UI.WebControls.JQGridSearchEventArgs e)
    {
        if (string.IsNullOrEmpty(GridCount.Value) || GridCount.Value == "0")
        {
            e.Cancel = true;
        }
    }