-
Notifications
You must be signed in to change notification settings - Fork 0
api_actions
An action defines how to communicate from the client to the server. There are four types of actions:
- listAction: Defines how to get the list of records.
- createAction: Defines how to submit a "create new record" form.
- updateAction: Defines how to submit an "edit record" form.
- cloneAction: Defines how to submit an "clone record" form.
- deleteAction: Defines how to delete a record.
Actions must be defined in the actions list of jTable options:
actions: {
// Action definitions go here
}An action value can be a URL string or a function. See each action for details.
- Type: URL string or function
- Default: None
If listAction is defined as a URL string, jTable makes an AJAX POST request to this URL to fetch the list of records. The URL must return a JSON object:
{
"Result": "OK",
"Records": [
{ "PersonId": 1, "Name": "Benjamin Button", "Age": 17, "RecordDate": "/Date(1320259705710)/" },
{ "PersonId": 2, "Name": "Douglas Adams", "Age": 42, "RecordDate": "/Date(1320259705710)/" }
]
}-
Result: Can be
"OK"or"ERROR". If"OK", theRecordsproperty must contain an array of records. - Records: An array of records to display in the table.
-
Message: If
Resultis"ERROR", this property explains the error.
-
/Date(milliseconds)/: Example:/Date(1320259705710)/. -
YYYY-MM-DD HH:MM:SS: Example:2012-01-03 21:40:42. -
YYYY-MM-DD: Example:2012-01-03.
If paging is enabled, jTable sends the following query string parameters:
-
jtStartIndex: Start index of records for the current page. -
jtPageSize: Maximum number of records to fetch.
The server must return:
-
TotalRecordCount: Total number of records (not just the current page).
If sorting is enabled, jTable sends:
-
jtSorting: A string representing the sorting request (e.g.,Name ASC,BirthDate DESC).
You can define listAction as a function. The function can return:
- The data itself.
- A promise (using
jQuery.Deferred) to return the data.
Example:
listAction: function (postData, jtParams) {
return $.Deferred(function ($dfd) {
$.ajax({
url: '/Demo/StudentList',
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}-
postData: Data provided when calling the
loadmethod. -
jtParams: Contains
jtStartIndex,jtPageSize, andjtSortingif paging or sorting is enabled.
- Type: URL string or function
- Default: None
If createAction is defined as a URL, jTable makes an AJAX POST request to this URL to save a new record. The URL must return a JSON object:
{
"Result": "OK",
"Record": { "PersonId": 5, "Name": "Dan Brown", "Age": 55, "RecordDate": "/Date(1320262185197)/" }
}-
Result: Can be
"OK"or"ERROR". -
Record: The newly created record (required if
Resultis"OK"). -
Message: Error message if
Resultis"ERROR"(required in the ERROR case, otherwise a "communication error" will be shown). If this property is present ifResultis"OK", then an info-dialog will show with this message after the record has been inserted (only if the insert is done via the add-record button the toolbar).
You can define createAction as a function. The function can return:
- The data itself.
- A promise (using
jQuery.Deferred) to return the data.
Example:
createAction: function (postData) {
return $.Deferred(function ($dfd) {
$.ajax({
url: '/Demo/CreatePerson',
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}- Type: URL string or function
- Default: None
If updateAction is defined as a URL, jTable makes an AJAX POST request to this URL to update a record. The URL must return a JSON object:
{
"Result": "OK",
"Record": { "Name": "Dan Brown", "Age": 55, "LastUpdateDate": "/Date(1320262185197)/" }
}-
Result: Can be
"OK"or"ERROR". - Record: Optional. Overwrites fields in the updated record.
-
Message: Error message if
Resultis"ERROR"(required in the ERROR case, otherwise a "communication error" will be shown). If this property is present ifResultis"OK", then an info-dialog will show with this message after the record has been updated (only if the updated is done via the updated-button in the table).
You can define updateAction as a function. The function can return:
- The data itself.
- A promise (using
jQuery.Deferred) to return the data.
Example:
updateAction: function (postData) {
return $.Deferred(function ($dfd) {
$.ajax({
url: '/Demo/UpdatePerson',
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}- Type: URL string or function
- Default: None
If cloneAction is defined as a URL, jTable makes an AJAX POST request to this URL to clone a record. The URL must return a JSON object:
{
"Result": "OK",
"Record": { "Name": "Dan Brown", "Age": 55, "RecordDate": "/Date(1320262185197)/" }
}-
Result: Can be
"OK"or"ERROR". - Record: Optional. Overwrites fields in the cloned record.
-
Message: Error message if
Resultis"ERROR"(required in the ERROR case, otherwise a "communication error" will be shown). If this property is present ifResultis"OK", then an info-dialog will show with this message after the record has been cloned (only if the clone is done via the clone-button in the table).
You can define cloneAction as a function. The function can return:
- The data itself.
- A promise (using
jQuery.Deferred) to return the data.
Example:
cloneAction: function (postData) {
return $.Deferred(function ($dfd) {
$.ajax({
url: '/Demo/ClonePerson',
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}- Type: URL string or function
- Default: None
If deleteAction is defined as a URL, jTable makes an AJAX POST request to this URL to delete a record. The URL must return a JSON object:
{
"Result": "OK"
}-
Result: Can be
"OK"or"ERROR". -
Message: Error message if
Resultis"ERROR"(required in the ERROR case, otherwise a "communication error" will be shown). If this property is present ifResultis"OK", then an info-dialog will show with this message after the record has been deleted (only if the deleted is done via the delete-button in the table).
You can define deleteAction as a function. The function can return:
- The data itself.
- A promise (using
jQuery.Deferred) to return the data.
Example:
deleteAction: function (postData) {
return $.Deferred(function ($dfd) {
$.ajax({
url: '/Demo/DeletePerson',
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}