Search code examples
phplaraveldatatablesyajra-datatable

PHP laravel column search is not working efficiently with ->editcolumn getdatatable


Script for search column:

<script>
    $.fn.dataTable.ext.errMode = 'none';
    var tableObj;

    $(document).ready(function(){
        // debugger;
        $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

         tableObj = $('{{ $class_name }}').DataTable({

                searchable: true,
                processing: true,
                serverSide: true,
                 "aaSorting": [],
                cache: true,
                type: 'GET',
                ajax: '{{ $routeValue }}',
                @if(isset($table_columns))
                columns: {!!$setData!!}
                @endif
                initComplete: function () {
                    this.api().columns().every(function () {
                        var column = this;
                        var input = document.createElement("input");
                        $(input).appendTo($(column.header()))
                        .on('keyup', function () {

                            column.search($(this).val(), false, false, true).draw();
                        });

                    });
                }


        });

          $('.datatable').find('select.form-control').removeClass('form-control input-sm').addClass('full-width').attr('data-init-plugin','select2');        
    });
    // console.log("it is working");
  </script>

Controller function to get data:

 $records = array();
   dd($record);
             $records = User::join('roles', 'users.role_id', '=', 'roles.id')

            ->select(['users.id','users.name', 'phone','users.default_bill_address','area','users.created_at','website','user_status','reason_inactive','is_online','login_enabled','role_id',

              'slug'])->orderBy('users.created_at', 'desc');

             if(!isset($_GET['order'])){
              $records = $records ->whereIn('users.id',$list_his)->orderBy('users.created_at', 'desc');
           }
        return Datatables::of($records)
       ->editColumn ('default_bill_address', function($records){
          if($records->default_bill_address)
          {
            $add = Address::where('id',$records->default_bill_address)->first();
            if($add)
                return $add->fullAddress();
            else
                return '-';
         }
         else
            return '-';
        })

Screenshot:

screenshot related to panel where search column is present.

Description

I have edited many columns in the same way from different tables, when I search in the specific column it returns nothing, because related text is not in the query table and I cannot join so many tables to get the result while searching.


Solution

  • Simplest solution i got

    In your script just cahnge a single line serverSide: true to serverSide: false,

    <script>
        $.fn.dataTable.ext.errMode = 'none';
        var tableObj;
    
        $(document).ready(function(){
            // debugger;
            $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
    
             tableObj = $('{{ $class_name }}').DataTable({
    
                    searchable: true,
                    processing: true,
                    serverSide: false,
                     "aaSorting": [],
                    cache: true,
                    type: 'GET',
                    ajax: '{{ $routeValue }}',
                    @if(isset($table_columns))
                    columns: {!!$setData!!}
                    @endif
                    initComplete: function () {
                        this.api().columns().every(function () {
                            var column = this;
                            var input = document.createElement("input");
                            $(input).appendTo($(column.header()))
                            .on('keyup', function () {
    
                                column.search($(this).val(), false, false, true).draw();
                            });
    
                        });
                    }
    
    
            });
    
              $('.datatable').find('select.form-control').removeClass('form-control input-sm').addClass('full-width').attr('data-init-plugin','select2');        
        });
        // console.log("it is working");
      </script>
    

    this will change the server side searching into client-side. search from the content not from the datatable. How silly i was mistaking