How to generate rowId client side?

How to generate rowId client side?

dtdevdtdev Posts: 9Questions: 4Answers: 0
edited July 2016 in Free community support

The jQuery DataTables rowId reference shows an example of setting the rowId option to a column from the data source (server side). This setting is used for the "select" extension and retaining row selection on Ajax reload.

Is there any way to generate the row identifier value in one of the following ways?
1. Client side, or
2. As a combination of multiple columns from the data source, or
3. With static prepended text?

Example data source:

{
    "data": [
    {
        "aid": 5421,
        "bid": 4502,
        "name": "John Smith"
    }
}

Code:

$("#datatable").DataTable({
    select: true,
    //rowId: "aid" each row ID is the value of the "aid" column
    //       e.g., <tr id="5421">

    //rowId: 0 each row ID is the value of the 0-indexed column
    //       e.g., <tr id="5421"> (same as above)

    rowId: [0, 1] // How? row ID combined value of 2+ columns
                  // e.g. <tr id="5421-4502">

    rowId: "random" // How? random generated client-side ID
                    // e.g., <tr id="id34e04">

    rowId: {
        prepend: true,
        content: "id",
        column: "aid"
    } // How? prepend text to id
       // e.g., <tr id="id5421">
});

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    Using createdRow is probably the way to go here - it provides complete flexibility since it is just a Javascript callback function, so you can add the id attribute to the row and use whatever data you want.

    Allan

  • dtdevdtdev Posts: 9Questions: 4Answers: 0

    Using the createdRow option to set each row's id attribute doesn't allow row selection to be retained upon ajax.reload. Is there any other way to do this? So far it seems that row selection can only be retained if the rowId option is set appropriately, but this only involves server-generated row IDs, not client side.

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin
    Answer ✓

    Yes, unfortunately that wouldn't work this way since it is just setting the id attribute rather than interfacing with DataTables' row id handling. Unfortunately the only way would be to modify the data (using ajax.dataSrc for example) to include the id that you want, and then set rowId to read from that newly created id property.

    Allan

This discussion has been closed.