Forum

July 10th, 2011
You must be logged in to post Login Register

Search Forums:


 






problem with exporting grid configuration in jsonstring format

No Tags
UserPost

11:29
05/05/2010


Asshiah

Member

posts 74

Post edited 08:30 – 05/05/2010 by Asshiah
Post edited 11:39 – 05/05/2010 by Asshiah


Hello,


I am using jqGridImport and jqGridExport to load the configuration of a grid and to save it to the server after the user has modified the hidden and showed columns with a column chooser.


The loading works just great. The column chooser too thanks to Tony's help! Laugh


However, I have a problem with the export.


I use this code to import the grid configuration, add a navigator and a columnchooser on it, then to export the configuration after an action has been performed in a column chooser:

$('#grid_users').jqGridImport({
      imptype: 'json',
      impurl: "/handlers/UsersJqGridImport.ashx",
      mtype: 'POST',
      jsonGrid: { config: 'Settings' },
      importComplete: function() {
        $('#grid_users').jqGrid('navGrid', '#pager_users', {
          edit: false,
          add: false,
          del: false,
          search: false,
          refresh:false
        }).jqGrid('navButtonAdd', '#pager_users', {
          caption: 'Colonnes',
          position: 'last',
          title: 'Show/Hide/Reorder columns',
          onClickButton: function() {
            $('#grid_users').jqGrid('columnChooser', {
              done: function(perm) {
                if (perm) {
                  var gridWidth = this.jqGrid('getGridParam', 'width');
                  this.jqGrid('setGridWidth', gridWidth);
                  this.jqGrid('remapColumns', perm, true);
                  $.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    url: "/handlers/UsersJqGridExport.ashx",
                    data: {exportdata: $('#grid_users').jqGridExport({
                        exptype: 'jsonstring',
                        root: 'Settings'
                        })}
                  });
                }
              }
            });
          }
        });
      }
    });

The thing is, when I try to get the exportdata on the serverside, I only get an empty string…


When using firebug, I can see that the exportdata posted in the request header has a problem with encoding:

exportdata=%7B%22Settings%22%3A%7B%0A%09%09%22url%22%3A%22%2Fhandlers%2FAltaresJqGridLoad.ashx%22%2C………………………………………..

Does someone have an idea on how to solve this problem?


Thank you.

10:40
06/05/2010


Asshiah

Member

posts 74

I can't figure it out. What can be the cause for this wrong encoding? And is it really this encoding problem that disable the possibility to get this data on the server side ina  string?

10:55
06/05/2010


Asshiah

Member

posts 74

In fact this encoding is an encoding that prevents problem with unsafe characters by encoding them with % followed by the ISO-8859-1 corresponding code of the character.


The thing is I don't know if it is automatically decoded in the server side or not.

11:18
06/05/2010


Asshiah

Member

posts 74

I think is is automatically decoded on the serverside or I would'nt get an empty string.


Maybe the problem is that the decoded jsonstring contains double quotes and it is a problem to hold this quotes in a normal string.

11:55
06/05/2010


Asshiah

Member

posts 74

Post edited 08:56 – 06/05/2010 by Asshiah
Post edited 08:57 – 06/05/2010 by Asshiah



I have try to modify my JS code like this:

$('#grid_users').jqGridImport({
      imptype: 'json',
      impurl: "/handlers/UsersJqGridImport.ashx",
      mtype: 'POST',
      jsonGrid: { config: 'Settings' },
      importComplete: function() {
        $('#grid_users').jqGrid('navGrid', '#pager_users', {
          edit: false,
          add: false,
          del: false,
          search: false,
          refresh:false
        }).jqGrid('navButtonAdd', '#pager_users', {
          caption: 'Colonnes',
          position: 'last',
          title: 'Show/Hide/Reorder columns',
          onClickButton: function() {
            $('#grid_users').jqGrid('columnChooser', {
              done: function(perm) {
                if (perm) {
                  var gridWidth = this.jqGrid('getGridParam', 'width');
                  this.jqGrid('setGridWidth', gridWidth);
                  this.jqGrid('remapColumns', perm, true);
                  $.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    url: "/handlers/UsersJqGridExport.ashx",
                    data: $('#grid_users').jqGridExport({
                        exptype: 'jsonstring',
                        root: 'Settings'
                        })
                  });
                }
              }
            });
          }
        });
      }
    });

Now with firebug I get this when looking at what is posted:

