Skip to content

api_actions

Franky Van Liedekerke edited this page Jul 15, 2025 · 6 revisions

jTable API Reference - Actions


Actions

An action defines how to communicate from the client to the server. There are four types of actions:

  1. listAction: Defines how to get the list of records.
  2. createAction: Defines how to submit a "create new record" form.
  3. updateAction: Defines how to submit an "edit record" form.
  4. cloneAction: Defines how to submit an "clone record" form.
  5. 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.


listAction

  • Type: URL string or function
  • Default: None

Setting a URL string as listAction

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", the Records property must contain an array of records.
  • Records: An array of records to display in the table.
  • Message: If Result is "ERROR", this property explains the error.

Date Formats

  • /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.

Paging

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).

Sorting

If sorting is enabled, jTable sends:

  • jtSorting: A string representing the sorting request (e.g., Name ASC, BirthDate DESC).

Setting a Function as listAction

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 load method.
  • jtParams: Contains jtStartIndex, jtPageSize, and jtSorting if paging or sorting is enabled.

createAction

  • Type: URL string or function
  • Default: None

Setting a URL string as createAction

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 Result is "OK").
  • Message: Error message if Result is "ERROR" (required in the ERROR case, otherwise a "communication error" will be shown). If this property is present if Result is "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).

Setting a Function as createAction

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();
            }
        });
    });
}

updateAction

  • Type: URL string or function
  • Default: None

Setting a URL string as updateAction

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 Result is "ERROR" (required in the ERROR case, otherwise a "communication error" will be shown). If this property is present if Result is "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).

Setting a Function as updateAction

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();
            }
        });
    });
}

cloneAction

  • Type: URL string or function
  • Default: None

Setting a URL string as cloneAction

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 Result is "ERROR" (required in the ERROR case, otherwise a "communication error" will be shown). If this property is present if Result is "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).

Setting a Function as cloneAction

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();
            }
        });
    });
}

deleteAction

  • Type: URL string or function
  • Default: None

Setting a URL string as deleteAction

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 Result is "ERROR" (required in the ERROR case, otherwise a "communication error" will be shown). If this property is present if Result is "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).

Setting a Function as deleteAction

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();
            }
        });
    });
}

Clone this wiki locally