1

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"?>\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>

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.

Community
  • 1
  • 1
user590586
  • 2,960
  • 16
  • 63
  • 96

1 Answers1

1

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg: I don't really think i understood you.. when is the unformatter triggred? and in my case - what should i write in the unformatter function in order to get the input checkbox value? thank's for your help. – user590586 Jan 26 '11 at 13:39
  • @user590586: Every time when the value from jqGrid should be read the corresponding standard or your custom unformat function will be called. It you use for example the standard checkbox formatter and have for example the option `editoptions:{{value:"1:0"}}` additionally, then "1" or "0" will be read as the values from the corresponding grid cell. If you don't use `editoptions.value`, the values will be "Yes" or "No" per default (see https://github.com/tonytomov/jqGrid/blob/master/js/jquery.fmatter.js#L496). In the unformatter function you should get from `cellobject` the string value. – Oleg Jan 26 '11 at 14:00
  • @Oleg: I added the unformat function to my column , but still nothing happened when the xml is generated. {name:'test1',xmlmap:'test1',edittype:"checkbox", formatter: checkboxFormatter ,unformat:myunformatfunc} what should i do? i think i'm missing something.. waiting for your reply, thank's!! – user590586 Jan 26 '11 at 14:33
  • @user590586: I could try to help you if you post the url with you jqGrid or a full code which can be used to reproduce your problem. – Oleg Jan 26 '11 at 14:39
  • @user590586: You can for example append the text of your question with HTML and JavaScript. If you get data from the server, then it would be enough to post the corresponding XML/JSON response from the server. In the way one could full reproduce the problem without having any server components. – Oleg Jan 26 '11 at 14:45
  • @Oleg: It's to much code to write in the comment so i will write an answer with the code? – user590586 Jan 26 '11 at 15:05
  • @Oleg: I edited the question above with my current code. wating for your response. – user590586 Jan 26 '11 at 15:36
  • @user590586: To be able to reproduce your problem I need the XML data returned by 'loadgrid.jsp?type=1'. You can use Fiddler (see http://www.fiddler2.com/fiddler2/) or Firebug (see http://getfirebug.com/) to see the server response. Moreover from the code posted it is not clear how the `MyExportToXml` function will be called. – Oleg Jan 26 '11 at 15:48
  • @Oleg: this is what inside 'loadgrid.jsp?type=1': 1 1 1 0 1> 2 <![CDATA[aaddddaaaa]]> – user590586 Jan 26 '11 at 16:08
  • and the MyExportToXml function is called from an input type button when clicked. – user590586 Jan 26 '11 at 16:09
  • @user590586: I updated my answer. See also the demo http://www.ok-soft-gmbh.com/jqGrid/ExportToXmlCustomCheckboxes.htm – Oleg Jan 26 '11 at 17:18
  • @Oleg: Is it possible to get only the selected row data of the grid when calling 'MyExportToXml' ? how can i get the grid's row data for the selected row? thank's again for your help. – user590586 Feb 01 '11 at 16:52
  • I was having issues with getting my custom checkbox formatter to work - it was the unformat method i was missing that was stopping the value being applied to the datasource – WraithNath Jan 10 '12 at 10:01
  • @WraithNath: Could you describe the problem more detailed? Which formatter and unformatter you use? What is the problem exactly? What is stopping to work? Why you need use custom formatter at all? Why the standard 'checkbox' formatter can't be used in your case? – Oleg Jan 10 '12 at 10:06
  • @Oleg - sorry, I was just commenting to say your post helped, I was missing the unformat function which was my issue, was just commenting as i Up voted :) thanks – WraithNath Jan 10 '12 at 18:11
  • @WraithNath: You are welcome! I asked you for the reason of the usage of custom formatter because in some cases, if one need to set some attributes like `style`, `title` or set `disabled="disabled"` based on some additional rules, one needs really needed to use custom formatter. Instead of that one can use `cellattr`. See [here](http://stackoverflow.com/a/8774139/315935) or [here](http://stackoverflow.com/a/8602761/315935) the corresponding examples. – Oleg Jan 10 '12 at 18:33
  • @Oleg - thanks again, I have just tried to apply what I got working with a checkbox column to a textbox column and I dont seem to be able to get it working in the same way. I wonder If you could point me in the right direction on this one!? http://stackoverflow.com/questions/8810151/asp-net-jqgrid-textbox-column-with-custom-formatter-not-firing-code-behind – WraithNath Jan 10 '12 at 20:43
  • @WraithNath: Sorry, but today and tomorrow I am busy. I'll try to help your in 2 days if till the time somebody else not solve your problem. – Oleg Jan 10 '12 at 21:10
  • @Oleg - thanks, ill continue to look at it in the mean time, I may have to settle for using the standard click and edit functionality until I can get some pointers. Most appreciated. – WraithNath Jan 10 '12 at 21:16