将jqGrid rowNum从ALL更改为-1的最佳方法,然后再传递给Web服务 [英] Best way to change jqGrid rowNum from ALL to -1 before pass to a web service

查看:109
本文介绍了将jqGrid rowNum从ALL更改为-1的最佳方法,然后再传递给Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找允许用户选择在jqGrid中显示所有记录的最佳方法.我知道为rows参数传递的-1值表示ALL,但是我希望单词"ALL"而不是-1出现在rowList select元素中,即. rowList:[15、50、100,'ALL'].

I'm looking to find the best way to allow users to choose to show ALL records in a jqGrid. I know that a -1 value passed for the rows parameter denotes ALL, but I want the word "ALL" not a -1 to appear in the rowList select element, ie. rowList: [15, 50, 100, 'ALL'].

我正在将网格请求传递到接受行"的int的Web服务,并且试图找到如何和何时将用户选择的"ALL"值更改为-1,然后再获取发送到Web服务.

I'm passing the grid request to a web service which accepts an int for "rows", and I'm trying find how and when I should change the user selected value of "ALL" to a -1 before it gets sent to the web service.

下面是我清理的网格代码.我在$ .ajax之前的数据类型函数中尝试了一些各种代码块.但是,大多数尝试似乎都使我不得不尽我所能地进行最复杂的操作.例如,

Below is my cleaned up grid code. I tried some various code blocks before my $.ajax in the datatype function. But most attempts just seemed like I have to be doing this the most convoluted way I possibly could. For example,

datatype: function(postdata) {
   if ($("#gridTableAssets").jqGrid('getGridParam', 'rowNum') == 'ALL') {
      $("#gridTableAssets").appendPostData({ "rows": -1, "page": 1 });
   }
   $.ajax({...

但是这样做似乎会使实际的页面" GridParam在随后的网格操作中无效,从而迫使我在其他地方进行处理.似乎似乎这是经常要在外面做的事情,并且有干净的做事方法.

But doing that seemed to cause the actual "page" GridParam to be nulled out on subsequent grid actions, forcing me handle that in other places. There just seems like this is something that would be frequently done out there and have clean way of doing it.

清洁的网格代码:

$("#gridTableAssets").jqGrid({
    datatype: function(postdata) {
       $.ajax({
           url: "/Service/Repository.asmx/GetAssets",
           data: JSON.stringify(postdata),
           type: 'POST',
           contentType: "application/json; charset=utf-8",
           error: function(XMLHttpRequest, textStatus, errorThrown) {
              alert('error');
           },
           success: function(msg) {
              var assetsGrid = $("#gridTableAssets")[0];
              assetsGrid.addJSONData(JSON.parse(msg));
              ...
           }
       });
    },
    ...
    pager: $('#pagerAssets'),
    rowNum: 15,
    rowList: [15, 50, 100, 'ALL'],
    ...                    
    onPaging: function(index, colindex, sortorder) {
       SessionKeepAlive();
    }
});

这是Web服务

[WebMethod]
public string GetAssetsOfAssetStructure(bool _search, int rows, int page, 
    string sidx, string sord, string filters)

推荐答案

首先,我发现您的问题很好.当我从jqGrid开始时,我正在rowList中搜索"All"并进行了一些实验,但没有成功.然后我忘记了这个问题.现在,在您提出问题之后,我考虑在所有jqGrids中使用此功能.

First of all I find your question very good. As I started with jqGrid I was searching for "All" in the rowList and made some experiments, but without any success. Then I forgot about this problem. Now after your question I think about using this feature in all my jqGrids.

现在介绍解决方案.这很容易,但是在此之前,我建议将datatype替换为datatype: 'json'的功能.您可以使用标准的"json"类型在函数内部执行的所有操作.要将contentType更改为"application/json; charset=utf-8",可以使用ajaxGridOptions选项(请参阅如何不使用内置搜索/过滤器过滤jqGrid数据框).最后,我们收到如下信息:

Now about the solution. It is very easy, but befor all I suggest to replace datatype as function to the datatype: 'json'. All what you do inside of the function you can do with standard 'json' type. To change contentType to "application/json; charset=utf-8" one can use ajaxGridOptions option (see Setting the content-type of requests performed by jQuery jqGrid for details). One can use postData parameter as a replacement of data parameter of jQuery.ajax, but you will need it only if you want to send some additional parameters to server (like described in How to filter the jqGrid data NOT using the built in search/filter box). At the end we receive something like following:

$("#gridTableAssets").jqGrid({
    datatype: 'json',
    gridview: true,
    url: '/Service/Repository.asmx/GetAssets',
    //postData: postdata, // add some additional parameters
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    mtype: 'POST',
    // ...
    pager: $('#pagerAssets'),
    rowNum: 15,
    rowList: [15, 50, 100, 'ALL'],
    serializeGridData: function (postData) {
        if (typeof postData.rows === "string") {  // "ALL"
            postData.rows = 10000;  // or -1 if your server 
        }
        if (isNaN(postData.page)) { // fix NaN in page for rows="ALL"
            postData.page = 1;
        }
        return JSON.stringify(postData);
    },
    // ...                    
});

如果您打算在所有jqGrid中使用serializeGridDataajaxGridOptions和其他一些参数,则可以在$.jgrid.defaults内部定义此功能(请参阅另一时间

If you plan to use serializeGridData, ajaxGridOptions and some other parameters in all your jqGrids you can define this function inside of $.jgrid.defaults (see one more time Setting the content-type of requests performed by jQuery jqGrid).

这篇关于将jqGrid rowNum从ALL更改为-1的最佳方法,然后再传递给Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