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

问题描述 投票:-3回答:2

我想知道如何使用复选框过滤数据表中的结果而不是使用搜索特定的coulmns?请查看屏幕截图,因此我想显示所有行,其中复选框的值为DEBIT,并且详细信息列中的文本为“DEBIT”。此外,如果我选中复选框“CREDIT”将执行相同的操作并合并结果,因此如果列“详细信息”具有值“CREDIT”和“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]);
      }
php laravel checkbox datatable
2个回答
0
投票

如果上面显示的复选框与Datatable绑定,那么您可以在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){....}

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);

 }
© www.soinside.com 2019 - 2024. All rights reserved.