shaunakwyn / gTalents

it contains gTalents files repository...
0 stars 0 forks source link

[ issue #11 ] Admin > Users - Filter is not persistent when using pagination #7

Open shaunakwyn opened 4 years ago

shaunakwyn commented 4 years ago

[File]

/htdocs/gtalents/alpha/app/Http/routes.php

[add]

{code}
       Route::post('getUsersByAjaxSearch', 'UsersController@getUsersByAjaxSearch' )->name('getUsersByAjaxSearch');
{code}
shaunakwyn commented 4 years ago

[File]

/htdocs/gtalents/alpha/app/Http/Controllers/UsersController.php

[Changes]

in index() function : 
{code}
    public function index()
    {
        $perPage = 20;
        $search = Input::get('search');
        $status = Input::get('status');
        $page = Input::get('page');

        if($status){
            Session::put('users-status', $status);
        }else{
            if(!$page){
                Session::forget('users-status');
            }            
        }
        if($search){
            Session::put('users-search', $search);
        }else{
            if(!$page){
                Session::forget('users-search');
            }            
        }

        if($page){
            if (Session::has('users-search')){
                $search = Session::get('users-search');
            }
            if (Session::has('users-status')){
                $status = Session::get('users-status');
            }
        }        

        $users = $this->users->paginate($perPage, $search, $status);
        $statuses = ['' => trans('app.all')] + UserStatus::lists();

        return view('user.index', compact('users', 'statuses'));
    }
{code}

for search on Ajax in future : 
{code}
    public function getUsersByAjaxSearch(Request $request)
    {
        $search = $request->get('search');
        $status = $request->get('status');
        $query = User::query();
        $users = User::select('id','first_name','last_name','code','email')->where('first_name', 'like', '%'.$search.'%')
                ->orWhere('last_name', 'like', '%'.$search.'%')
                ->orWhere('code', 'like', '%'.$search.'%')
                ->orWhere('email', 'like', '%'.$search.'%')->get();

        return response()->json($users->toArray());
    }
{code}
shaunakwyn commented 4 years ago

[File]

/htdocs/gtalents/alpha/resources/views/user/index.blade.php

[add]

for select status field : 
{code}
      {!! Form::select('status', $statuses, Session::get('users-status'), ['id' => 'status', 'class' => 'form-control']) !!}
{code}

Change table id :
{code}
       <table class="table" id="users_table">
{code}
shaunakwyn commented 4 years ago

[File]

/htdocs/gtalents/alpha/resources/views/layouts/app.blade.php

[add]

Add script with other datatables declaration :
{code}
     $('#users_table').DataTable({
            "paging":   false,
            "dom": '<"se_users"f>t',
            "processing": true
        });      
     $('.se_users input[type="search"]').on('input', function(){
            return;
            _this = $(this);            
            setTimeout(function(){
                if(_this.val().length > 3){
                    search = _this.val();
                    status = $('#status').val();
                    if(status == undefined){
                        status = '';
                    }
                    $.ajax({
                        url: "{{ route('getUsersByAjaxSearch') }}",
                        data: { 
                            search: search, 
                            status : status
                        },
                        type: "POST",
                        datatype: "json",
                        success: function(data){
                            $('#users_table').DataTable({
                                "paging":   false,
                                "dom": '<"se_users"f>t',
                                "processing": true,
                                "serverSide": true,       
                                data: data
                            });      
                        }
                    })
                }
            },1000)
        });  
{code}