{"Settings":{

"url":"/handlers/UsersJqGridLoad.ashx",

"height":300…..

So now I don't have any encoding problem.


The thing is how can I get the JSON data on the server side?


16:40
10/05/2010


Asshiah

Member

posts 74

I have tried many things but nothing worked.


When I use advanced search, the different filters are passed to an url in JSON format with a post variable named "filters":


filters =
{"groupOp":"AND",
"rules":[
{"field":"invdate","op":"ge","data":"2007-10-06"},
{"field":"invdate","op":"le","data":"2007-10-20"},
{"field":"name","op":"bw","data":"Client 3"}
]
}

The json encoding of the different filters appear to be fine and don't have en encoding problem.

I can work with this jsonstring and use it on the server side without any problem.


But when I try to export the grid configuration in JSON format with the following AJAX request I get an encoding problem that prevents me from doing anything with the jsonstring in the server side:


$.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    url: "/handlers/UsersJqGridExport.ashx",
                    data: {exportdata: $('#grid_users').jqGridExport({
                        exptype: 'jsonstring',
                        root: 'Settings'
                        })}
                  });

I get this kind of encoding:


exportdata=%7B%22Settings%22%3A%7B%0A%09%09%22url%22%3A%22%2Fhandlers%2FAltaresJqGridLoad.ashx%22%2C


Does anyone know how to send the exported configuration jsonstring with the rigth encoding?


Thanks.


17:20
10/05/2010


Asshiah

Member

posts 74

In fact, it appears when looking the source code of the request with firebug that the encoding is the same for the advanced search and the export jsonstring.


Both are encoding using urlencoding.


Only, for the filters, firebug can decode the jsonobject while it can't for the exportdata.

The exportdata appears to be too big for firebug.


Thus, if the filters for the search can be read on the server side, so should be the exportdata.


Is it possible that an exportdata too big prevents it from beeing transmitted entirely to the server?

12:56
12/05/2010


tony

Sofia, Bulgaria

Moderator

posts 7375

Hello,

If you use get method in the ajax that send the data to the server this is quite possible,

Please use post method in the ajax request when you send data to the server.

Regards

Tony

For professional UI suites for PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site – trirand.net – by the very same guys that created jqGrid.

14:16
12/05/2010


Asshiah

Member

posts 74

Unfortunately, I already use the POST method.


Maybe this is not a transmission problem but rather a problem with the configuration content like the column names for instance that can contain double quotes.


I am presently investigating it.

14:30
12/05/2010


Asshiah

Member

posts 74

Apparently, I don't have any double quotes in my column names.


All I know is that when I try to read the content of the exportdata on the server side, I get an empty string.

But when I look at what is posted with firebug, the exportdata is not empty but appear to be too long for firebug.


I can't figure it out.

17:12
12/05/2010


Asshiah

Member

posts 74


Finally!!!


I have succeeded in exporting the configuration and saving it on the server side.


The trick was:


  • Be sure that there is neither a single or a double quote in a column name.
  • Do not specify a POST parameter in order to pass the export data to the server but rather send the result of the jqGridExport method directly into the AJAX call data parameter.
  • On the server side, read the request inputstream directly from the http context with a streamreader and convert it to a string afterwards.

Here is an exemple of the AJAX call definition:


$.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    url: "/handlers/testJqGridExport.ashx",
                    data: $('#grille_altares').jqGridExport(
                        {
                            exptype: 'jsonstring',
                            root: 'Settings'
                        }
                        )
                  });


I hope this will help some people.


Thanks!

20:11
27/01/2013


nj

Colombia

Member

posts 3

Hi Asshiah,

I am trying to import some predefined grid configs from a DB. However I cannot get it working.

Would you mind sharing your ashx import handler?

thanks in advance.

Nj

No Tags

About the jQuery Grid Plugin – jqGrid forum

Most Users Ever Online:

215


Currently Online:

71 Guests

Forum Stats:

Groups: 1

Forums: 7

Topics: 10268

Posts: 30346

Membership:

There are 11031 Members

There have been 448 Guests

There is 1 Admin

There are 2 Moderators

Top Posters:

OlegK – 1210

markw65 – 179

kobruleht – 144

phicarre – 132

YamilBracho – 124

Renso – 118

Administrators: admin (60 Posts)

Moderators: tony (7375 Posts), Rumen[Trirand] (81 Posts)




Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information