Get data from input in table cell using rows().data()

Get data from input in table cell using rows().data()

muhamadyuraz07muhamadyuraz07 Posts: 8Questions: 3Answers: 0

hello, I'm having difficulties operating datatable when fetching data.

var table = $('#example1').DataTable({
            "language": {
                "sProcessing":    "Procesando...",
                "sLengthMenu":    "Mostrar _MENU_ registos",
                "sZeroRecords":   "Não foram encontrados resultados",
                "sEmptyTable":    "Nenhum dado dísponivel nesta tabela",
                "sInfo":          "A mostrar registos de _START_ a _END_ de um total de _TOTAL_ registos",
                "sInfoEmpty":     "Mostrando registos de 0 a 0 de um total de 0 registos",
                "sInfoFiltered":  "(filtrado de um total de registos _MAX_)",
                "sInfoPostFix":   "",
                "sSearch":        "Pesquisar:",
                "sUrl":           "",
                "sInfoThousands":  ",",
                "sLoadingRecords": "A carregar...",
                "oPaginate": {
                    "sFirst":    "Primeiro",
                    "sLast":    "Último",
                    "sNext":    "Seguinte",
                    "sPrevious": "Anterior"
                },
                "oAria": {
                    "sSortAscending":  ": Ative para ordenar a coluna em ordem ascendente",
                    "sSortDescending": ": Ative para ordenar a coluna em ordem decrescente"
                }
            },
            'order': [[1, 'asc']]
        });
$('#example1 tbody').on("change", ".produtochkbox", function () {

                        var i = 0;
            var data = table.rows().data();

            console.log( 'The table has ' + data.length + ' records' );
            console.log( 'Data', data );

            data.each(function(){
                                //count the rows
                console.log(i);

                                //getting all data from the table but in a raw format like an array of objects
                                console.log(data[i]);

                                //getting specific data example ( not working )
                var id = table.cell('.id').data().text();
                console.log(id);

                                i++;
            });
        });

My problem is that the code for getting the specific data that I want isn't working. Please help.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,150Questions: 26Answers: 4,736

    This example is doing something similar and should help. Note there is row().data() to get one row and rows().data() to get multiple rows.

    Kevin

  • muhamadyuraz07muhamadyuraz07 Posts: 8Questions: 3Answers: 0

    Yes, I have seen those examples but my problem is that I'm trying to get specific data, not an array of data.

    In my example there I used this code :

    var id = table.cell('.id').data().text();
    

    witch is not working.
    What I'm aiming there is to select the text of the class '.id' witch is in the data cell.
    Is there a way that I can do that?
    The same goes for input in the table.

    The data that is returning is :

    ["<a href="#" class="id" data-toggle="modal" data-target="#exam…d="45" data-nome="Laborum Fuga At cu">#PROD45</a>", "<img class="responsive image rounded" src="http://…4e77a4e956b1cd9210e064cd1cd5ba0.png" width="100">", "Armanti 02", "Laborum Fuga At cu", "100.00", "<span class="badge badge-success">Dísponivel</span>", "1CX x 1EMB x 4UND", "<input type="number" id="prodquantity-45" value="0…his.value = Math.round(this.value);" disabled="">", "<div class="custom-control custom-checkbox">↵                    …" for="chkboxprodid-45"></label>↵                                      </div>", DT_RowId: "tr-45"]
    

    What I want is :

    #PROD45 // ( from the class '.id' )
    
  • kthorngrenkthorngren Posts: 20,150Questions: 26Answers: 4,736
    Answer ✓

    I have seen those examples but my problem is that I'm trying to get specific data, not an array of data

    For array based data you can do something like row().data()[1] for the data in column 1. Or if using rows().data() you can use pluck() to get the specific column.

    However you can use cell().data() or you may need to use cell().node() to get the td instead of the data. Using table.cell('.id').data() is not specific enough as it doesn't limit to the row that is clicked. In your change event you probably will want something like this:

    var myCol = 2;  // Column number containing the data of interest.
    var tr = $(this).closest('tr');
    var data = table.cell(td, myCol).node();
    

    Then parse the data variable to get what you want. Please see the cell() docs for all the options of selecting the cell.

    If you need further help please build a simple tests case with examples of your data so we can provide more specific help.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • muhamadyuraz07muhamadyuraz07 Posts: 8Questions: 3Answers: 0

    Thank you kthorngren.
    Your answer solved my issue!!
    :)

This discussion has been closed.