1

I have constructed the jqgrid using struts2-jquery-jqgrid plugins. Everything is working great except paging,

Problem : When I click on the next button in pager, I want to perform some ajax-based operation. I am using onPaging(jqgrid method) or onPagingTopics topics(jqgrid-plugins) to trap the paging event. I get this paging event successfully but from here actual problem starts. When I click on next button, I trap the paging event using onPagingTopics Topics, inside this event I call an ajax-request, but before this ajax-request is completed , the jqgrid is automatically getting reload, and that is why I can not make the ajax-call successfully. Why this is happening and how to restrict jqgrid from reload.

Note: My jqgrid is getting reload by calling the url I have specified inside this grid. You can check the grid here how to load json in jqgrid

This is how I trap the paging Event:

$.subscribe('paging',function(event,data) 
 {
   var search_string=$("#searchString").val();
   var search_field=$("#searchField").val();        
$.ajax({
         url:"adminJsontable?searchString="+search_string,
         dataType:"json",
         success: function(jsondata,stat)
          {
            var mgrid = jQuery("#gridtable")[0];
            mgrid.addJSONData(jsondata);
          }
       });
  });

Update Section:

     $.subscribe('before',function(event,data) 
  {
    var grid = jQuery("#gridtable"); 
    grid.jqGrid('setGridParam',{url:"adminJsontable?searchString="+"neo"});
  });

This is how I am changing the url, but its not working. This way I can get any events like ready, complete,success. Please guide me now wht I have to do.

Note : I can use any methods or any events which is used in jqgrid but don't know how to make it.

Community
  • 1
  • 1
arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47

1 Answers1

1

I think that there are just misunderstanding how jqGrid works. If the user choose another page jqGrid makes ajax request to the server per default. If you need to include additional custom filter in the request to the server you can use postData (see the answer). You can do for example the following

$("#grid").jqGrid({
    url: "adminJsontable",
    postData: {
        searchField: function () {
            return $("#searchField").val();
        },
        searchValue: function () {
            return $("#searchString").val();
        }
    },
    ... // other options which you use
});

I don't use Struts myself, but I am sure that you can rewrite yourself for the usage of inside Struts.

The above code will send additional parameters searchField and searchValue on every request to the server including paging sorting etc. So if the user enter some data in #searchField and #searchString then the data will be considered by your server code.

UPDATED: If you don't know how to specify postData in case of usage struts2-jquery-jqgrid plugin you must be still able to include some JavaScript file on the HTML page. If you includes

$.extend(true, $.jgrid.defaults, {
    postData: {
        searchField: function () {
            return $("#searchField").val();
        },
        searchValue: function () {
            return $("#searchString").val();
        }
    }
});

you will be able to redefine default value {} of postData parameter of jqGrid to the value which I suggested above. It's important that the above code should be executed before jaGrid is created. If you can execute JavaScript code only after the grid is created you can execute the following code

$.extend(true, $("#gridtable").jqGrid("getGridParam", "postData"),
    {
        searchField: function () {
            return $("#searchField").val();
        },
        searchValue: function () {
            return $("#searchString").val();
        }
    }
});
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • "jqGrid makes ajax request to the server per default", this is what i want to stop ,actually problem is poor documentation of struts2-jquery-plugins, grid reload is creating a lot of problem for me, – arvin_codeHunk Feb 28 '13 at 11:29
  • as well as I am creating grid using struts2,which is internally usage jqgrid, is there any kind of solution which prevent jqgrid from reload. – arvin_codeHunk Feb 28 '13 at 11:32
  • @arvin_codeHunk: You want to do manually exactly the same Ajax call which can jqGrid do for you. If you will do it manually you will have to solve many problems. You included only the first one in your question. I am sure that you goes in the wrong way. Just try my suggestion and you will see the advantage of the approach. – Oleg Feb 28 '13 at 11:33
  • feeling shame for saying, but I dont know how to do this manually, In my grid I am loading grid using a key href="my url" , this the reason for jqgrid can automatically call the action, but I dont know how to do it manually, – arvin_codeHunk Feb 28 '13 at 11:40
  • @arvin_codeHunk: Do you can specify `postData` parameter of jqGrid in the truts2-jquery-jqgrid plugins? – Oleg Feb 28 '13 at 11:55
  • first of all where I should use postData , as well as the above code you have mentioned – arvin_codeHunk Feb 28 '13 at 11:59
  • @arvin_codeHunk: We speak on different languages. jqGrid is open source **JavaScript** plugin to jQuery. So you create jqGrid in the form like I posted either directly or indirectly. In any way you should be able to execute some *additional* JavaScript on the same page. Isn't so? If you can then you can execute `jQuery.extend(true, jQuery.jgrid.defaults, {posdData: {searchField: function () {return $("#searchField").val();},searchValue: function () { return $("#searchString").val();}}});`. If you execute the code **before** jqGrid is created it will use `postData` which I posted in my answer. – Oleg Feb 28 '13 at 12:03
  • the same problem I faced here http://stackoverflow.com/questions/15005307/restrict-jqgrid-from-auto-refreshing, which i solved ,Please have a look, I need that kind of solution – arvin_codeHunk Feb 28 '13 at 12:31
  • @arvin_codeHunk: I don't think that the problem in another your question is the same as here. Do you tried to change `$.jgrid.defaults` like I suggested in the previous comment? – Oleg Feb 28 '13 at 13:31
  • yes, but ,postData is not working in my grid, as well as I am able to trap events like beforeGrid,gridcomplete etc.,but nothing changed, there is a possibility that i am doing in the wrong way. – arvin_codeHunk Feb 28 '13 at 13:55
  • @arvin_codeHunk: Do you have some live demo? In the case I could immediately see what you make incorrect. – Oleg Feb 28 '13 at 13:58
  • live demo is not possible here, because filters are active inside server – arvin_codeHunk Feb 28 '13 at 14:04
  • oleg my problem is my ajax-call is executing as well as another jqgrid url is also executed ,that is the main issue, – arvin_codeHunk Feb 28 '13 at 14:28
  • @arvin_codeHunk: I can repeat that I think that *you misunderstand the problem which you has*. What you ask is about the question how you can remove the motor from the auto because the auto run too quickly if you need to turn right or left. The origin of the problem is that you need use brake of the auto in case if it require. Removing the motor is not real solution of the problem. If setting of `postData` is not work in your code then you do this in the wrong way you should analyse the JavaScript code which will be produce, find the reason and fix the problem. – Oleg Feb 28 '13 at 15:02
  • @arvin_codeHunk: You can use [Fiddler](http://www.fiddler2.com/Fiddler2/version.asp), [Firebug](https://getfirebug.com/) or Developer Tools of IE or Google Chrome to catch HTTP traffic. If you see that jqGrid send `searchField` and `searchValue` (from `postData`) then `postData` works. – Oleg Mar 01 '13 at 06:10
  • I have getters n setters for searchField posted (from postData) ,if postdata is working then I must have values in these getters-setters but searchField is showing null – arvin_codeHunk Mar 01 '13 at 06:46
  • @arvin_codeHunk: Do you verified HTTP traffic? Could you see `searchField` and `searchValue` sent to the server? – Oleg Mar 01 '13 at 07:50
  • _ 1362205292275 , _search false , nd 1362205292273, page 1, rows 10, sidx , sord asc , these params are passed when i check the traffic using firebug. – arvin_codeHunk Mar 02 '13 at 06:22
  • @oleg.. i solved this using formids attribute of struts2-jqgrid, which post all form element to server. – arvin_codeHunk Mar 20 '13 at 10:16