如何使用JQuery.DataTables()

时间:2018-10-19 02:28:40

标签: jquery json ajax datatable

我已经在一个页面中创建了许多datable,并加载onchange select事件。

<!-- bootstrap 3-->
<div class="panel-body tabla1 ">  <!-- INICIO -->
    <a href="#item-0" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #1    </a>
    <div class="list-group collapse" id="item-0" data-id="0">


<table id="exampleTablaDinamica0" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-1" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #2  </a>
    <div class="list-group collapse" id="item-1" data-id="1">


<table id="exampleTablaDinamica1" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-2" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #3 </a>
    <div class="list-group collapse in" id="item-2" data-id="2" style="">


<table id="exampleTablaDinamica2" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-3" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #4   </a>
    <div class="list-group collapse in" id="item-3" data-id="3" style="">


<table id="exampleTablaDinamica3" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-4" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i> #5 </a>
    <div class="list-group collapse" id="item-4" data-id="4">


<table id="exampleTablaDinamica4" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-5" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>#6   </a>
    <div class="list-group collapse" id="item-5" data-id="5">


<table id="exampleTablaDinamica5" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-6" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #7 </a>
    <div class="list-group collapse" id="item-6" data-id="6">


<table id="exampleTablaDinamica6" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-7" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i> #8 </a>
    <div class="list-group collapse" id="item-7" data-id="7">


<table id="exampleTablaDinamica7" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
    <!-- INICIO -->
    <a href="#item-8" class="list-group-item" data-toggle="collapse">
      <i class="glyphicon glyphicon-chevron-right"></i>  #9  </a>
    <div class="list-group collapse" id="item-8" data-id="8">


<table id="exampleTablaDinamica8" class="table table-striped table-bordered dt-responsive" style="width:100%">
  <thead></thead><tbody></tbody><tfoot></tfoot></table>



    </div>
  </div>

在同一页面中之后,我将事件堆叠在show.bs.collapse上 当打开折叠时,我不想用唯一的ID填充一个空表并应用datatable响应功能。在测试中,表在打开折叠时保持为空。

    $("body").on('show.bs.collapse',".collapse",  function(){

    var selected = [];
    $('.mesesPresupuesto input:checked').each(function() {
        selected.push($(this).attr('value'));
    });


 urlToLoad = 'acciones/mostrarDatosTablaFechaAjax.php';   

    id = $(this).attr("data-id");

    $.ajax({
        type: 'GET',
        url: urlToLoad,
        data: 'meses='+selected+'&idPresupuesto=1',
        dataType: 'json',
        beforeSend: function () {
        //console.log("bs");
        },
        success:function(data){

          var columns = [];
          columns = (Object.keys(data.data[0]));

        if ( $.fn.DataTable.isDataTable('#exampleTablaDinamica'+ id) ) {
          $('#exampleTablaDinamica'+ id ).DataTable().destroy();
        }


        console.log(data);


          $('#exampleTablaDinamica'+id).DataTable({
              "responsive": true,
              "destroy": true,
              "processing" : true,
              "ajax": {
                  url: urlToLoad,
                  dataSrc : '',
                  contentType: "application/json",
                  data: {
                      "meses": selected,
                      "idPresupuesto": 1
                  }
              },
              "columns" : columns
          }).columns.adjust();

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log("Status: " + textStatus); alert("Error: " + errorThrown);
        }
      });

  });

如何通过JSON在列和行上使所有表负责设置?

0 个答案:

没有答案