title: jqGrid set records count
body: <p>I have a jqGrid that points at an external API, which I have no control over.  This external API has two endpoints:</p>
  
  <ul>
  <li><strong>Data</strong> Endpoint - Returns the table row data</li>
  <li><strong>Count</strong> Endpoint - Returns pagination counts etc.,</li>
  </ul>
  
  <p>Based on user input the jqGrid filter gets converted into the appropriate query-string to filter the external API's <strong>Data</strong> and <strong>Count</strong> Endpoints.</p>
  
  <p>I have jqGrids <code>url</code> being dynamically built based off of user input and targets the <strong>Data</strong> Endpoint...and during the <code>loadBeforeSend()</code> event it calls the <strong>Count</strong> Endpoint to get the latest pagination information based on users filter.</p>
  
  <p>I am using the jsonreader capabilities:</p>
  
  <pre><code>jsonReader: {
                      root: 'products',
                      id: 'id',
                      records: function () {
  
                          return gridTotal;
                      },
                      total: function () {
                          // var totalPages = (gridTotal + reqOptions.limit-1) / reqOptions.limit;
                          var totalPages = Math.ceil(gridTotal / reqOptions.limit);
                          console.log('totalPages: ' + totalPages);
                          return totalPages;
                      },
                      page: function () {
                          //var totalPages = Math.ceil(gridTotal/20);
                          console.log('currentPage: ' + reqOptions.page);
                          return reqOptions.page;
                      }
                  },
  </code></pre>
  
  <p>Sample of the loadBeforeSend method:</p>
  
  <pre><code>loadBeforeSend: function (xhr, settings) {
      settings.url = _newUrl || endpointURL;
  
      // Lets fetch our data count...this may change as items get published so lets fetch during load
      products.count(accessToken, _filterQuery)
          .success(function (resp) {
              // This is the total number of products that match our current search
              gridTotal = resp.count;
          }).catch(function (err) {
              console.error(err);
          });
  }
  </code></pre>
  
  <p>Fetching from the <strong>Data</strong> Endpoint works really well, the issue is how to call the <strong>Count</strong> Endpoint and update the pagination data. </p>
  
  <p>Tried the following:</p>
  
  <ul>
  <li>Using <code>setGridParam</code> for records, last_page, etc.,</li>
  <li>Using <code>getGridParam('reccount')</code></li>
  <li>Just update the html to look correct (not effective since paging will be off)</li>
  </ul>
  
  <p>Is there a way to</p>
  
  <ul>
  <li>Manually fire off the XHR for jqgrid URL...so I can request the Count first and when it returns then go fetch the Data?</li>
  <li>Rerun the jsonreader functionality once the Count returns and <code>gridTotal</code> is set</li>
  <li>Use a promise like structure to resolve records count</li>
  </ul>
  
  <hr>
  
  <p>Updated to show @Oleg solution</p>
  
  <pre><code>    loadBeforeSend: function (xhr, settings) {
          settings.url = _newUrl || endpointURL;
  
          // Lets fetch our data count...this may change as items get published so lets fetch during load
          products.count(accessToken, _filterQuery)
              .success(function (resp) {
                  // This is the total number of products that match our current search
                  gridTotal = resp.count;
  gridTotal = resp.count;
                              grid.jqGrid('setGridParam', {
                                  page: gridOpts.jsonReader.page(),
                                  records: gridTotal,
                                  lastpage: gridOpts.jsonReader.total()
                              });
                              grid[0].updatepager(false, true);
              }).catch(function (err) {
                  console.error(err);
              });
      }
  </code></pre>