使用按钮值设置用于数据表过滤的Javascript变量

时间:2018-09-26 07:37:11

标签: javascript datatables

我有一个通过MySql查询动态创建的DataTable。表格数据由“订单ID”和“订单状态”组成

我想使用按钮来过滤数据。这些按钮是通过MySql查询动态生成的,并且可以将这些按钮的值传递给Javascript变量。

我设法获得了用于设置JS变量fired_button的按钮,但是由于某种原因,它没有传递给表过滤器(oTable.fnFilter( fired_button , 2 );请注意,“ 2”是指该列

foreach($orderscount as $order) {
    $buttons .= '<button role="button" onclick="changeTheVariable()" class="btn btn-sm" value="'.$order->order_status.'">'.$order->order_status.' ('.$order->qty.')</button>';
}

echo $buttons;
?>
<table id="example">
    <thead>
        <tr>    
            <th>Order ID</th>
            <th>Status</th>
        </tr>
    </thead>    
    <tbody> 
        <tr>                
            <td></td>
            <td></td>
        </tr>
    </tbody>    
</table>
<script>
    $("button").click(function() {
        var fired_button = $(this).val();
        alert(fired_button);
    });
    $(document).ready(function() {
      var oTable = $('#example').dataTable( {});
      // Sometime later - filter...
      oTable.fnFilter( fired_button , 2 );
    } );
</script>
<?php

}

0 个答案:

没有答案