无论如何在jqgrid中有多个列,为下拉列表读取相同的源数据?

2022-01-19 00:00:00 jquery ajax jqgrid

我有一个案例,我在 jqGrid 中有多个列,它们来自同一个列表来填充下拉列表.

I have a case where i have multiple columns in jqGrid that source the same list to populate the dropdown.

   { name: "Manager", index: "Manager", width: 120, editable: true, edittype: "select", editoptions: { dataUrl: "/Person/GetSelectData" }, editrules: { required: false} },

  { name: "Delegate", index: "Delegate", width: 120, editable: true, edittype: "select", editoptions: { dataUrl: "/Person/GetSelectData" }, editrules: { required: false} },

我想看看是否有办法让 tihs 在上面工作,而无需两个单独的 ajax 调用来获取相同的数据列表:

I wanted to see if there is a way to have tihs work above without two seperate ajax calls to the same action just to get the same list of data:

   dataUrl: "/Person/GetSelectData"

所以我可以调用一次并将项目​​列表链接到两列?在 jqGrid 中可以吗?

so I can call it once and have the list of items linked to both columns? Is that possible in jqGrid?

推荐答案

任何你想要的实现都意味着对 "/Person/GetSelectData" 的数据进行某种缓存.我更喜欢自己的一种方法是使用 value 而不是 dataUrl.选择值列表可以包含在对填充网格的服务器的主要响应中.如果 url 中使用的操作可以返回附加数据.您可以在定义为函数的 value 内使用返回的数据,也可以在 beforeProcessing 内设置 value.为了让我的建议更清楚,我用一个例子来解释它.

Any implementation of what you want will mean some kind of caching of the data for "/Person/GetSelectData". One way which I would prefer myself is the usage of value instead of dataUrl. The list of select values can be included in the main response to the server which fill the grid. In the case the action used in url can returns additional data. You can use the returned data inside of value defined as a function or you can set the value inside of beforeProcessing alternatively. To make my suggestion more clear I explain it on an example.

第一种方式:使用value作为函数.可以在主 JSON 响应中包含您通常在 "/Person/GetSelectData" 中返回的数据.例如,您可以使用 userdata(或输入数据的任何其他扩展):

The first way: usage value as function. One can include the data which you returns typically in "/Person/GetSelectData" inside of main JSON response. For example you can use userdata (or any other extensions of the input data):

{
    "rows": [
        ...
    ],
    "userdata": {
        "Persons": "Bill:Bill;Oleg:Oleg;Leora:Leora"
    }
}

然后可以使用

beforeProcessing: function (data) {
    var $self = $(this), userData = data.userdata, persons, selectOptions;
    if (userData && userData.Persons) {
        persons = userData.Persons;
        selectOptions = {
            searchoptions: { value: ":All;" + persons }, // for toolbar search
            stype: "select",
            editoptions: { value: persons },
            edittype: "select"
        };
        $self.jqGrid("setColProp", "Manager", selectOptions);
        $self.jqGrid("setColProp", "Delegate", selectOptions);
    }
}

顺便说一句,甚至可以将 formatter: "select" 用于Manager"和Delegate"列.它允许使用 id 而不是名称.例如

By the way one can even use formatter: "select" for "Manager" and "Delegate" columns. It allows to use ids instead of names. For example

"Persons": "3:Bill;1:Oleg;2:Leora"

也应该将 formatter: "select" 添加到 selectOptions 中.它允许在主数据(JSON 数据的 rows 部分)中使用 ID 312.使用 dataUrl 的标准方式不允许使用 formatter: "select".

One should add formatter: "select" to selectOptions too. It allows to use ids 3, 1 and 2 inside of the main data (rows part of JSON data). The standard way with the usage of dataUrl don't allow to use formatter: "select".

我建议你阅读答案,这个和这个了解更多关于使用beforeProcessing 用于动态修改网格.

I recommend you to read the answer, this one and this one for more information about usage beforeProcessing for dynamic modification of the grid.

相关文章