Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

jqgrid generate xml from grid data problem

I have a question regarding the question: enter link description here I added 'datatype:"local"' to my grid and it worked , i got the xml , but it did include inside of it the checkbox column i have in the grid. this is my code:

  <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"?>
<rows>
' +
                          xmlJsonClass.json2xml (dataFromGrid, '') + '</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>

how can i include the checkbox values inside the created xml? and if it isn't possible, Is there another way to get xml from my data inside the grid?

Thank's in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suppose that your problem will be solved is you include additionally custom unformatter to the custom formatter checkboxFormatter which you currently use.

Without having unformatter jqGrid can not read any value from the grid. Look at the source code of jqGrid which implement unformatter for the standard chackbox type.

UPDATED: The data will be successful exported by your code. The problem which you probably have is another. You include the custom formatter which has enabled checkboxes instead of disabled chechboxes created by predefined checkbox formatter. In the case if the user change the state of some checkbox you have to update the data parameter of jqGrid manually. Because you not do this, the data parameter will be corresponds to the initial values of the checkboxes.

To fix the problem you should 1) fix the code of your unformatter myunformatfunc to the following

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

2) You should use jQuery("#confirm_payment").jqGrid('getRowData') method (which will use your custom unformatter) instead of jQuery("#confirm_payment").jqGrid('getGridParam','data'). Disadvantage of the method is that it read the data only the current page, but because you don't use local data paging it is not problem for you.

On the demo you can check/uncheck some checkboxes and cick the "Export Data to XML" button. Two different version of XML will be displayed: one with respect of 'getRowData' and another with respect of getGridParam('data'). How you can see, the 'getRowData' way gives actual values of the checkboxes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...