How can I get the selected row data outside the table?

How can I get the selected row data outside the table?

Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0
edited October 2022 in Free community support

Hi:

I have a table that only acceppts 1 selection.

I´m having trouble figuring out how to get the selected row data outside the table.

console.log(table.DataTable().rows( { selected: true } ).data().toArray())

I´m receiving the entire datatset, instead of only the the selected one.

Can someone help?

Thank´s

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955
    edited October 2022

    I copied your code into this test case and it works as expected:
    http://live.datatables.net/hugutaju/1/edit

    You have table.DataTable() so i assume table is the jQuery selector for the HTML table, ie, $("#example"). If this doesn't help please update the test case to replicate your issue.

    Kevin

  • Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

    This is a React JS App

    I grab a reference (tableRef) and then I use the current value of the reference.

        const tableRef = useRef()  
        const table = $(tableRef.current)    
    

    After the html table is rendered, (useEffect) I call the setUpDataTable() function that looks like this:

        function setUpDataTable() {                             
            table.DataTable({    
                "dom": '<"row col-12">t<"row col-12">',        
                "language": languageStrings,
                "responsive": true,          
                "order": [[ 0, "asc" ]],
                //"columnDefs": getRowButtonsDefs(),
                "destroy": true,
                "paging": true,
                "keys": true
            })
    
            table.DataTable().on('dblclick', 'tr', function () {
              table.DataTable().$('tr.selected').removeClass('selected')
              $(this).addClass('selected')   
            })
     
            setAfterTableSetup(true)         
          }  
    

    I haven´t figured out yet.

    As mentioned before, I get always the entire dataset.

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955

    Oh, it doesn't look like you are using the Select extension. The {selected:true} selector-modifier is used with the Select extension. Either use the Select extension if you want to use {selected:true} or change to use the row-selector as a string being the classname which is a jQuery selector.

    Kevin

  • Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

    Sorry Kevin.

    I didn´t understand the last part.

    "...or change to use the row-selector as a string being the classname which is a jQuery selector."

    Can you explain it?

    Also, I want to select the rows using double click.

    Which solution would be closer to be a "native" one?

    Changing the code in order to use the extension or change the function that should get the selected record?

    Thank you very much in advance.

    P.S. I´m javascript, react js newbie, but learning fast.

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955
    Answer ✓

    or change to use the row-selector as a string being the classname which is a jQuery selector."

    Try this:

    console.log(table.DataTable().rows( ".selected" ).data().toArray())
    

    Also, I want to select the rows using double click.

    Then you probably won't want the select extension. If what you ahve works stick with it :smile:

    Kevin

  • Leo BrunoLeo Bruno Posts: 19Questions: 3Answers: 0

    It worked.

    Thank you Kevin!

  • fhuofhuo Posts: 7Questions: 0Answers: 0

    Hy Bruno
    can you place the full coding to understand what you done?
    Thans a lot

Sign In or Register to comment.