JQGrid动态选择数据

问题描述 投票:0回答:2

我已经使用了此链接的示例代码

中的示例代码

我的网格可以在添加和编辑时显示动态构建的选择下拉列表。但是,当它仅显示网格中的数据时,它会显示下拉索引而不是关联的数据。有没有办法让网格显示与索引关联的数据而不是索引本身。

例如我选择的数据可能是“0:Hello;1:World”;编辑/添加窗口上的下拉菜单显示“Hello”和“World”,并具有它们的正确索引。如果单元格的值为 1,我希望它在网格本身中显示 World,但它显示的是 1。

这是我的网格中的行本身:

{ name: 'picklist', index: 'picklist', width: 80, sortable: true, editable: true,
  edittype: "select", formatter: "select", editrules: { required: true} },

我在loadComplete事件中填写动态数据内容如下:

$('#mygrid').setColProp('picklist', { editoptions: { value: picklistdata} });

picklist 数据是一串“0:Hello;1:World”类型的值对。

请任何人提供任何帮助。我对 JQGrids 还很陌生,所以请您也提供一些示例。

select dynamic jqgrid population
2个回答
1
投票

我知道你已经解决了这个问题,但我在我的项目中遇到了同样的问题,并想提供我的解决方案。

首先,我为我的选择列(在本例中为“用户名”列)声明一个自定义格式化程序。

$.extend($.fn.fmatter, {
    selectuser: function(cellvalue, options, rowdata) {
        var userdata;
        $.ajax({
            url:'dropdowns/json/user',
            async:false,
            dataType:'json',
            cache:true,
            success: function(data) {
                userdata = data;
            }
        });
        return typeof cellvalue != 'undefined' ? userdata[cellvalue] : cellvalue ;
    }
});

在本例中,此格式化程序加载 id 和用户的映射,并返回特定单元格值的用户名。然后,我将

formatter:'selectuser'
选项设置为列的
colModel
,它就起作用了。

当然,这会对网格中显示的每一行执行一个 json 请求。我通过为 json 响应的标头设置 10 秒的 caching 解决了这个问题,如下所示:

private function set_caching($seconds_to_cache = 10) {
    $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
    header("Expires: $ts");
    header("Pragma: cache");
    header("Cache-Control: max-age=$seconds_to_cache");
}

我知道这个解决方案并不完美,但它足以满足我的应用程序。浏览器会立即提供缓存命中服务,网格流动顺畅。最终,我希望内置的

select
格式化程序能够修复以处理 json 数据。


0
投票

如果您保存在

select
元素的 jqGrid id 中并希望显示相应的文本,那么您应该在
formatter:'select'
中使用
colModel
(请参阅 http://www.trirand.com/jqgridwiki/doku.php ?id=wiki:predefined_formatter#formatter_type_select) 与
edittype: "select"

如果您计划支持数据搜索,

stype: 'select'
的用法也可能对您感兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.