在列上使用editoptions时,如何在jqGrid中获取select的值

在列上使用editoptions时,如何在jqGrid中获取select的值,select,jqgrid,Select,Jqgrid,我在jqGrid中有两列edittype=select。如何读取特定行中当前选定值的选项值 e、 g:当我提供以下选项时,如何为联邦快递等获得FE editoption: { value: “FE:FedEx; IN:InTime; TN:TNT” } rowId/cellname的getRowData仅返回select的文本/显示组件 如果我在列上设置了一个change data事件,则底层仅在鼠标单击时触发change事件,而不是在键盘选择时触发change事件。有许多引用涉及通用选择和

我在jqGrid中有两列edittype=select。如何读取特定行中当前选定值的选项值

e、 g:当我提供以下选项时,如何为联邦快递等获得FE

editoption: { value: “FE:FedEx; IN:InTime; TN:TNT” } 
rowId/cellname的getRowData仅返回select的文本/显示组件

如果我在列上设置了一个change data事件,则底层仅在鼠标单击时触发change事件,而不是在键盘选择时触发change事件。有许多引用涉及通用选择和鼠标/键盘问题

底线是,选择新值时,我需要在更改时以及在发布到服务器之前知道选项值。

getRowData的状态:

编辑行或单元格时不要使用此方法。这将返回单元格内容,而不是输入元素的实际值

调用getRowData时是否仍在编辑该行

更新


同意,jqGrid处理得不太好。在我的应用程序中,我实际上能够通过不指定编辑选项来解决这个问题,这意味着,键/值都是联邦快递;然后在服务器上完成对ID的转换。这不是编写代码的正确方法,但它对我当时的需要来说已经足够好了。

我刚刚通过设置JqGrid unformat选项解决了这个问题,并使用以下函数对单元格值进行了unformat

function Unformat_Select(cellvalue, options, cellobject)
{
    var unformatValue = '';

    $.each(options.colModel.editoptions.value, function (k, value)
    {
        if (cellvalue == value)
        {
            unformatValue = k;
        }
    });

    return unformatValue;
}
每当网格需要单元格数据时(如调用getRowData方法时),都会调用上述方法。但是,我的函数仅支持键对值编辑选项。您需要按照以下模式更改数据

editoption: 
{
    value:
    {
        FE:'FedEx', 
        IN:'InTime', 
        TN:'TNT'
    }
}
有关取消格式化选项的更多信息,请参见以下链接

另外,可以修改我的函数以支持客户端dropdownlist值。但我认为不可能将此函数应用于服务器端dropdownlist值

更新

在最新的jqGrid 3.8.1中,当用户取消编辑行或以编程方式调用restoreRow方法时,我发现了一些错误,jqGrid将使用数据键而不是数据值创建标签。我创建以下函数来解决此问题。要使用它,必须将其作为此列的自定义格式化程序函数。此函数通过比较键或值将单元格值映射到列表的值

function JqGridInlineEditor_SelectFormatter(cellvalue, options, rowObject)
{
    var temp = '';
    $.each(options.colModel.editoptions.value, function (key, value)
    {
        if (cellvalue == key || cellvalue == value)
        {
            temp = value;
            return false;
        }
    });

    return temp;
}
因此,您可以将键或值作为列数据发送,以由上述自定义格式设置程序呈现。

您必须将列的格式设置为“选择”

wiki中的示例:

colModel:[{name:'myname', edittype:'select',格式化程序:'select', 编辑选项:{值:1:1;2:2}}。。。 ]

在这里看到更多


我也遇到了同样的问题,如果您有这样的需求,每一行都有下拉列表,它的值如下

FE:‘联邦快递’, 在:'时间', TN:‘TNT’

现在,不再在下拉更改事件中将数据保存到后端;您希望在行级别的“保存”按钮上保存数据,但希望在选定值TN上保存DropdWn而不是显示文本tnt。您可以创建另一个隐藏字段,以便在内联编辑下拉列表时设置选定的国家/地区代码。在这里,当用户在单元格内联编辑后退出时,将调用afterSaveCell方法,您可以按照以下代码中所述进行设置:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>http://stackoverflow.com/q/9655426/315935</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/redmond/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/css/ui.jqgrid.css" />
    <style type="text/css">
        html, body { font-size: 75%; }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
    <script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/js/i18n/grid.locale-en.js"></script>
    <script type="text/javascript">
        $.jgrid.no_legacy_api = true;
        $.jgrid.useJSON = true;
    </script>
    <script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/js/jquery.jqGrid.src.js"></script>
    <script type="text/javascript">
    //<![CDATA[
        /*global $ */
        /*jslint devel: true, browser: true, plusplus: true, nomen: true, unparam: true */
        $(document).ready(function () {
            'use strict';
            var listOptions = "CN:Canada; US:United States; FR:France; IN:India";
                var mydata = [{
                    name: "Toronto",
                    country: "CN",
                    continent: "North America",
                    countrycode: "CN"
                }, {
                    name: "New York City",
                    country: "US",
                    continent: "North America",
                    countrycode: "US"
                }, {
                    name: "Silicon Valley",
                    country: "US",
                    continent: "North America",
                    countrycode: "US"
                }, {
                    name: "Paris",
                    country: "FR",
                    continent: "Europe",
                    countrycode: "FR"
                }, {
                    name: "Pune",
                    country: "IN",
                    continent: "Asia",
                    countrycode: "IN"
                }]

                $("#gvManageMapping").jqGrid({
                    data: mydata,
                    datatype: "local",
                    colNames: ["Name", "Country", "Continent", "countrycode"],
                    colModel: [{
                        name: 'name',
                        index: 'name',
                        editable: false,
                    },
                    {
                        name: 'country',
                        index: 'country',
                        editable: true, edittype: "select", formatter: 'select', editoptions: {
                            value: listOptions,
                        }, editrules: { required: true, integer: false }, formatter: "select"
                    },
                    {
                        name: 'continent',
                        index: 'continent',
                        editable: false,
                    },
                    {
                        name: 'countrycode',
                        index: 'countrycode',
                        editable: false
                    }],
                    afterSaveCell: function (rowid, cellname, value) {
                            var selectedCountryCode, $this;
                            if (cellname === 'country') {
                                $this = $(this);
                                selectedCountryCode = $this.jqGrid("getCell", rowid, 'country');
                                $this.jqGrid("setCell", rowid, 'countrycode', selectedCountryCode);
                            }
                        },
                    pager: '#pager',
                    'cellEdit': true,
                    'cellsubmit': 'clientArray',
                    editurl: 'clientArray'
                });
        });
    //]]>
    </script>
</head>
<body>
    <table id="gvManageMapping"><tr><td /></tr></table>
    <div id="pager"></div>
</body>
</html>

否。对getRowaData的调用是在编辑之后进行的,例如,将行数据填充到json对象中以发送到服务器时。只是文档中绝对没有提到获取选择的基本选项值,除了绑定到更改事件,这在一般情况下是不可靠的。是的,我也同意。我想我将恢复到将显示的值(即注册值)映射回密钥服务器端。缺点是值必须是唯一的。顺便说一句,保存CellRowId、cellname、value、iRow、iCol后的事件确实会返回in值。很高兴知道。那么,您最终使用了这种方法吗?可能重复的是,我遇到了完全相同的问题,并在我的colModel中添加了格式化程序:“select”,这也解决了这个问题。