2016-02-11 55 views
0

我想在我的initComplete函数中触发if语句,但它不起作用。为什么是这样? 这是我的代码:数据表初始化完成回调没有触发

var dataTable = $('.datatable-column-search-inputs').DataTable({ 
 
initComplete: function() { 
 
        var myDate = new Date(); 
 
        var n = myDate.getHours(); 
 
        
 
        if (n < 12) { 
 
         
 
         dataTable.fnFilter(this.value, 'DESAYUNOS'); 
 
         alert("LOGRO DESAYUNOS"); 
 
        } 
 
        else /* 12:00pm-5:59pm */ 
 
         if (n >= 12 && n <= 17) { 
 
          dataTable.fnFilter(this.value, 'COMIDAS'); 
 
          alert("LOGRO COMIDAS"); 
 
         } 
 
         else /* 6:00PM-12:00AM*/ 
 
          if (n > 17 && n <= 24) { 
 
           $('#sel2').change(function() { 
 
            dataTable.fnFilter(this.value, 'CENAS'); 
 
            alert("LOGRO CENAS"); 
 
           }); 
 
          } 
 
          else /* the hour is not between 0 and 24, so something is wrong */ { 
 
           alert("I'm not sure what time it is!"); 
 
          } 
 

 
        }); 
 
        
 

 
       } 
 
    });

HTML表:

<table class='table datatable-column-search-inputs table-hover table-striped table-bordered'> 
 

 
            <thead> 
 

 
             <tr> 
 

 
              <td class='thead_search'>Platillo</td> 
 
              <td class='thead_select'>Pdv</td> 
 
              <td class='thead_select'>Rid</td> 
 
              <td>PV</td> 
 
              <td>1</td> 
 
              <td>2</td> 
 
              <td>3</td> 
 

 
              <td>Total</td> 
 
              <td class='text-center'>Venta</td> 
 

 
             </tr> 
 
            </thead> 
 
            <tfoot> 
 
             <tr> 
 
              <th colspan="4" style="text-align:left">Total:</th> 
 
              <th></th> 
 
             </tr> 
 
            </tfoot> 
 

 
           </table>

我只是说表的HTML sintax,该表由填充一个ajax调用,它填充正确,但我的IF语句在initComplet e:从不发送警报,不管是什么时候都不是最后的其他人 为什么不触发回调函数?

+0

你可以添加包含表格的html部分吗? – gen

+0

你刚刚添加了表格代码,所有if,它们中的更多者发送警报,这意味着更接近他们@NisseEngström –

+0

只是一个猜测,但是你可以添加一个id到你的表并初始化id上的数据表? – gen

回答

0

fnFilter()是较旧的jQuery数据表1.9的API的方法,但可变dataTableDataTable()一个实例,其提供了访问较新的API仅(1.10+)。

要么将​​DataTable()替换为dataTable(),要么使用更新的API方法search()而不是fnFilter()

第二个参数fnFilter()应该是一个整数(从零开始的列索引),请参阅older API reference

+0

但问题仍然存在,如果if语句被忽略,if语句触发器中的警报 –