Jquery Jtable load Json from ajax call

Misha Akopov picture Misha Akopov · Dec 9, 2017 · Viewed 10.7k times · Source

I am using jtable.org-s library for showing rows in a table. For populating rows I used listAction like this:

actions: {
  listAction: '/Passengers/Search'
},

This URL returned valid json and everything worked fine. But I want to call the URL manually from ajax, because this URL returns more info than rows. It also returns info I need for other form elements, such as search result items count, woman/man ratio and etc.

So, I want to ajax call, and load the response to my jtable:

$.ajax({
  url: '/Passengers/Search',
  type: 'POST',
  data: {},
  success: function(data) {
    // here I would like to inject the Json(data) to my table
  }
});

Answer

misterP picture misterP · Mar 19, 2018

The answer suggested by abpatil is a good answer.

A simpler solution may be to use the jTable recordsLoaded event http://jtable.org/ApiReference/Events#event-recordsLoaded

The handler is given an event data object which has two components. data.records which is an array of all the records sent by the server, and data.serverResponse which is the full json response from the server.

You can loop through the records and process any extra fields.

The server can also send extra json properties not used by jTable and you can process the data.serverResponse here and use them in other page elements.