TableTools data tables do not work with two tables

I'm using TableToolsand DataTables v1.10on the same page.

My main page has a table and an empty div for modal ones.

<div id="resultDiv">
  <table id="mainTable"> ... </table>
  <div id="detailModal">
    <div id="detailModal-content"></div>
  </div>
</div>

<script>
$(document).ready(function () {
  var mainTable = $('#mainTable').DataTable({
    "dom": 'T<"clear">lrtip',
    "tableTools": { ... },
    "columns": [
      {
        "data": null,
        "render": function(data, type, row, meta) {
          return '<a href="" onClick="return loadDetail(' + data.id + ')">Details</a>';
        }
      },
      ....
    ],
    ........
  });
});

function loadDetail(id) {
  $.ajax({
    async: false,
    url: ...,
    success: function(respose) {
      var tableInstance = TableTools.fnGetInstance('detailTable');
      console.log(tableInstance); //null
    }
  });      
}
</script>

On a separate details page, there is another table that appears in the detailModal-contentdiv.

<table id="detailTable">

</table>

<script>
$(document).ready(function () {
  var mainDetailTable = $jq11('#detailTable').DataTable({
    "dom": 'T<"clear">ltipr',
    "tableTools": { ... },
    ..............
  });
});
</script>

Here, the first TableToolsof mainTableworks fine, but for the second table it does not work (I can click the button, but clicking on it does not create the xls file). I am trying to solve this problem by calling fnResizeButtons()after creating the table suggested here . But tableInstanceit is null.

Any suggestion?

+4
source share
3 answers

, TableTools ( DataTable), , ?

, , , - Flash, , , TableTools, , :

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    var target_id = $(e.target).attr("href");
    var jqTable = $(target_id).find("table");
    var oTableTools = TableTools.fnGetInstance( jqTable[0] );
    if (oTableTools != null && oTableTools.fnResizeRequired()){
        /* A resize of TableTools' buttons and DataTables' columns is only required on the
         * first visible draw of the table
         */
        jqTable.dataTable().fnAdjustColumnSizing();
        oTableTools.fnResizeButtons();
    }
});

, shown.bs.modal , TableTools.

, .

+2

loadDetail() <div id="detailModal-content"></div>, .

loadDetail() :

function loadDetail(id) {
   $.ajax({
      async: false,
      url: '/script.php',
      data: { 'id' : id },
      success: function(response){
         $('#detailModal-content').html(response);
      }
  });
}
+1

Watch out if you table header is already a table, then use jqTable [1] or even better jqTable.get (1)

0
source

All Articles