Reload all data send id and json

Reload all data send id and json

Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

Hello,
I try to refresh the table to each click in which a parameter is sent, I honestly do not know how to do that process.

The following code does what I need but I'm not sure it's the right one.

      $('.rsel').click(function(e){
        $('#wlista').css('display','block');
        idexp = $(this).attr('id');
        $('#wlista').find('h2').text($($('figcaption span')[idexp]).text());

        if(table != null) {
          table.destroy();
        }

        table = $('#datatable').DataTable({
            "autoWidth": false,
            "order": [[ 0, "desc" ]],
            "processing": true,
            "ajax": {
              "url":"<?php echo base_url() ?>documentos/getDocExpAjax",
              "type": "POST",
              "data": {
                id_expediente: idexp
              },
            },
            "columns": [
              { "data": "id_documento" },
              { "data": "identificador" },
              { "data": "nombre_documento" },
              { "data": "productor" },
              { "data": "estado"},
              { "data": "accion"}
            ],
            "columnDefs": [
              {
                "targets" : [5],
                "visible" : ("<?php echo $this->session->userdata('codigo') ?>" == 'AD') ? true : false,
                "searchable" : ("<?php echo $this->session->userdata('codigo') ?>" == 'AD') ? true : false,
              }
            ],
            "language":
              {
                "url": "<?php echo base_url('vendors/datatables.net/i18n/Spanish.json') ?>"
              },
        });
      });

Answers

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Yes, you don't really want to destroy the table on every click and then recreate it. Use ajax.reload() and make ajax.data a function. The documentation shows an example of it with a function, and it basically executes that function whenever the ajax.reload() method is called.

    Allan

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Thanks for answering.

    I would need to reload for each sending of the parameter, what would the code look like?

    var table = $('#example').DataTable();
     
    table.ajax.reload( function ( json ) {
        $('#myInput').val( json.lastInput ); //Here you should send the parameter?
    } );
    

    I have in mind that the sending of parameters is as follows:

      "ajax": {
        "url": "data.json",
        "data": {
            "user_id": 451
        }
      }
    
  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    You'd uise the ajax.data option as a function as I mentioned above:

    "ajax": {
      "url": "data.json",
      "data": function ( d ) {
          d.user_id = 451;
      }
    }
    

    The 451 could be replaced with a variable or something that you read from the document. It will be evaluated every time you call ajax.reload().

    Allan

This discussion has been closed.