jqGrid - 服务器端寻呼机

时间:2011-12-13 09:29:12

标签: jquery jqgrid jqgrid-asp.net pager

我坚持这个问题,并会感谢任何帮助...(奥列格,你在这里????)

我有一个jqgrid进行排序,在服务器端搜索,我知道需要在服务器上进行分页,我发现将执行该操作的webService方法,但是当我点击任何分页按钮时没有任何反应并且呼叫不会发送到服务器。

任何人都可以帮助我,看看我的代码中有什么问题???

我的代码是休闲:

$(myGrid).jqGrid({
    datatype: function (pdata) {
        Invoke("GetAll", pdata);
    },
    colNames: columnNames,
    colModel: columnModel,
    jsonReader: {
        root: "Result",
        page: "page",
        total: "total",
        records: "records"
    },
    rowNum: 10,
    //rowList: [5, 10, 20, 30],
    pager: '#ViewNQueryPager',
    viewrecords: true,
    shrinkToFit: true,
    loadtext: "Loading....",
    emptyrecords: "No records to view",
    viewrecords: true,
    //scrollOffset: 0,
    height: '300',
    //width: '100%',
    ignoreCase: true,
    sortname: 'ID',
    sortable: true,
    sortorder: 'asc',
    grouping: true,
    groupingView: {
        groupField: ['ID']
    }
});
$(myGrid).jqGrid('navGrid', '#ViewNQueryPager', { del: false, add: false, edit: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: true, showQuery: true, onSearch: function (response) { showQueryDetails(); } });
$(myGrid).jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true });
$(myGrid).fluidGrid({ base: '#tableBox', offset: -20 });

function Invoke(action, pdata) {

    var request = new Object();
    if (pdata.filters == undefined && pdata._search == false)
        request.Action = "Sort";
    else {
        if (pdata.filters != undefined && pdata._search == false)
            request.Action = action;
        else request.Action = "Filter";
    }
    if (pdata) {
        request.SortIndex = pdata.sidx;
        request.SortOrder = pdata.sord;
        request.PageNumber = pdata.page;
        request.PageSize = pdata.rows;
        request._search = pdata._search;
        request.filters = pdata.filters;
    }

    var cRequest = new Object();
    cRequest.request = request;

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: 'WebService.asmx/Get',
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        dataType: "json",
        data: JSON.stringify(cRequest),
        success: function (xhrResponse, textStatus) {
            var data = xhrResponse.d;
            var table = $('#ViewNQueryTable');
            table.clearGridData();
            //table.total = data.total;

            for (var i = 0; i < data.Result.length; i++) {
                table.addRowData(i + 1, data.Result[i], 'last');
            }
            $('#totalRecordsFound').html(data.records + " Customers");
            pdata.filters = undefined;
            pdata._search = false;
        },
        error: function (data, textStatus) {
            alert("Error fetching data");
        }
    });
}

我尝试添加这个:

onPaging: function (which_button) {
        doSomething()
    } 

但它没有帮助。

如何让分页按钮调用服务器方法?

修改

我的网络方法是:

[WebMethod]
public kResponse Get(kRequest request)
{
    if (count == 0)
    {
        CurrentList = JsonHelper.GetPersons();
        count++;
    }

    var response = new kResponse();

    switch (request.Action)
    {
        case "GetAll":
            var result = new List<Person>();
            var list = JsonHelper.GetPersons();
            CurrentList = list;
            response.records = CurrentList.Count;
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            for (int i = 0; i < 10; i++)
            {
                result.Add(list[i]);    
            }

            //response.Result = result;
            response.Result = list;

            break;
        case "Filter":
            var filterParams = Filter.Create(request.filters);
            List<Person> FilterdList = GetFilteredList(filterParams);
            CurrentList = FilterdList;
            response.Result = CurrentList;
            response.records = CurrentList.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            break;

        case "Sort":
            //var listPersons = JsonHelper.GetPersons();
            IQueryable<Person> SortedList = ApplySort(CurrentList.AsQueryable(), request.SortIndex, request.SortOrder);
            CurrentList = SortedList.ToList();
            response.Result = CurrentList;
            response.records = CurrentList.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber;
            break;
        case "NextPage":
            List<Person> allList = JsonHelper.GetPersons();
            IQueryable<Person> NextPagelist = allList.AsQueryable();
            NextPagelist = NextPagelist.Skip(request.PageNumber * request.PageSize).Take(request.PageSize).AsQueryable();
            response.Result = NextPagelist;
            response.records = NextPagelist.Count();
            response.total = response.records / request.PageSize;
            //response.total = list.Count;
            response.page = request.PageNumber++;


            break;
    }
    return response;
}

与班级:

public class kRequest

public string Action { get; set; }
public int PageSize { get; set; }
public int PageNumber { get; set; }
public string SortIndex { get; set; }
public string SortOrder { get; set; }
public string Search { get; set; }
public bool _search { get; set; }

public string filters { get; set; }

public class kResponse

//public int Total { get; set; }
public object Result { get; set; }
public int page { get; set; }
public int total { get; set; }
public int records { get; set; }
//public GridRow[] rows { get; set; }

我正在尝试这个寻呼机:

$('#next_ViewNQueryPager').click(function () {

    grid.Action = "NextPage";
    triggerReloadGrid();

});

但我相信这不是那种方式......是什么?

1 个答案:

答案 0 :(得分:2)

我认为不需要额外的Action参数。问题可能来自于误解了jqGrid在每个标准请求中从服务器请求的数据。问题是,排序,分页和过滤应该始终分析并由Web服务器应用,所有“操作”可以组合在一起

例如,您在网格中显示数据库中包含两列的数据:产品名称和每个产品的单价。让我们按产品名称对网格进行排序,作为初始jqGrid选项(sortname'ProductName'),并使用rowNum的默认值:20。初始网格填充后的用户集过滤器,以获得价格低于100美元的产品。如果_search参数设置为truefilters将设置为以JSON格式编码过滤器的字符串(请参阅the documentation)。因此,将使用参数_search = truefilters = some valuepage = 1rows = 20sidx ='ProductName'sord = 'asc'调用您的网络服务的网络方法。让我们得到的清单将是55个产品。因此,您将有3页结果,并且Web服务必须返回与输入参数page = 1对应的第一页。然后,用户可以手动键入3作为页码,然后按 Enter 或用户可以单击“价格”列的标题按价格对产品进行排序。无论如何,网络方法将接收新请求,其中包含pagerowssidxsord_search参数的新值以及{{} { 1}}参数。

因此,服务器应始终测试filters是否为_search。在服务器应该将过滤器应用于获取所有产品的原始SQL查询的情况下。然后结果必须始终排序对应于truesidx参数的值。最后,Web服务器应计算过滤结果行的总数,并将其作为sord输出参数的值返回。以同样的方式,Web服务应计算过滤页面的总数并将其返回到records输出参数中。最后,Web服务应根据输入参数totalrows的值返回最多page条记录(一页)。

我试着解释说你真的不需要定义“GetAll”,“Sort”,“Filter”,“NextPage”等动作,而web服务应该总是考虑所有输入参数< / em>的

顺便说一下,如果你想要重命名和输入参数,你可以使用rows(参见here)。在the old answer中,我解释了如何使用prmNames代替datatype: 'json'直接实现对Web服务的调用。