get data from table

get data from table

BaranekBaranek Posts: 3Questions: 2Answers: 0

I dont know get data from table. I use ajax and php for load data from mysqkdatabase. I use paginatoion and filtering too. But I domt know get data from table. I dont have any idea for variabel to use for get data from any field by click in row.

<script>
  $(document).ready(function() {
    $('#datatable').DataTable({
    ordering: false,
    searching: true,
    processing: true,
    serverSide: false,
    ajax: 'php-list-zakaznik.php',
    dataSrc: 'data',
        columns: [
                    { data: 'zak_id' },
                    { data: 'zak_nazov' },
                    { data: 'zak_ulica' },
                    { data: 'zak_mesto' },
                    { data: 'zak_icdph' },
                    { data: 'zak_tel1' },
                    { data: 'zak_tel2' },
                    { data: 'zak_email' },
                    { data: 'zak_poznamka' }
                ],
         
    columnDefs: [
         {             
                targets: [ 0 ],
                visible: false,
                 searchable: false
          }
     ],
    });
         
// Click on row
    $('#datatable tbody').click( function () {
        var table =

        alert( "salary is: ");
    });
  });  
</script>

How I can have data for example first hidden column ID when I click in to table row, please?
Thanks a lot.

PS: your example with otable doesnt work for me.

Or can anybody post full example for this:
- load data from MySQL database
- fucntion filtering, pagination, translating for table
- last row with button
- click on button - open dialog with form for change all values in clicked row and save button
- store all these row updated datas in to datatabase
- refresh table with new updated datas.

Thanks, thanks, thanks....

Answers

  • indymxindymx Posts: 63Questions: 3Answers: 0

    I think what you are looking for is Editor. If you do not currently have Datatables Editor, you will either have to purchase that or build your own editor. I highly recommend Datatables Editor.

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    You don't need Editor to get the data from a DataTable (although don't let me stop you buying it ;)). row().data() should be all that you need:

      $(document).ready(function() {
        var table = $('#datatable').DataTable({
           ...
        });
              
        // Click on row
        $('#datatable tbody').on('click', 'tr', function () {
            var rowData = table.row( this ).data();
    
            alert( "salary is: "+ rowData.zak_id);
        });
      }); 
    

    A few things to note:

    1. You need to assign table as I have done in line 2 above
    2. You should use a delegated event handler for the click
    3. And it should select the tr elements, not the tbody
    4. I've used zak_id, but change as you need.

    Allan

This discussion has been closed.