Ajax数据表多列搜索

时间:2018-11-04 09:40:14

标签: javascript php ajax datatable

我是js和datable的新手,我很难将单列搜索转换为多列搜索。这是我的代码:

        var dataTable = $('#product_data').DataTable({
         "processing":true,
         "serverSide":true,
         "order":[],
         "ajax":{
            url:"fetch.php",
            type:"POST",
         },
         "columnDefs":[
            {
             "targets":[0,5,7],
             "orderable":false,
            },
         ],
        });

         function load_data(is_suppliers)
         {
          var dataTable = $('#product_data').DataTable({
           "processing":true,
           "serverSide":true,
           "order":[],
           "ajax":{
            url:"fetch.php",
            type:"POST",
            data:{is_suppliers:is_suppliers}
           },
           "columnDefs":[
            {
             "targets":[0,5,7],
             "orderable":false,
            },
           ],
          });
         }

         $(document).on('change', '#supplier_filter', function(){
          var supplier = $(this).val();
          $('#product_data').DataTable().destroy();
          if(supplier != '')
          {
           load_data(supplier);
          }
          else
          {
           load_data();
          }
         });

我使用的查询是:

$query = "
SELECT * FROM products
INNER JOIN suppliers
ON suppliers.supplierid = products.supplier
";
$query .= " WHERE ";
if(isset($_POST["is_suppliers"]))
{
 $query .= "products.supplier = '".$_POST["is_suppliers"]."' AND ";
}

if(isset($_POST['search']['value']))
{
 $query .= '
 (productname LIKE "%'.$_POST['search']['value'].'%"
 OR category LIKE "%'.$_POST['search']['value'].'%")
 ';
}

if(isset($_POST['order']))
{
 $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
 $query .= 'ORDER BY productid DESC ';
}

$query1 = '';

if($_POST['length'] != -1)
{
 $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

(已编辑-添加了完整查询,但执行和获取部分除外)我尝试复制函数并将值替换为我想要的列,并且还向查询添加了另一个if(isset),但仅一个列搜索将起作用其他将不显示匹配记录。

0 个答案:

没有答案