【问题标题】:jqgrid generate xml from grid data problemjqgrid 从网格数据生成 xml 问题
【发布时间】:2011-01-26 12:48:57
【问题描述】:

我对此问题有疑问:enter link description here 我在网格中添加了 'datatype:"local"' 并且它有效,我得到了 xml,但它确实在其中包含了我在网格中的复选框列。这是我的代码:

  <script type="text/javascript" >

        var MyExportToXml = function (grid)
        {
            var dataFromGrid = {row: grid.jqGrid ('getGridParam', 'data') };
            var xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>\n<rows>\n' +
                          xmlJsonClass.json2xml (dataFromGrid, '\t') + '</rows>';
            alert(xmldata);
        };                                   

        function checkboxFormatter(cellvalue, options, rowObject)
        {
            var _checkbox_name = options.colModel.name;
            var _checkbox_name_id = _checkbox_name + options.rowId;

            cellvalue = cellvalue + "";
            cellvalue = cellvalue.toLowerCase();
            var bchk = cellvalue.search(/(false|0|no|off|n)/i) < 0 ? " checked " : " ";

            return "<input type='checkbox' id='"+_checkbox_name_id+"' onclick=\"\" " + bchk + " value='" + cellvalue + "' offval='no' />"
        }

        function myunformatfunc(cellvalue, options, rowObject)
        {
            alert(cellvalue);
            return cellvalue;
        }

            jQuery("#confirm_payment").jqGrid({
                url:'loadgrid.jsp?type=1',
                datatype: "xml",
                loadonce:true ,
                direction:"rtl",
                height: '100%',
                width: '100%',
                colNames:['test1' , 'test2' , 'test3' , 'test4'],
                colModel:[
                    {name:'test1',xmlmap:'test1', width:18,align:"center",sortable:false ,edittype:"checkbox", formatter: checkboxFormatter ,unformat:myunformatfunc},
                    {name:'test2',xmlmap:'test2', width:18,align:"center",sortable:false ,edittype:"checkbox", formatter: checkboxFormatter ,unformat:myunformatfunc},
                    {name:'test3',xmlmap:'test3', width:80, align:"right",sorttype:"int"},
                    {name:'test4',xmlmap:'test4', width:70, align:"right",sorttype:"int"},
                ],
                xmlReader: {
                      root:"payments",
                      row:"payment",
                      page:"payments>page",
                      total:"payments>total",
                      records:"payments>records",
                      repeatitems:false
                  },
                multiselect: false,
                autowidth: true,
                forceFit: false,
                shrinkToFit: false
            });           
    </script>

如何在创建的 xml 中包含复选框值? 如果不可能,是否有另一种方法可以从网格内的数据中获取 xml?

提前致谢。

【问题讨论】:

    标签: jqgrid


    【解决方案1】:

    我想你的问题将得到解决,如果你在当前使用的custom formatter checkboxFormatter 中添加了custom unformatter

    如果没有 unformatter,jqGrid 无法从网格中读取任何值。查看the source code of jqGrid,它为标准的 chackbox 类型实现了 unformatter。

    更新:您的代码将成功导出数据。您可能遇到的问题是另一个问题。您包括具有 启用 复选框的自定义格式化程序,而不是由预定义的复选框格式化程序创建的禁用 chechboxes。如果用户更改某些复选框的状态您必须手动更新 jqGrid 的data 参数。因为您不这样做,所以data 参数将对应于复选框的初始值。

    要解决此问题,您应该 1) 将您的 unformatter myunformatfunc 的代码修复为以下内容

    function myunformatfunc(cellvalue, options, rowObject)
    {
        return $('input',rowObject).attr("checked") ? "1" : "0";
    }
    

    2) 您应该使用jQuery("#confirm_payment").jqGrid('getRowData') 方法(它将使用您的自定义解格式化程序)而不是jQuery("#confirm_payment").jqGrid('getGridParam','data')。该方法的缺点是它只读取当前页面的数据,但是由于您不使用本地数据分页,所以对您来说没有问题。

    the demo 上,您可以选中/取消选中某些复选框并单击“将数据导出为 XML”按钮。将显示两种不同版本的 XML:一种与“getRowData”有关,另一种与getGridParam('data') 有关。你怎么看,'getRowData'方式给出了复选框的实际值。

    【讨论】:

    • @Oleg:我真的不认为我理解你.. unformatter 什么时候触发?在我的情况下 - 我应该在 unformatter 函数中写什么以获得输入复选框值?谢谢你的帮助。
    • @user590586: 每次来自 jqGrid 的值应该被读取相应的标准或者你的自定义 unformat 函数将被调用。例如,如果您使用标准复选框格式化程序并具有例如选项editoptions:{{value:"1:0"}},则“1”或“0”将被读取为相应网格单元格中的值。如果您不使用editoptions.value,则默认值将是“是”或“否”(请参阅​​github.com/tonytomov/jqGrid/blob/master/js/…)。在 unformatter 函数中,您应该从 cellobject 获取字符串值。
    • @Oleg:我将 unformat 函数添加到我的列中,但生成 xml 时仍然没有发生任何事情。 {name:'test1',xmlmap:'test1',edittype:"checkbox", formatter: checkboxFormatter ,unformat:myunformatfunc} 我该怎么办?我想我错过了一些东西.. 等待您的回复,谢谢!
    • @user590586:如果您将网址与 jqGrid 或可用于重现您的问题的完整代码一起发布,我可以尝试帮助您。
    • @user590586:例如,您可以使用 HTML 和 JavaScript 附加问题文本。如果您从服务器获取数据,那么从服务器发布相应的 XML/JSON 响应就足够了。这样一来,无需任何服务器组件即可完全重现该问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多