Get Data from a Selected row the cell

Get Data from a Selected row the cell

bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
edited November 2015 in DataTables

Hello,

i like the value from a cell in a selected row, but it doesn't work.
I like to call the cell by the name, because later i need a hidden value which is not shown in the table - the id.

This is my code

columns: [
    {   "data": "ks_rezept.ks_order_name"       },
        {   "data": "ks_status.ks_status_name"      },
        {   "data": "ks_auftrag_sammel.ks_Stueckzahl" },
    {   "data": "ks_auftrag_sammel.ks_Gewicht_order"   }
    ],
        select: true,
    lengthChange: false,
    buttons: [   {
                extend: 'selectedSingle',
                text:   'Show Order Name'
                action: function ( e, dt, node, config ) {
                        /var sdata = table.cell('.selected', 0).data();   -> this is working
                        var tdata = table.fnGetData('ks_rezept.ks_order_name');
                        alert (tdata) ;
              }

Can anybody help me?

Thanks a lot,

Thomas

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,880Questions: 1Answers: 8,601 Site admin

    var sdata = table.cell('.selected', 0).data(); -> this is working

    The comment nots that this method (cell().data()) is working - but perhaps has only the data for that cell and not the whole row - which perhaps you need? The legacy fnGetData method is not (which I would suggest ignoring since it is a legacy method).

    If you want the data for the whole row use:

    var idx = table.cell('.selected', 0).index();
    var data = table.row( idx.row ).data();
    
    ...
    

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    Hello Allan,

    sorry but I do not find any answers for my question in the documentation.

    Now I take your code - It is working. I get the index of the row. Thx...

    var idx = table.cell('.selected', 0).index();
    var data = table.rows( idx.row ).data();
    

    But now i like to print a value of a cell of the row.

    example table:
    surname : age : land
    Tom        : 33   :  ger
    bill       : 50   : us
    charles    : 60   : eng
    
    -> selected row is bill.
    

    I like only the surname - i tried it with

    alert (data[idx.row][0] ) ;
    alert (data[idx.row]['surname']) ; 
    

    Where can i find an example, how it works? it can't be so difficult.

    Thanks a lot,
    Thomas

  • allanallan Posts: 54,880Questions: 1Answers: 8,601 Site admin

    Simply: data.surname (assuming you are using an object).

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
    edited November 2015

    Hello Allan,

    and here i have a small problem (maybe).
    The name of the column is 'ks_order.ks_surname'
    Because my datasource is a leftjoin (PHP) and so I have a dot in the column-name.

    So must i rename the column as 'tbl_surname' or is there an other way?

    If i will rename the column - how is the right way?

    Field::inst('ks_order.ks_surname AS tbl_surname')
    

    Thx,
    Thomas

  • allanallan Posts: 54,880Questions: 1Answers: 8,601 Site admin

    In which case you should be able to simply use data.ks_order.ks_surname.

    Adding console.log( data ); will print the data out into your browser's console so you can inspect its structure.

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    Hello Allan,

    thanky for your reply. I have done it.

    Now i see the structure, when i click on the 'object' in my browser console.
    it looks like:

    DT_RowId: "row_431"
    ks_order: Object
            ks_name
            ks_surname
            ks_order_age
    ks_receipt: Object
            ks_name
            ks_receipt_status
    

    Now i like to the column 'data.ks_order.ks_surname' but i don't know how.

    var idx = table.cell('.selected', 0).index();
    var data = table.rows( idx.row ).data();
    console.log( data );
    alert ('Surname:' + data.ks_order.ks_surname) ;
    

    But It doesn't work - nothing happen.

    Thx for your help.

    Thomas

  • allanallan Posts: 54,880Questions: 1Answers: 8,601 Site admin
    Answer ✓

    Does it work if you console.log() it? It might be that alert has been disabled for your page. Looking at the console trace you gave I can't see any reason why that data access wouldn't work...

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
    edited November 2015

    Hello Allan,

    what do you mean with console.log() it?

    I tried it now in chrome and mozilla it doesn't work.

    The alert box works alert (test) and it doesn't work with alert ('1: ' + data.ks_order.ks_surname ) ;

    What can I try? Thx a lot,
    Thomas

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
    edited November 2015

    Hello Allan,
    find the Mistake.
    It was a litte Bastart-Mistake... :-(

    buttons: [  
        {
             extend: 'selectedSingle',
             text:   'Produktionsfreigabe' ,
             action: function ( e, dt, node, config ) {
             var idx = table.cell('.selected', 0).index();
             var data = table.row( idx.row ).data();
             console.log( data );
             alert ('1:+ data.ks_rezept.ks_rezept_name' ) ;
         }
    
    

    var data = table.row was wrong - i write var data = table.rows

    Allan - Thanks a lot for your help!

    Thomas

This discussion has been closed.