AbdulBazith's avatar

Problem in pagination after ajax return in laravel

Guys I am working with a project milkfarm

I am having a form with search box and text box.

In the textbox event on change i wrote a jquery to search the value

Everything is file it searches and appends with the

But after search is done if I move to page 2 then it is refreshed the

search option disappears and all the data are displayed

Say for example I will seach time=”am” then it fetches 7 records

because there are only 7 record matches. But I have given

pagination(5). So only 5 records are displayed in the page 1 to see the

balance 2 records if I click page 2 the the page is refreshed so search

done is cancelled

How to rectify it

This is my view.blade

<tbody >
            @foreach($exs as $ex)
                <tr>         
                    <td>{{ $ex->date }}</td>
                    <td>{{ $ex->time }}</td>
                    <td>{{ $ex->no_of_litre }}</td>
                    <td>{{ $ex->note }}</td>

</tr>
@endforeach
</tbody>

This is my javascript


<script type="text/javascript">

    $('#search_time').on('keyup',function(){
        $.ajax({
        dataType: 'json',
        type : 'get',
        url : '{{URL::to('search_excess_milk_details')}}',

        data:{
             
                'search_time': $('#search_time').val()
             
        },
        success:function(data)
        {
           
            var res='';
            $.each (data, function (key, value) {
            res +=
            '<tr>'+
                '<td>'+value.date+'</td>'+
                '<td>'+value.time+'</td>'+
                '<td>'+value.no_of_litre+'</td>'+
                '<td>'+value.note+'</td>'+  
             '</tr>';
   });
            $('tbody').html(res);
        }
    });
    })
</script>


This is my controller search

public function search_excess_milk_details( Request $request )
    {               
                    $xcess_milks = Excess_milk::where(function ($query) use ($request)
                       {                 

                            if (!empty($request->search_time)) {
                            $query->Where('time', 'LIKE', '%' . $request->search_time . '%');
                         }
                    })->orderBy('date','asc')->get;

   return Response($xcess_milks);
}


Kindly some one help please..!!!

0 likes
9 replies
tykus's avatar

If you wanted to persist the search filter across GET requests you might be better to append the search to the query string. Then the pagination links will automatically have the filter as well as the page number

1 like
pardeepkumar's avatar

Try your code like

controller 

public function search() {

    $input = Input::get('search');
    $posts = Post::where('title','LIKE',$input)->paginate(5);

    if (Request::ajax()) {
        return View::make('posts.search')->withPosts($posts);
    }
    return View::make('posts.index')->withPosts($posts);
}

1 like
pardeepkumar's avatar

First i am get search input value by using$input = Input::get('search'); then by help of Post model with where condition search particular value and then paginate it. then by ajax request with if condition to get desire output .

1 like
newbie360's avatar
public function search_excess_milk_details( Request $request )
{               
    $searchTime = $request->filled('search_time') ? $request->query('search_time') : null;
    $query = Excess_milk::query();
    $queryStr = [];
    
    if (!empty($searchTime))
    {
        $query->Where('time', 'LIKE', '%' . $searchTime . '%');
        $queryStr['search_time'] = $searchTime;
    }

    $xcess_milks = $query->orderBy('date', 'asc')->paginate(5);

    // append the query string, eg, http://localhost/something?search_time=searchTime
    $xcess_milks->appends($queryStr);


   return Response($xcess_milks);
}

and view

@if ($xcess_milks->hasPages())
    {{ $xcess_milks->links() }}
@endif

this just an example, you need modify by yourself

jlrdw's avatar

can slightly explain with coding please.

If you don't understand the query string why don't you take a break from laravel and take a couple of quality tutorials before getting too deep into laravel.

It should go without saying that those parameters have to be passed somewhere.

1 like

Please or to participate in this conversation.