根据用户输入过滤jqgrid

时间:2010-04-05 04:51:22

标签: c# jquery jqgrid-asp.net

除了一个小问题,我的jqgrid一切正常。我在下面定义了postData:

 $(document).ready(function() {

$("#ctl00_ContentPlaceHolder2_drpUSite").change(function() {
    site = ($("#ctl00_ContentPlaceHolder2_drpUSite").val());
    loadusergrid();
});
var usrparams = new Object();
var site = ($("#ctl00_ContentPlaceHolder2_drpUSite").val());
//----grid code---------
$("#users").jqGrid({
    prmNames: {
        _search: "isSearch",
        nd: null,
        rows: "numRows",
        page: "page",
        sort: "sortField",
        order: "sortOrder"
    },
    // add by default to avoid webmethod parameter conflicts
    postData: { searchString: '', searchField: '', searchOper: '', sites: site },
    datatype: function(postdata) {
        mtype: "GET",
        $.ajax({
            url: 'Users.aspx/GetUsers',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(postdata),
            dataType: "json",
            success: function(data, st) {
                if (st == "success") {
                    var grid = $("#users")[0];
                    var m = JSON.parse(data.d);
                    grid.addJSONData(m);
                }
            },
            error: function() {
                alert("Loading Failed!");
            }
        });
    },
    // this is what jqGrid is looking for in json callback
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        cell: "cell",
        id: "login",
        repeatitems: true
    },
    colNames: ['Login', 'First Name', 'Last Name', 'Email', 'Site', 'Role', 'Room', 'UnitID', 'Supervisor', 'Super'],
    colModel: [
    { name: 'login', index: 'login', width: 20 },
    { name: 'fname', index: 'fname', width: 20, hidden: true },
    { name: 'lname', index: 'lname', width: 60, align: "center", sortable: true, searchoptions: { sopt: ['eq', 'ne']} },
    { name: 'email', index: 'email', width: 20, align: "center", sortable: false },
    { name: 'site', index: 'site', width: 50, align: "center", sortable: true, searchoptions: { sopt: ['eq', 'ne']} },
    { name: 'role', index: 'role', width: 15, align: "center", sortable: true, searchoptions: { sopt: ['eq', 'ne']} },
    { name: 'room', index: 'room', width: 30, align: "center", sortable: true },
    { name: 'unitid', index: 'unitid', width: 10, align: "center", sortable: false },
    { name: 'super', index: 'super', width: 20 },

    { name: 'supername', index: 'supername', width: 10, align: "center", sortable: false },
],

    pager: "#pageusers",
    viewrecords: true,
    caption: "Registered Users",
    imgpath: 'themes/steel/images',
    rowNum: 20,
    rowList: [10, 20, 30, 40, 50],
    sortname: "pname",
    sortorder: "desc",
    showpage: true,
    gridModel: true, gridToolbar: true,
    onSelectRow: function(id) {
        var ret = jQuery("#users").getRowData(id);
        accpara.id = ret.id;
        accpara.pname = ret.pname;
        accpara.pid = ret.pid;
        accpara.bld = ret.bld;
        accpara.cname = ret.cname;
        accpara.amt = ret.amt;
        accpara.status = ret.status;
        accpara.notes = ret.notes;
        accpara.lname = ret.lname;
    }
});
jQuery("#users").navGrid('#pageusers', { view: false, del: false, add: false, edit: false },
 {}, // default settings for edit
 {}, // default settings for add
 {}, // delete
 {closeOnEscape: true, multipleSearch: true,
 closeAfterSearch: true
 }, // search options
 {}
 );
$("#users").setGridWidth(1300, true);
$("#users").setGridHeight(500, true);
jQuery("#users").jqGrid('filterToolbar');
//----grid code ends here



   function loadusergrid() {
       $("#users").setGridParam({ page: 1 }, { pgbuttons: true }, { pginput: true }, { postData: { "site": site} }).trigger("reloadGrid");
    }
});

第一次加载页面时,这可以正常工作..

现在我有4个下拉菜单可以过滤用户。我写了一个函数,当下拉列表被更改时重新加载网格,但它不工作..我在这里做错了什么?当我为下拉菜单启用回发时,我得到过滤结果。我想避免在我的页面上回发:)。现在我已经添加了网站下拉列表作为过滤器。一旦这开始工作,就加上剩下的3个。

firebug显示ajax调用已成功触发但有一个空的网站名称。请注意,当第一次加载页面时,站点下拉列表会保留一个空值。

提前致谢

1 个答案:

答案 0 :(得分:2)

$("#mygrid").setPostData({parameters});可以解决问题