SoftwareSavant
SoftwareSavant

Reputation: 9757

beforeRequest event jqGrid: set jqGrid page before url is called

I am trying to set the page variable in jqGrid before the GET request to the URL...

        jQuery("#frTable").jqGrid({                
            cmTemplate: { sortable: false },
            caption: '@TempData["POPNAME"]' + ' Population',
            datatype: 'json',                
            mtype: 'GET',
            url: '/Encounters/GetAjaxPagedGridData/',

I have tried all sorts of things and I can't seem to get this to work no matter what I try... I have tried all sorts of things and the only thing that 'sort-of' worked was subscribing to the LoadComplete event like so...

            loadComplete: function() {
                if ((rowFromTemp != "") && (pageFromTemp != "")) {
                    setTimeout(function () {
                    $("#frTable").trigger("reloadGrid", [{page: pageFromTemp, rowNum: rowFromTemp}]); 
                    }, 50);
                }
            },

the only problem that I had with that was that it was constantly updating. Not sure why that would happen. The only thing I figure there is that I am somehow looping with my timeout function...

I have tried subscribing to a couple of other requests but each of those seems to fail. The most promising I felt was the beforeRequest event. This is what I currently have...

            beforeRequest: function() {
                if ((rowFromTemp != "") && (pageFromTemp != "")) {
                    $("#frTable").setGridParam([{ page: pageFromTemp, rowNum: rowFromTemp }]);                        
                    }
            },

It doesn't care what the heck I set the variables to, It just opens up on page 1 NO MATTER WHAT.
Now, I here that you can set the data to 'local' and that means that you have to reload the grid to get any data.

But I want the data to load when a user first opens it up. I am trying to set the page so that after a user edits a row with my edit screen, when he hits save, he will be taken back to the page that he was just on. Not the first page. As you know, that would be very frustrating for a user.

UPDATE

Ok... I put an alert in my beforeRequest event, and it fired before my grid loaded. Setting a break point in my action that is called in the url section tells me that the alert is called before I actually make the request. Which is what I am hoping for. I just need to figure out how to properly set the page variable.

UPDATE 2 Ok. I set the variable, and I had my alert show me that the variable was indeed set...

            beforeRequest: function() {
                if ((rowFromTemp != "") && (pageFromTemp != "")) {
                    this.p.page = pageFromTemp;
                    alert("This is what the page is: " + this.p.page + ";  And this is what I want it to be: \n" + pageFromTemp + " How do I get it there \n");

However, it still loads the first page? So obviously, it is getting overridden again. Why? How can I set this permanently.

Upvotes: 2

Views: 8799

Answers (1)

Diogo Luis
Diogo Luis

Reputation: 228

I had the same problem but I got it working doing this on the BeforeRequest function:

$('#GRID_ID').jqGrid('setGridParam', { postData: { page: 4} });

Upvotes: 3

Related Questions