2016-10-05 58 views
0

我正在創建一個DataTables表,其中數據來自AJAX調用,並且加載正常。搜索也很好。當我爲多過濾器選擇添加代碼時,會顯示下拉列表,但它們是空的。我正在爲jQuery和DataTables使用最新的庫。DataTables multi-filter-select not populate dropdown

JS

function overview() { 
    $.ajax({ 
     url: "/_inc/_ajax/ajax.tables.php", 
     dataType: 'json', 
     success: function(results) { 

     var table = $('#overviewTable').DataTable 
     ({ 
      initComplete:  function() { 
            this.columns().every(function() { 
             var column = this; 
             var select = $('<select><option value=""></option></select>') 
              .appendTo($(column.footer()).empty()) 
              .on('change', function() { 
               var val = $.fn.dataTable.util.escapeRegex(
                $(this).val() 
               ); 

               column 
                .search(val ? '^'+val+'$' : '', true, false) 
                .draw(); 
              }); 

             column.data().unique().sort().each(function (d, j) { 
              select.append('<option value="'+d+'">'+d+'</option>') 
             }); 
            }); 
           } 
     }); 

     table.clear(); 
     for(var i = 0; i < results["id"].length; i++) { 
      table.row.add(
       [ 
        results["id"][i], 
        results["title"][i], 
        results["Tier"][i], 
        results["region"][i], 
        results["2016"][i], 
        results["2017"][i], 
        results["Letter_Type"][i], 
        results["Change_Type"][i], 
       ] 
      ).draw(); 
     } 
    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     alert("ERROR: " + xhr.responseText + " - " + thrownError); 
    } 
}); 
} 

HTML

<table id="overviewTable" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
    <thead>'; 
     <tr> 
      <th>ID</th> 
      <th>TITLE</th> 
      <th>TIER</th> 
      <th>REGION</th> 
      <th>2016</th> 
      <th>2017</th> 
      <th>LETTER TYPE</th> 
      <th>CHANGE TYPE</th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
      <th>ID</th> 
      <th>TITLE</th> 
      <th>TIER</th> 
      <th>REGION</th> 
      <th>2016</th> 
      <th>2017</th> 
      <th>LETTER TYPE</th> 
      <th>CHANGE TYPE</th> 
      </tr> 
     </tfoot> 
</table>'; 

回答

0

我設法弄清楚爲什麼它不工作。我不得不使用DataTables使用的ajax語法。更多信息請參見DataTables。下面是代碼:

JS

function overview() { 
var table = $('overviewTable').DataTable 
({ 
    "ajax":   { 
     "url": "/_inc/_ajax/ajax.tables.php", 
     "dataSrc": "", 
    }, 
    "columns":  [ 
     { "data": "id" }, 
     { "data": "title" }, 
     { "data": "tier" }, 
     { "data": "region" }, 
     { "data": "2016" }, 
     { "data": "2017" }, 
     { "data": "Letter_Type" }, 
     { "data": "Change_Type" }, 
    ], 
    initComplete: function() { 
     var columnsCustom = [2, 3, 6, 7]; 
     var columnNames = ["Tier", "Region", "Letter Type", "Change Type"]; 
     var columnId = ["tier", "region", "letterType", "changeType"]; 
     for (var i = 0; i < 4; i++) { 
      this.api().columns(columnsCustom[i]).every(function() { 
       var column = this; 
       var select = $('<label for="' + columnId[i] +'" class="sr-only">' + columnNames[i] +'</label><select id="' + columnId[i] +'"><option value=""></option></select>') 
        .appendTo($(column.footer()).empty()) 
        .on('change', function() { 
         var val = $.fn.dataTable.util.escapeRegex(
          $(this).val() 
         ); 

         column 
          .search(val ? '^' + val + '$' : '', true, false) 
          .draw(); 
        }); 

       column.data().unique().sort().each(function (d, j) { 
        select.append('<option value="' + d + '">' + d + '</option>') 
       }); 
      }); 
     } 
    }, 
}); 

}

HTML

<table id="overviewTable" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
<thead>'; 
    <tr> 
     <th>ID</th> 
     <th>TITLE</th> 
     <th>TIER</th> 
     <th>REGION</th> 
     <th>2016</th> 
     <th>2017</th> 
     <th>LETTER TYPE</th> 
     <th>CHANGE TYPE</th> 
    </tr> 
</thead> 
<tfoot> 
    <tr> 
     <th>ID</th> 
     <th>TITLE</th> 
     <th>TIER</th> 
     <th>REGION</th> 
     <th>2016</th> 
     <th>2017</th> 
     <th>LETTER TYPE</th> 
     <th>CHANGE TYPE</th> 
     </tr> 
    </tfoot> 

相關問題