Is there a simple way to get row().data.DT info from datatable field type?

Is there a simple way to get row().data.DT info from datatable field type?

Stacey1134Stacey1134 Posts: 112Questions: 20Answers: 0

Ok, after three days I am totally stumped.

the 'datatables' field type comes in three flavors it seems, but the don't work in tandem? Maybe I am missing something. At this point I just need to get the contact ID number from the row the user selects from the datatable field type. Options pair will always be blank because this is only used when adding new records.

Is there a way to do something like this ?

var table = $('#example').DataTable();
 
$('#example tbody').on( 'click', 'tr', function () {
    console.log( table.row( this ).data() );
} );

without resorting to creating a separate table as listed in this example:
https://editor.datatables.net/examples/datatables/parentChild.html

I am using the new/edit function of the datatables fieldtype because a user may have to add a new contact. That part works fine!, I just cannot grab that info so I can complete the rest of the entry.

Thanks as always.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The equivalent of your code for a datatable field type would be:

    editor.field('myTable').dt().on('click', 'tr', function () {
        console.log( table.row( this ).data() );
    });
    

    That uses the dt() method of the field type to get the DataTable API instance for that table, and on() to handle the click listener.

    Does that do what you need? I'm not quite clear on what you mean by the "three flavours" that the field type would come in?

    Allan

  • Stacey1134Stacey1134 Posts: 112Questions: 20Answers: 0
    edited August 2023

    So, er, thanks. That worked great. I also realized that options pair does indeed work as intended. Once I made the database name explicit in the field for "Datatable_Table_4", I could save the id from the datatable field type.

    Argh! It's a little confusing when in one example the fieldtype works as a select, another is an editable lookup table, and the most recent a sort of parent-child table set-up. And none of the examples explicitly name the database, (That's the three flavors I mentioned earlier) but I finally get it now.....

    **Awesome fieldtype by the way, the click event made it even more useful. **

    Thanks a bunch :smile:

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    We get there in the end :).

    I like the DataTable field type - I usually treat it as a select list myself, but it does offer a lot more utility it needed.

    Allan

Sign In or Register to comment.