2017-09-14 107 views
0

我已经找到了一个解决方案,但我没有清楚地理解它不工作的原因。你可以帮我吗 ?Jquery Datatable - 选择器抛出表/行undefined

这是我的代码,它抛出一个表未定义的错误,当我改变var data = window.table.row($(this)).data(),以var data = window.table.row($(this)).data(),误差变化

0x800a138f - JavaScript runtime error: Unable to get property 'row' of undefined or null reference

<hr /> 
<div class="row"> 
    <div class="col-xs-12"> 
     <table class="table table-striped table-bordered nowrap" id="calculation-table"> 
      <thead> 
       <tr> 
        <td>Beneficial Owner</td> 
        <td>Account</td> 
        <td>Country</td> 
        <td>Currency</td> 
        <td>Year</td> 
        <td>Updater</td> 
        <td>Updated</td> 
        <td>Output</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <th>Beneficial Owner</th> 
        <th>Account</th> 
        <th>Country</th> 
        <th>Currency</th> 
        <th>Year</th> 
        <th>Updater</th> 
        <th>Updated</th> 
        <th>Output</th> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     //Calculation.init(); 
     $('#calculation-table').DataTable(
      { // set server side processing to true 
       bServerSide: true, 
       // set controller responsible for sorting and paging 
       sAjaxSource: "CalculationTest/AjaxHandler", 
       // show processing is happening while getting data 
       bProcessing: true, 
       sPaginationType: 'full_numbers', 
       iDisplayLength: 15, 
       lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]], 
       columns: [ 
        { data: "BeneficialOwner" }, 
        { data: "Account" }, 
        { data: "Country" }, 
        { data: "Currency" }, 
        { data: "Year" }, 
        { data: "Updater" }, 
        { data: "Updated".toString() }, 
        { 
         "targets": -1, 
         "data": null, 
         "defaultContent": "<button class='btn btn-primary'>Download File</button>" 
        } 
       ], 
       "language": { 
        "search": "", 
        "searchPlaceholder": "Search..." 
       } 
      }); 

     $('#calculation-table > tbody').on('click', 'button', function() { 
      var data = table.row($(this)).data(); 
      var url = '/Calculation/DownloadCalculation'; 
      $.ajax({ 
       type: "GET", 
       url: url, 
       data: { id: data[5] }, 
       dataType: "html", 
       success: function (data) { 
        //put your code here 
       } 
      }); 
     }); 

    }); 
</script> 
+0

'var window.table = $('#calculation-table').DataTable(...'并确保匹配你的'columns'部分的列数。 – davidkonrad

回答

0
<td> is for <tbody> 
<th> is for <thead> 

你已经在你的HTML

交换
+0

谢谢!,我没有注意到。我改变了它应该如何,但我仍然得到相同的错误 –

+0

你有没有计算你的col号码为每一行? Datatable非常紧迫,它希望拥有与cols数量相同的完美表格,完美的语法,无错误的字符等。 – GGO

+0

包含在您的javascript代码中,属性'columns' – GGO