Laravel和datatables - 如何使用复选框过滤结果

时间:2017-12-28 06:50:35

标签: php laravel checkbox datatable

我想知道如何使用复选框过滤数据表中的结果,而不是使用搜索特定的coulmns? 请查看屏幕截图,因此我想显示选中值为DEBIT的复选框的所有行,并且详细信息列中的文本为" DEBIT"。 如果我选中了复选框" CREDIT"将做同样的事情并结合结果,因此如果列"详细信息"将显示行。有价值" CREDIT"和#34; DEBIT",我希望这是由ajax完成的。 enter image description here

    <label>CREDIT</label> 
  <input type="checkbox" id="chk-20" >
  <label>DEBIT</label> 
  <input type="checkbox" id="chk-21" >

   <table id="users-table">
    <thead>
     <tr>
  <td>Details</td>
  <td>Description</td>
   </tr>
  </thead>
 </table>

的javascript

    <script type="text/javascript">
      var oTable = $('#users-table').DataTable({
            dom: 'flBrtip',
            stateSave: true,
            paging:     true,
            pagingType: 'simple_numbers',
            lengthMenu: [ [10,15, 30, 50, -1 ], [ 10,15, 30, 50, "All" ] ],
            processing: true,
            serverSide: true,
            ajax: {
                 url: 'custom-filter-data',
                    data: function(d) {
                          d.start_date = $('input[name=start_date]').val();
                             d.end_date = $('input[name=end_date]').val();
                          }
                      },
               columns : [
       {data: 'details', name: 'details'},
         {data: 'description', name: 'description'},        
        ],
        });
   oTable.draw();
  </script>

控制器

    public function getCustomFilterData()
    {
     $arrStart = explode("/", Input::get('start_date'));
    $arrEnd = explode("/", Input::get('end_date'));
    $start = Carbon::create($arrStart[2], $arrStart[0], $arrStart[1], 0, 0, 0);
    $end = Carbon::create($arrEnd[2], $arrEnd[0], $arrEnd[1], 23, 59, 59);

    $orders = Checks::between($start, $end);

    return Datatables::of( $orders )

        ->make( TRUE );
      }

模型

   public function scopeBetween($query, Carbon $from, Carbon $to)
     {
    $query->whereBetween('postingdate', [$from, $to]);
      }

2 个答案:

答案 0 :(得分:0)

如果上面显示的复选框与Datatable绑定,那么您可以获取数据表搜索请求变量中的复选框值,我确实喜欢这样。

public function getCompletedGroupSurveyData(Request $request){

    $search         = $request->get('search')['value'];

    return Datatables::of($response)
        ->filter(function($response) use ($search) {
            if(!empty($search) ) { // problem clause
                $response->whereRaw("CONCAT(first_name,' ',middle_initial,' ',last_name) like ?", ["%{$search}%"]);
            }
        })
  

('search')['value']是从前端/ JS传递到后端的数据表的默认变量。

如果您的复选框是基于自定义的,则可以将复选框值传递到Datatable Data{}对象。例如:寻找此行 d.notification_status_pn

"ajax": {
    'url':"{!!route('get-push-notification')!!}",
        'data':function(d){
        d.notification_frequency_pn     = $('#notification_frequency_pn').val(),
        d.notification_status_pn        = ($('#notification_status_pn').is(':checked')?0:1);

    }
},
"columns": [
    {data: 'status',          name: 'status',"width":"2%", 'className':'text-center'},
    {data: 'delete',          name: 'delete',"width":"1%"},
]

所以现在在后端,我抓住像这样的复选框值

  $notification_type_pn       =   $request->input('notification_type_pn',0);
  $notification_status_pn     =   $request->input('notification_status_pn',1);

  $notification               =     PushNotification::orderBy('date', 'ASC');
  $notification->with(['notificationAppType', 'notificationCategory','notificationRepeats']);
  $notification->where('status',$notification_status_pn);

  return Datatables::of($notification){....}

答案 1 :(得分:0)

  <input onchange="filterme()" type="checkbox" name="type" value="credit">credit
  <input onchange="filterme()" type="checkbox" name="type" value="check">check
 <input onchange="filterme()" type="checkbox" name="type" value="DEBIT">DEBIT

  function filterme() {
 var details = $('input:checkbox[name="type"]:checked').map(function() {
return '^' + this.value + '\$';
 }).get().join('|');
otable.fnFilter(details, 0, true, false, false, false);

 }