简体   繁体   English

JQGrid设置标题和列名

[英]JQGrid set caption and column name

I have a JQGrid whith 2 columns where I will got to the server and get some data, then I will concatenate some strings based on the filters on the server and want to set this as caption and also wants to change the columns names based on those filters. 我有一个JQGrid,有两列,我将到达服务器并获取一些数据,然后我将基于服务器上的过滤器连接一些字符串,并希望将其设置为标题,并且还希望根据这些更改列名称过滤器。 Is there a way to set the caption and columns names based on the ActionResult from the server ? 有没有办法根据服务器的ActionResult设置标题和列名?

I find your question interesting. 我觉得你的问题很有意思。

We can start with the simple grid: 我们可以从简单的网格开始:

$("#list").jqGrid({
    url: 'ColumnNamesAndTitelFromServer.json',
    datatype: 'json',
    loadonce: true,
    colNames: ['Name', 'Email'],
    colModel: [
        {name: 'name', width: 100},
        {name: 'email', width: 150}
    ],
    rowNum: 5,
    rowList: [5, 10, 20],
    pager: '#pager',
    gridview: true,
    rownumbers: true,
    sortname: 'name',
    sortorder: 'asc',
    caption: 'Just simple local grid',
    height: 'auto'
});

and the JSON data: 和JSON数据:

{
    "total": 1,
    "page": 1,
    "records": 2,
    "rows": [
        {"id": "id1", "cell": ["John",    "john@example.com"]},
        {"id": "id2", "cell": ["Michael", "michael@example.com"]}
    ]
}

We will receive the following results 我们将收到以下结果

在此输入图像描述

(see the demo ) (见演示

Now we extend the JSON data with our custom additional information: 现在我们使用我们的自定义附加信息扩展JSON数据:

{
    "total": 1,
    "page": 1,
    "records": 2,
    "rows": [
        {"id": "id1", "cell": ["John",    "john@example.com"]},
        {"id": "id2", "cell": ["Michael", "michael@example.com"]}
    ],
    "userdata": {
        "title": "Das ist der Titel bestimmt beim Server",
        "columnNames": {
            "name": "Die Name",
            "email": "Die E-Mail"
        }
    }
}

In the example above I just define in userdata the title and the column names of the grid in German language. 在上面的例子中,我只是在userdata定义了德语网格的标题和列名。 To read and to use the userdata we can add the following loadComplete event handler to the grid: 要读取和使用userdata我们可以将以下loadComplete事件处理程序添加到网格中:

loadComplete: function () {
    var $grid = $(this), columnNames, name,
        userdata = $grid.jqGrid('getGridParam', 'userData');

    if (userdata) {
        if (userdata.title) {
            $grid.jqGrid('setCaption', userdata.title);
        }
        if (userdata.columnNames) {
            columnNames = userdata.columnNames;
            for (name in columnNames) {
                if (columnNames.hasOwnProperty(name)) {
                    $grid.jqGrid('setLabel', name, columnNames[name]);
                }
            }
        }
    }
}

Now the same grid will looks like 现在相同的网格看起来像

在此输入图像描述

(see another demo ) (见另一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM