| |

PHP Advanced Datatable in Laravel >= 5.0


Autocomplate filter:
It's used to filter data by one value, and this value is searched in the same data or other.

IDUsernameFirstnameLastnameEmail


The datatable class contians "addfilter(filed_name,'autocomplete',label,operator)" method to add new select filter


    $dt->addfilter(filed_name,'autocomplete',label,operator);


           operator takes the following values:    =
                                                    <
                                                    >
                                                    >=
                                                    <=
                                                    !=
                                                    like

How to create it?

  1. call "addfilter" method in the action of your controller: $dt->addfilter('filed_name','autocomplete',label,operator);
  2. create end point method in your datatable class with name: "respond_filters_autocomplete_" as prefix and field_name as suffix
  3. "respond_filters_tag" must be called by "respond_filters_tag_filed_name" as following example:
    
           static function respond_filters_autocomplete_filed_name($search){
                //can be fetched from database
                return parent::respond_filters_tag([1=>'Ahmad',2=>'Ali']);
            }
    
            

Note:

respond_filters_tag requires a two-dimensional array (key,value)


For example:
in the action of your controller put the following code:
    /* in the action of your controller put the following code:*/

    $dt = new Your/Datatable/Class(
            array(
                'firstname' => 'Firstname',
                'lastname' => 'Lastname'
            ));
    $dt->addfilter('username','autocomplete','Username','like');
    return $dt->display('Your/Blade/Template',$request);

in your datatable put the following code:

    static function respond_filters_autocomplete_username($search){

        $records=\DB::table('mdl_user')->where('username', 'like', '%'.$search.'%')->get();
        $d=[];
        foreach ($records as $record){
            $d[$record->username]='User: '.$record->username;
        }
        return parent::respond_filters_tag($d);
    }


in your blade template where you want your datatable appears put the following code:
	@include('phpdt::datatable_view')
	@section('datatable_section')
		@parent
	@show
        


By: Mohammad Alaa Aldeen
Git: https://github.com/mhdalaaaldeen