scottsuhy's avatar

How to keep user on same paginated page after deleting an item?

Using pagination. When a user deletes an item on page >=2 they are taken back to page 1 versus left on the page they were on.

I need to keep the user on the page that they were on.

How do I do this?

Here is the code for the JS and controller function:

Javascript

    $(document).on('click','.removeFromAuction', function(event) {
        var itemid = $(this).attr('itemid');
        var show = $("#show option:selected").val();
        $.ajax({
            url:"/removeitemfromauction",
            data:{show:show, itemid:itemid},
            success:function(data_return)
            {
                $('#auction_items_pagination').html(data_return);
            }
        });
    });

Controller function

    function removeitemfromauction(Request $request)
    {
        $result = DB::table('auctionitems')
            ->where('coin_id', '=', $request->itemid)
            ->delete();

        if($result){
            $errormsg = 0;
        }
        else{
            $errormsg = 1;
        }

        $items = DB::table('coins')
            ->select(DB::raw('coins.id, auctionitems.auction_id, auctionitems.item_reserve, coins.series, coins.year, coins.mint, coins.rating, coins.rating_group, coins.notable_value_description, coins.purchase_price_total, coins.retail_value'))
            ->leftJoin('auctionitems', function($join){
                $join->on('auctionitems.coin_id', '=', 'coins.id');
            })
            ->where('user_email', '=', auth()->user()->email)
            ->orderBy('coins.id', 'asc')
            ->paginate($request->show);

        return view('auction.auction_items_pagination_data', compact('errormsg','items'))->render();
    }
0 likes
2 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

Get whatever page they were on in the request. And using JavaScript window location redirect to that page in your js. You will have to add the page as part of your response.

scottsuhy's avatar

You rock. (again :-) )

    $(document).on('click','.removeFromAuction', function(event) {
        var itemid = $(this).attr('itemid');
        var show = $("#show option:selected").val();
        var page = $(this).attr('currentpage');  //<<<<<<<-added this, also updated the HTML and controller to return current page.

        $.ajax({
            url:"/removeitemfromauction?page="+page,  //<<<<<<<-changed this
            data:{show:show, itemid:itemid},
            success:function(data_return)
            {
                $('#auction_items_pagination').html(data_return);
            }
        });
    });

Please or to participate in this conversation.