Getting the Datatable Server-side ID

Asked

Viewed 313 times

2

I’m using the Datatable Server-side, but I’m not sure how to catch the ID from my table to open the edit and delete pages.

server-side

$table = 'financ_receita';

$primaryKey = 'ID_Receita';

$columns = array(
    array( 'db' => 'ID_Receita',    'dt' => 0 ),
    array( 'db' => 'nome',          'dt' => 1 ),
    array( 'db' => 'valor',         'dt' => 2 ),
    array( 'db' => 'dataVenci',     'dt' => 3 ),
    array( 'db' => 'formaEntrada',  'dt' => 4 ),

);

Table mount

var myTable =
  $('#dynamic-table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "dados.php",
    "columns": [{
        "bVisible": false,
        "bSortable": false
      },
      null,
      null,
      null,
      null,
      {
        data: null,
        className: "center",
        defaultContent: '<a href="editar?id=" class="editor_edit button blue">Editar</a> <a href="excluir?id=" class="editor_remove button red">Del</a>',
        orderable: false,
        bSearchable: false,
      }
    ],
    "language": {
      "url": "/assets/js/dataTables/lang/pt-br.json"
    },
  });

I’d like to put the Id_recipe in the respective links: editar?id= and excluir?id=.

1 answer

1


For dynamic elements, use render instead of defaultContent:

render: function(data){
   return '<a href="editar?id='+data[0]+'" class="editor_edit button blue">Editar</a> <a href="excluir?id='+data[0]+'" class="editor_remove button red">Del</a>'
},

The value in data[0] represents the data in the first column, i.e. ID_Receita.

See what it says to documentation concerning the defaultContent:

This option is available for those use cases - Creating Static content for a column. If you Wish to create Dynamic content (i.e. based on other data in the Row), the columns.render option should be used.

Free translation:

This option is available for cases where the content is static. If wants to create dynamic content (i.e., based on other data from table row), the option columns.render should be used.

  • Good morning Sam. How do I get the render read function onClick="return false;". '<a href="financeiroContaAreceberEpagar.php?id='+data[0]+'&i=3d" class="btn-link no-border red delete-event" data-title="Apagar" data-content="Deseja apagar esse lançamento?" onClick="return false;"><i class="ace-icon fa fa-trash-o bigger-130"></i></a>' +

  • I am organizing in this way https://prnt.sc/o4uay4

  • Good morning. I don’t quite understand the onclick function. You want to override the link redirect?

  • How the data is coming via ajax, and using the render to display the View, Edit and Delete controls, it seems that the return of render is nullifying my onClick="return false;", this function just opens the modal asking if you really want to delete.

  • On the edit button, I have a class called link where she is just to identify that other function $(".link").mouseover(function(){&#xA; var href = $(this).attr('href');&#xA; $('.iframe').attr('src', href)&#xA; }); which passes the href into the src iframe, so that the modal opens the page. For some reason also does not recognize.

  • 1

    Do so: $(document).on("mouseover", ".link", function(){

  • It worked. Thank you Sam

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.