2016-11-04 127 views
0

我有服務器端數據表。我有多列搜索問題,所有列的工作,如果我只搜索一個列。但如果我多列搜索,過濾器沒有work.example.if我只搜索「名稱」過濾工作,如果我只搜索一個start_date過濾器工作too.but如果我同時搜索名稱和日期.filter沒有工作。 所以在這裏我的代碼:多列搜索數據表

//my script: 
 
var table = $('#dataTables'); 
 

 
// begin first table 
 
    table.dataTable({ 
 

 
    "ajax": { 
 
      "url": "<?php echo site_url('leader') ?>", 
 
      "type": "POST", 
 
      "data": function(d) { 
 
      d.full_name = $('#full_name').val(); 
 
      d.start_date = $('#start_date').val(); //whats right code? 
 
      d.end_date = $('#end_date').val(); //whats right code? 
 
         
 
      }, 
 
      }, 
 
     ..... 
 
     ..... 
 
     $('#btn-search').click(function(){ 
 
      var data_tables =   $('#dataTables').DataTable(); 
 
      data_tables 
 
       .search(this.value) 
 
       .draw(); 
 
     })
<!-- my view: --> 
 
<div class="form-inline"> 
 
<div class="form-group"> 
 
<input type="text" name="full_name" class="form-control" id="full_name" placeholder="Name"/> 
 
</div> 
 
        
 
<div class="form-group"> 
 
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy"> 
 
    <input type="text" name="start_date" class="form-control" readonly id="start_date" size="8"> 
 
    <span class="input-group-btn"> 
 
    <button class="btn default" type="button"> 
 
     <i class="fa fa-calendar"></i> 
 
    </button> 
 
    </span> 
 
</div> 
 
</div> 
 
<div class="form-group"> 
 
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy"> 
 
<input type="text" name="end_date" class="form-control" readonly id="end_date" size="8"> 
 
<span class="input-group-btn"> 
 
<button class="btn default" type="button"> 
 
    <i class="fa fa-calendar"></i> 
 
</button> 
 
</span> 
 
</div> 
 
</div> 
 
<button class="btn btn-warning" id="btn-search">Search</button> 
 
</div>

服務器端:

if ($full_name) 
 
\t \t { 
 
      $where = "And full_name = '$full_name'"; 
 
     } 
 
     else if($start_date) 
 
     { 
 
     \t $where = "And start_date >= to_date('$start_date','DD-MM-YYYY') "; 
 
     } 
 
     else if($end_date) 
 
     { 
 
     \t $where = "And end_date <= to_date('$end_date','DD-MM-YYYY') "; 
 
     } 
 
     
 
     else 
 
     { 
 
     \t $where = " "; //how to combine 3 above 
 
     }

如何創造條件,如果我在數據表多列搜索?

回答

0

您的服務器端代碼需要改變 嘗試是這樣的:在阿里納斯

$where = ""; 
if (!empty($full_name)) 
{ 
    $where .= "And full_name = '$full_name' "; 
} 
if(!empty($start_date)) 
{ 
    $where .= "And start_date >= to_date('$start_date','DD-MM-YYYY') "; 
} 

if(!empty($end_date)) 
{ 
    $where = "And end_date <= to_date('$end_date','DD-MM-YYYY') "; 
} 

: 您標記這個問題作爲一個笨的相關問題 - 什麼背後的原因不使用查詢生成器? (你的方法有點「不安全」......)

+0

我在codeigniter中創建上面的代碼,所以很多感謝代碼工作。 – ione