AbdulBazith's avatar

Ajax with seach Pagination in not working Laravel

Guys i am working with a project

i am using search inajax

This is my controller before search

 public function index()
    {

        $loc=Location_details::orderBy('created_at','desc')->paginate(10);
        return view('Location_details.view')->withloc($loc);

    }

This is my partials 'tbody'


        @foreach($loc as $locs)
            <tr>
                <td>{{ $locs->ven_loc }}</td>
                <td>{{ $locs->ven_area }}</td>
                <td>{{ $locs->ven_booth }}</td>
<td>

        {!! Form::open(['route'=>['Location_details.destroy',$locs->id], 'onsubmit' => 'return ConfirmDelete()','method'=>'Delete']) !!}
    <a href="{{ route('Location_details.edit',$locs->id) }}" class="btn btn-info glyphicon glyphicon-edit"></a>

    <button type="submit" class="btn btn-info glyphicon glyphicon-trash">
        </button>
</td>

{!! Form::close() !!}
     </tr>
        @endforeach

This is my view


 <tbody id="results">
        @include('Location_details.tbody')
    </tbody>

  </table>

        <div class="text-center">

            {!! $loc->Links(); !!}
        </div>

This is my search_Location controller

public function search_location_details( Request $request )
    {


            $loc = Location_details::where(function ($query) use ($request)
                {

                    if (!empty($request->location)) {
                    $query->Where('ven_loc', 'LIKE', '%' . $request->location . '%');
                    }

                if (!empty($request->area)) {
                    $query->Where('ven_area', 'LIKE', '%' . $request->area . '%');
                }

                if (!empty($request->booth)) {
                    $query->Where('ven_booth', 'LIKE', '%' . $request->booth . '%');
                }

            })->orderBy('created_at','desc')->paginate(10);

             return view('Location_details.tbody', compact('loc'));     

         
    }

This is my ajax

success:function(data)
        {
            console.log(data);

              $('#results').html(data);
             

        },

everything works fine. but pagination is the only problem

see i have given pagination in the view file..

Kindly help me this

it sorts correctly, but when i click page 2 then the page is reloaded so once again i need to search..

When i click page 2 page is refereshed how to solve this??

0 likes
13 replies
Sergiu17's avatar
success: function(data)
{
    console.log(data);
    $('#results').html(data);
}

Is this your AJAX Request?

1 like
pardeepkumar's avatar

try something like

$(document).ajaxComplete(function() {
    $('.pagi   a').click(function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $.ajax({
            url: url,
            success: function(data) {
                $('#result').html(data);
            }
        });
    });
});
crnkovic's avatar

Yes, exactly. This is not a bug. This is just how it works. Let me walk you through it.

You load 5 items from the server, and the server counts 20 items total. So you have 4 pages, right? You loop through those 5 items using Blade and render pagination links using Blade, right? So at this point of time, 5 items have been loaded and pagination links are rendered and your website is visible. Then, you add an AJAX call that searches your server for some data and returns it back, and you replace those 5 items with items you retrieved from AJAX call. At this point, Javascript only replaced those 5 items FOR CURRENT REQUEST. If you refresh the page, all those 5 items will still be the same as initially. At this point, you have those pagination links you rendered before AJAX is done. Now, you click page 2 and everything is refreshed because: server sees you want page 2, it loads 5 items starting from position 5 and counts total of 20 items on your server. It renders 5 items it just got and renders pagination links. You again need to perform AJAX to search and modify existing data displayed.

I recommend learning PHP and how request lifecycle works in PHP before starting with Laravel.

1 like
zion's avatar

You cannot use laravels pagination if you do not want the page refresh. Go for a more client-sided approach using ajax.

Not trying to be rude, but do you even attempt to do any programming yourself? Based on the amount of posts with help request you create it seems you often try the easy way and just create a question for help. Also, try a google search, it'll give you an answer way faster most of the times.

1 like
jlrdw's avatar

I will give you a helpful hint, you have a search that for you have parameters.

Those parameters have to be passed in the query string.

There are free YouTube videos on using Ajax jQuery with both Post and Get.

This stuff is not learned in one evening some here have done this stuff 20 to 30 years and are still learning.

Another tip, this sort of thing has been answered here before if you attempt a search.

1 like
Sergiu17's avatar

@zion

You cannot use laravels pagination if you do not want the page refresh

I saw many examples with built in Laravel's paginations, listen for click event for those links and send ajax requests, nothing special. Here's good one I guess https://gist.github.com/tobysteward/6163902

I didn't test it

1 like
AbdulBazith's avatar

@zion

most of the times i refer google. but iam new to laravel so sometimes i cant understand. in this threads some one will answer with explanation. that would help me improve my project as well as knowledge.

actually i am in dead end of my project that's why asked this question.

see my thread past 10 threads speaks about this pagination only.

pagination problem with this ajax is some what tedious task for me

kindly dont mistake me..

1 like
AbdulBazith's avatar

@crnkovic thankz for your reply.

ya what you said is right. what i expect is after my search only the search records and the pagination for that search records must be displayed.

say for example after search the result contain 8 records means, i give paginate(5) in my search controller. so only two pages with 5 + 3 must be displayed..

but my old pagination before search is displayed.

based on my search my pagination should also change..

thats what i expect..

1 like
crnkovic's avatar

Yeah, however you can use Vue which makes it super simple to make dynamic pagination really easy and makes what you want super easy.

1 like
AbdulBazith's avatar

@crnkovic ya let me try. tomorrow itself i need to finish it,,

i need to refer tutorial for that and i will try it, thank you for your reply.

If you don't mind can can you kindly share the appropriate video link for my problem in vue.js

AbdulBazith's avatar

@pardeepkumar @Sergiu17


$('#location,#area,#booth').on('keyup',function(){ 
        $.ajax({
        type : 'get',

        url : '{{URL::to('search_location_details')}}',

        data:{
                'location': $('#location').val(),
                'area': $('#area').val(),
                'booth': $('#booth').val()
        },

        success:function(data)
        {
            console.log(data);
              $('#results').html(data);

        }

        });
        })


this is what i am passing from my ajax..

can you say where i should add your coding @pardeepkumar

AbdulBazith's avatar

@pardeepkumar @Sergiu17 @zion @crnkovic @jlrdw

One doubt, actually i have replaced the content of tbody after my ajax search so the searched item is displayed .

then why cant i replace the pagination also


div class="text-center">   

    {!! $loc->Links(); !!}

</div>

see the above section is displayed before search.

After change why cant i replace this pagination s that my search result pagination content only will be displayed am i right??

why i am saying this is because, i no need of moving my search result to page without refresh

instead if i replace the pagination then whenever i search a content pagination will be for that content only that's what i expected

is there any idea??

What i expect exactly is when i search using jquery that pagination link remains there and it is not updated as per my returned results. my pagination must update based on the return result thats what exactly i expect..

Kindly help please

Please or to participate in this conversation.