Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7732917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:51:06+00:00 2026-06-01T06:51:06+00:00

I have been trying to figure this out for some time. It seems lots

  • 0

I have been trying to figure this out for some time. It seems lots of others have asked similar questions but I have not been able to find a specific solution yet. Anyway, I have a jqgrid that I am using on a large data set. I am only paging 100 records at a time out of several thousand records. What I want to do is add a new record. Send it to the server that returns the new record ID and then be able to fetch the sorted data section where my newly added record resides and position to that row in the grid.

Here is my grid definition:

$("#list1").jqGrid({
      url: 'jqgrid.php?cmd=getrecs',
      editurl: 'jqgrid.php?cmd=editrec',
      datatype: 'json',
      colNames:['Branch', 'Description', 'Type', 'Active' ],
      colModel :[
        {name:'rbranch',
                index:'rbranch',
                sortable:true,
                editable:true
        },
        {name:'des',
                index:'des',
                sortable:true,
                editable:true
        },
        {name:'type',
                index:'type',
                sortable:true,
                editable:true
        },
        {name:'status',
                index:'status',
                sortable:false,
                editable:true
        }
      ],
      pager: '#pager1',
      sortname: 'rbranch',
      sortorder: 'asc',
      rowNum: 100, // Only fetch 100 at a time
      viewrecords: true,
      scroll: 1,
      sortable: true,
      caption: 'Scheduling Resources'
});

$("#list1).navGrid("#pager1",
      // Turn on the icons
      {edit:true,
              add:true,
              del:true,
              search:true,
              refresh:true,
              refreshstate:'current',
              view:true
      },
      // Edit dialog parameters
      {reloadAfterSubmit: false,
              closeAfterEdit: true
      },
      // Add dialog parameters
      {reloadAfterSubmit: true,
              afterSubmit: function (response) {
                   var x = $.parseJSON(response.responseText).userdata.newID;
                   alert(x);
                   return [true, '', "Saved"];
              },
              closeAfterAdd: true
      },
      // Delete dialog parameters
      {reloadAfterSubmit: false},
      // Search dialog parameters
      {},
      // View dialog parameters
      {}
);

Using afterSubmit I can get the newly created ID value by returning it via JSON.

I already have a routine on the server that locates the proper record set and can return it. However, I am at a loss on how to pass this new ID I get back on add back to the server.

Examining how the Grid Add works if using “reloadAfterSubmit: true” (which I need to resort my new record) it appears it actually makes two calls to the server. The first call uses the “editurl:” from the grid which tells the server all the new fields and lets me create and send back the new ID. It then calls the server a second time to fetch the new set of records this time using “url:” from the grid which just fetches the first page.

