dsifjgogw48491752 2015-06-16 09:05
浏览 82

Laravel 5 Pagination如何在另一个页面中渲染结果

Hi I made a Laravel Project which I can view some records with sorting. For examle I have a deliveries page with URL

http://localhost/dboard/public/deliverytracker

In that form there are From Date and To Date to sort out the data before the query. When I clicked submit the data is rendered and the pagination links is there. However when click, for example page 2 of the pagination which is

http://localhost/dboard/public/deliverytracker?page=2

in the link it won't rendered the page 2 of the data. It just reloaded the page where I need to select again the from date and to date then clicking submit again. Did I missed something?

Here's my route

/* Delivery Tracker */
Route::get('deliverytracker', 'DeliveryTrackerController@deliveryIndex');
Route::post('deliverytracker', 'DeliveryTrackerController@getDeliveries');

My function when I submit the form

public function getDeliveries()
{
    $rules = array(
        'fromdate'  => 'required',
        'todate'    => 'required',
    );

    $validator = Validator::make(Input::all(), $rules);

    // Check if all fields is filled
    if ($validator->fails()) 
    {
        return Redirect::to('deliverytracker')->withErrors($validator);
    }
    else
    {
        $from   = Input::get('fromdate');
        $to     = Input::get('todate');

        $deliveries = new DeliveryTracking();
        $result = $deliveries->getDeliveries($from, $to);

        // Get the current page from the url if it's not set default to 1
        $page = Input::get('page', 1); 

        // Number of items per page
        $perPage = 5;

        // Start displaying items from this number;
        $offSet = ($page * $perPage) - $perPage; // Start displaying items from this number

        // Get only the items you need using array_slice (only get 10 items since that's what you need)
        $itemsForCurrentPage = array_slice($result, $offSet, $perPage, true);

        // Return the paginator with only 10 items but with the count of all items and set the it on the correct page
        $result =  new LengthAwarePaginator($itemsForCurrentPage, count($result), $perPage, $page);
        $result->setPath('deliverytracker');

        return view('deliverytracker.index')->with(array('result' => $result));

    }

}

My view

<div class="panel-body">
                @if(isset($result))
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td>Delivery Status</td>
                            <td>Ref. Number</td>
                            <td>PO #</td>
                            <td>Count</td>
                            <td>Delivery QTY</td>
                            <td>Date Delivered</td>
                            <td>Filename</td>
                            <td>Invoice #</td>
                            <td>Date Invoice</td>

                        </tr>
                    </thead>
                    <tbody>
                    @foreach($result as $key => $value)
                        @if($value->stat == "Completed")
                            <tr>
                                <td class="bg-success">{{ $value->stat  }}</td>
                                <td class="bg-success">{{ $value->oc }}</td>
                                <td class="bg-success">{{ $value->pon }}</td>
                                <td class="bg-success">{{ $value->cnt }}</td>
                                <td class="bg-success">{{ $value->dq }}</td>
                                <td class="bg-success">{{ $value->dd }}</td>
                                <td class="bg-success">{{ $value->fn }}</td>
                                <td class="bg-success">{{ $value->inum }}</td>
                                <td class="bg-success">{{ $value->di }}</td>
                            </tr>
                        @elseif($value->stat == "Active")
                            <tr>
                                <td class="bg-danger">{{ $value->stat  }}</td>
                                <td class="bg-danger">{{ $value->oc }}</td>
                                <td class="bg-danger">{{ $value->pon }}</td>
                                <td class="bg-danger">{{ $value->cnt }}</td>
                                <td class="bg-danger">{{ $value->dq }}</td>
                                <td class="bg-danger">{{ $value->dd }}</td>
                                <td class="bg-danger">{{ $value->fn }}</td>
                                <td class="bg-danger">{{ $value->inum }}</td>
                                <td class="bg-danger">{{ $value->di }}</td>
                            </tr>   
                        @endif

                    @endforeach
                    </tbody>
                     </table>
                        {!! $result->render() !!}
                @else 
                    No records found.    

                @endif
 </div>
  • 写回答

2条回答 默认 最新

  • duanjiu1003 2015-06-16 11:07
    关注

    In your post you mention that the second page is called using only ?page=N, seeing your route is defined with both GET and POST you will hit the route definition for GET when clicking the next page link.

    The pages buttons are simply links and not POST forms. So instead of hitting the getDeliveries() you are hitting deliveryIndex().

    Also you might consider appending the fromdate and todate to your pagination links; see Appending To Pagination Links

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?