An action defines how to communicate from client to the server. There are four types of action:
An action must be defined in actions list of jTable options a like:
... actions: { //Actions definitions come here } ...
An action value can be a URL string or a function (See each action for details).
If you defined listAction as a URL string, then, when you use the load method, jTable makes an AJAX POST to this URL address to get list of records. This URL must return a JSON object. Here, there is a sample return value for this URL:
{ "Result":"OK", "Records":[ {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"} ] }
Result property can be "OK" or "ERROR". If it is "OK", Records property must be an array of records. If Result is "ERROR", Message property must explain reason of the error to show to the user.
Spesifically, a date field (that is transferred between jTable and server side code) must be one of the formats below:
If paging is enabled, jTable sends two query string parameters to the server on listAction AJAX call:
Also, one additional information is expected from server:
If sorting is enabled, jTable sends one additional query string parameter to the server on listAction AJAX call:
You can set a function to this action. Your function can return one of the followings:
To return the data itself, you can write such an action definition:
listAction: function (postData, jtParams) { return { "Result": "OK", "Records": [ { "StudentId": 39, "ContinentalId": 1, "CountryId": 18, "CityId": 55, "Name": "Agatha Garcia", "EmailAddress": "[email protected]", "Password": "123", "Gender": "F", "BirthDate": "\/Date(-1125111600000)\/", "About": "", "Education": 2, "IsActive": true, "RecordDate": "\/Date(1369083600000)\/" }, { "StudentId": 61, "ContinentalId": 4, "CountryId": 1, "CityId": 1, "Name": "Agatha Lafore", "EmailAddress": "[email protected]", "Password": "123", "Gender": "F", "BirthDate": "\/Date(1017694800000)\/", "About": "", "Education": 3, "IsActive": true, "RecordDate": "\/Date(1393192800000)\/" }], "TotalRecordCount": 2 }; }
While returning the data is usable in demos, it's not the common case for real world scenarios. Probably you want to make an ajax call and get the data from the server. In this case, you can return a promise using jQuery.Deferred object as shown example below:
listAction: function (postData, jtParams) { return $.Deferred(function ($dfd) { $.ajax({ url: '/Demo/StudentList?jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting, type: 'POST', dataType: 'json', data: postData, success: function (data) { $dfd.resolve(data); }, error: function () { $dfd.reject(); } }); }); }This function gets some arguments:
In the function, you can change format of returning data before use $dfd.resolve(data). Data must be formatted as expected format as jTable waits for. So, all of the written in 'Setting a URL string as listAction' are also valid here.
If you defined createAction as a URL, then, when you create and save a record, jTable makes an AJAX POST call to this URL address to save the record to the server.
createAction is optional. If it is not defined, user can not add new records to the table. This URL must return a JSON object. Here, there is a sample return value for this URL:
{ "Result":"OK", "Record":{"PersonId":5,"Name":"Dan Brown","Age":55,"RecordDate":"\/Date(1320262185197)\/"} }
Result property can be "OK" or "ERROR". If it is "OK", Record property must be the newly created record. This is needed by jTable to add record to the table. If Result is "ERROR", Message property must explain reason of the error to show to the user.
You can set a function to this action. Your function can return one of the followings:
Since it's very similar to listAction, it will not be defined in detailed here. You can see the demo.
When you edit and save a record, jTable makes an AJAX POST to this URL address to update record on the server.
updateAction is optional. If it is not defined, edit image (button) is not shown. This URL must return a JSON object. Here, there is a sample return value for this URL:
{"Result":"OK"}
Result property can be "OK" or "ERROR". If Result is "ERROR", Message property must explain reason of the error to show to the user.
updateAction can also return a Record value (optional). This record is overwriten to updating record in the table. It's not completely replaced, overwritten field by field. So, for instance, if you return a record with just a single field, only this field is overwritten.
{ "Result":"OK", "Record":{"Name":"Dan Brown","Age":55,"LastUpdateDate":"\/Date(1320262185197)\/"} }
You can set a function to this action. Your function can return one of the followings:
Since it's very similar to listAction, it will not be defined in detailed here. You can see the demo.
When you delete a record, jTable makes an AJAX POST to this URL address to delete the record on the server.
deleteAction is optional. If it is not defined, delete image (button) is not shown. This URL must return a JSON object. Here, there is a sample return value for this URL:
{"Result":"OK"}
Result property can be "OK" or "ERROR". If Result is "ERROR", Message property must explain reason of the error to show to the user.
You can set a function to this action. Your function can return one of the followings:
Since it's very similar to listAction, it will not be defined in detailed here. You can see the demo.
Advertisement: Professional startup template for ASP.NET MVC & AngularJs by creator of jTable!
Based on metronic theme, includes pre-built pages like login, register, tenant, role, user, permission and setting management. Learn more...