首页 > 解决方案 > 隐藏列后数据表中出现错误

问题描述

在我的表单中,我有一个带有 2 个选项的选择框:当您选择其中一个时,我的 DataTable 必须更改他的列:

//Changing TYpe of Price level will change datatable
$('#price_method').on("change", function(){
  //Declare the variable
  var oTable=$('#item_pricelvl').DataTable();
  oTable.clear();
  //Destroying the table and then rebuilding it again
  oTable.destroy();
  //If Selected Price Override			
  if ($(this).val()=="PO"){
    //I gotta hide a column
    $('#item_pricelvl').DataTable({
      "columnDefs":[
        {
          "class":"details-control",				
        },
        {				
          "targets":['_all'],
          "orderable":false
        },
        //I'm hiding the 4th column 
        { "visible": false, "targets": 4 },
        "Column"
      ],
    })
  }else{ //Else I can show all columns
    $('#item_pricelvl').DataTable({
      "columnDefs":[
        {
          "class":"details-control",				
        },
        {				
          "targets":['_all'],
          "orderable":false
        },					
        "Column"
      ],
    })	
  }		
});

//INSERT NEW PRICE LEVEL INTO THE ITEM PRICE TABLE
$('#insertplvl').click(function(){
  //Checking what method is selected
  if ($('#price_method').val()=="CM"){
    //starting to add the new row into the DataTable, and No columns hiding
    it_det.row.add([			
    "D",
    "COST MARKUP",
    "1",
    "999999999",
    "0.00",
    "0.00",
    "<input type='checkbox' name='chkdet' class='chk_plvl' id='"+ct_row+"'/>"
    ]).draw(false);
  }else{
    //starting to add the new row into the DataTable, and adding just 6 values because of the hide column 
    it_det.row.add([			
    "D",
    "PRICE OVERRIDE",
    "1",
    "999999999",			
    "0.00",
    "<input type='checkbox' name='chkdet' class='chk_plvl' id='"+ct_row+"'/>"
    ]).draw(false);
  }
  //Increasing the counter
  ct_row++;
})
<table id="item_pricelvl" class="table table-bordered table-striped">
  <thead>
    <tr>												
      <th>Price Level</th>
      <th>Pricing Method</th>									
      <th>From Quantity</th>
      <th>To Quantity</th>									
      <th>Markup Amount</th>									
      <th>Unit Price</th>
      <th><input type="checkbox" class="checkall" /></th>												
    </tr>
  </thead>																	
</table>

当我尝试插入选择“POP”时,出现此错误:

DataTables 警告:表 id=item_pricelvl - 请求第 0 行第 6 列的未知参数“6”。有关此错误的更多信息,请参阅http://datatables.net/tn/4

下面我将粘贴此页面的屏幕 在此处输入图像描述 因此,如果它与所有列一起使用,为什么当我将列不可见时会出现错误?

标签: javascriptphpjqueryhtmldatatables

解决方案


推荐阅读