[DT 10.1] Select row

[DT 10.1] Select row

deliatordeliator Posts: 95Questions: 1Answers: 0

Hi,

How can I programmatically select a row after inserting a new one ? (its has be inserted with an ajax call to the DB)

Thanks

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Are you using row.add() to add the row and the Select extension for selection? If so:

    table.row.add( ... ).select();
    

    will do it.

    Allan

  • deliatordeliator Posts: 95Questions: 1Answers: 0

    Hello Allan,

    No, i 'm not using it.
    I use a client side datatable.

    I add a record in the DB with an ajax call and then as a return success action : $('#myTable').DataTable().ajax.reload();

    The row i will highlight is the one added with my sql.

    Thank you,

    Marc

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    I built this example for something else but you should be able to adapt it for your needs:
    http://live.datatables.net/hatojihe/3/edit

    You will need the row().show() plug-in and the Select extension.

    Click the "Find Quinn" button. The function searches column 0 for Quinn, selects the row and uses the row().show() plugin to jump to the page.

    Kevin

  • deliatordeliator Posts: 95Questions: 1Answers: 0
    edited January 2018

    Error is throwing :

    Uncaught TypeError: oTable.row is not a function
    at HTMLButtonElement.<anonymous> (definitions.js:400)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.r.handle (jquery.min.js:3)

    Maybe because i'm working with server side data ?

    Edit : I've edited a old script you have previously send to me :

    live.datatables.net/tanutevi/1/edit?html,css,js,console,output

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    live.datatables.net/tanutevi/1/edit?html,css,js,console,output

    This one isn't working because the server-side script is throwing an SQL error. That appears to be happening because DataTables is sending -10 as the start point, which appears to be caused by the show plug-in.

    First, and possibly most important question, are you using server-side processing on your actual table? The link above does, but it isn't throwing the error oTable.row is not a function. If you are using server-side processing, that changes things significantly since a client-side API method alone will not be enough.

    Allan

  • deliatordeliator Posts: 95Questions: 1Answers: 0

    Indeed i'm using server side processing on my table.
    What is the way to resolve my issue ?

    Thank you Allan

    Marc

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Edit : I've edited a old script you have previously send to me :

    I made a couple changes to have this script work:
    http://live.datatables.net/tanutevi/3/edit

    Since you defined columns.data you need to change how the data is accessed. The return in your click event needs to look like this:
    return data.first_name === 'Bruno';

    The draw(false) needs to be removed as that is causing the data to be reloaded from the server. Since you are staying on the same page you don't need the draw(false).

    Uncaught TypeError: oTable.row is not a function

    My guess is you are running into the issue discussed in the first FAQ here:
    https://datatables.net/faqs/index#Most-common-FAQs

    Instead of this:
    oTable.row(function ( idx, data, node ) {

    Try this (replacing myTable with your table's ID):
    $('#myTable').DataTable().row(function ( idx, data, node ) {

    Kevin

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Regarding my point about server-side processing, if you have a look at the code for the row().show() plug-in you will see that it needs to get the index of the row in question. However, if you have server-side processing enabled, that row probably won't exist on the client-side (only rows in the current page would, and showing them is redundant).

    What you would have to do is make an Ajax call to the server to say "what page is this piece of data shown on?" You would need to use ajax.params() to let the server know what sorting and filtering is applied to the table (since that can obviously change the location of the row).

    Then when the server tells you where the data is, you would need to use page() to jump to that page.

    Basically its the same as the client-side plug-in does, but with server-side interaction.

    Allan

This discussion has been closed.