I think I can do what I want if I could only pass the new ID I get from the first call as a parameter to the second call. Maybe there is a real easy way to do this but I am new to jquery and jqgrid so have not figured this out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:51:08+00:00Added an answer on June 1, 2026 at 6:51 am

    OK, I think I figured this out and will post my findings here. Maybe someone will find it helpful. (or maybe there is an even better way)

    First of all I added a new hidden area in the DOM to store the returned ID of the newly added row.

    <div id="extraData" style="display:none;">
      <input type="text" id="gotoID"/>
    </div>
    

    Then in the navGrid definition under the add section I added these settings:

    reloadAfterSubmit: true,
    afterSubmit: function (response) {
       var id = $.parseJSON(response.responseText).userdata.newID;
       $("#gotoID").val(id);
       return [true, '', "Saved"];
    },
    

    When the add first calls the server and sends the record data to be added I create the record and return the newID as part of the JSON response. Which looks something like this:

    {
       "userdata": {
            "type": "success",
            "newID": "36137"
       }
    }
    

    The afterSubmit function defined above will store this in the DOM. Because I have reloadAfterSubmit: true the add then does a second call to the server using the standard trigger(“reloadGrid”).

    Here I had to modify my jqGrid with the following:

    // This will send the stored gotoID if set to the server 
    postData: {gotoID: function() {return $('#gotoID').val();}},
    // This allows the grid to scroll selected row into view
    scrollrows: true,
    loadComplete: function(){
       var userdata = jQuery("#list1").getGridParam('userData');
    
       // Blank out the gotoID holder
       $("#gotoID").val('');
    
       // This is the returned ID to position to
       if (userdata.selId) {
          // You need to unselect any selected rows
          jQuery("#list1").resetSelection();
          // Here I reset the page to be the newly determined page
          $("#list1").jqGrid('setGridParam',{ page: userdata.page });
          jQuery("#list1").setSelection(userdata.selId, true);
        }
      }
    

    So what happens on the subsequent call to the server is that it sends the gotoID as part of the post data. I can then find what page, based on all my settings (like current sort/filter criteria, number of items per page, etc.) and send that back as part of my JSON response along with the grid data. So I get back the ID to position to, the page that is to be displayed, and all the grid data that is to be displayed on that page. Again, I include this additional data in the JSON response in the userdata something like this:

    {
        "userdata": {
            "type": "success",
            "page": "76",
            "selId": "36137",
            "records": "12618"
        },
        "rows": [
            <this is your grid data>....
        ]
     }
    

    This same routine works for both adding new records or editing existing records where the sort order may change.

    *Note: I found out that this won’t work unless you are using the standard paging model (so you can’t use scroll:1 feature of the jqgrid) *

    So as a recap here is the final grid and navGrid settings:

    $("#list1").jqGrid({
      url: 'jqgrid.php?cmd=getrecs',
      editurl: 'jqgrid.php?cmd=editrec',
      datatype: 'json',
      colNames:['Branch', 'Description', 'Type', 'Active' ],
      colModel :[
        {name:'rbranch',
                index:'rbranch',
                sortable:true,
                editable:true
        },
        {name:'des',
                index:'des',
                sortable:true,
                editable:true
        },
        {name:'type',
                index:'type',
                sortable:true,
                editable:true
        },
        {name:'status',
                index:'status',
                sortable:false,
                editable:true
        }
      ],
      pager: '#pager1',
      sortname: 'rbranch',
      sortorder: 'asc',
      rowNum: 100, // Only fetch 100 at a time
      viewrecords: true,
      // This will send the stored gotoID if set to the server 
      postData: {gotoID: function() {return $('#gotoID').val();}},
      // This allows the grid to scroll selected row into view
      scrollrows: true,
      loadComplete: function(){
          var userdata = jQuery("#list1").getGridParam('userData');
    
          // Blank out the gotoID holder
          $("#gotoID").val('');
    
          // This is the returned ID to position to
          if (userdata.selId) {
              // You need to unselect any selected rows
              jQuery("#list1").resetSelection();
              // Here I reset the page to be the newly determined page
              $("#list1").jqGrid('setGridParam',{ page: userdata.page });
              jQuery("#list1").setSelection(userdata.selId, true);
          }
      },
      sortable: true,
      caption: 'Scheduling Resources'
    });
    
    $("#list1).navGrid("#pager1",
      // Turn on the icons
      {edit:true,
              add:true,
              del:true,
              search:true,
              refresh:true,
              refreshstate:'current',
              view:true
      },
      // Edit dialog parameters
      {reloadAfterSubmit: true,
              afterSubmit: function (response) {
                  var id = $("list1").getGridParam('selrow');
                  $("#gotoID").val(id);
                  return [true, '', "Saved"];
              },
              closeAfterEdit: true
      },
      // Add dialog parameters
      {reloadAfterSubmit: true,
              afterSubmit: function (response) {
                  var id = $.parseJSON(response.responseText).userdata.newID;
                  $("#gotoID").val(id);
                  return [true, '', "Saved"];
              },
              closeAfterAdd: true
      },
      // Delete dialog parameters
      {reloadAfterSubmit: false},
      // Search dialog parameters
      {},
      // View dialog parameters
      {}
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying this for some time but could not figure out yet.
i have been trying to figure this out but i just can't.... for some
I have been trying to figure this out for some time now and can't
I have been Trying to figure this out for some time now. I have
I have been trying to figure this out for quite a while, but what
So I have been trying to figure this out for a while, but nothing
I have been pulling my hair trying to figure this out but nothing is
I have been trying to figure this one out but I am having a
I have been trying to figure this out for way to long tonight. I
I have been trying to figure out why I am getting this problem and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.