拉拉维尔过滤器在下一页点击分页后重置



>我创建了一个显示数据的表。我还添加了一些过滤器来过滤数据。使用分页,我每页显示 20 条记录。选择过滤器并单击表格过滤器中的搜索记录后,第一页上有分页,但一旦我单击下一页过滤器就会重置。如何阻止过滤器重置?

下面是我的代码,

public function index() 
{
    $agos = DB::table('orders')
        ->leftJoin('companies', 'orders.company_id', '=', 'companies.id')
        ->select(DB::raw('orders.id, companies.name, orders.type, orders.data, orders.currency, orders.price, orders.status, DATE_FORMAT(orders.created_at,"%M %d, %Y") as created_at '))
        ->where('orders.merchant', '=', 'agos')
        ->where(function ($query) {
            $status  = Input::has('status') ? Input::get('status') : null;
            $company = Input::has('company') ? Input::get('company') : null;
            $from    = Input::has('from_date') ? Input::get('from_date') : null;
            $to      = Input::has('to_date') ? Input::get('to_date') : null;
            $from    = date("Y-m-d", strtotime($from));
            $to      = date("Y-m-d", strtotime($to));
            
            if ( isset($status) ) {
                $query->where('orders.status', '=', $status);
            }
            
            if ( isset($company) ) {
                $query->where('companies.name', '=', $company);
            }
            
            if ( !empty($from) && !empty($to) ) {
                $query->whereBetween('orders.created_at', [$from, $to]);
            }
        })->orderBy('orders.created_at', 'desc')
        ->paginate(20);
    return $agos;
}

刀片文件代码,

@extends('layouts.agos')
@section('title', Translator::transSmart('app.Common Clerk(AGOS)', 'Common Clerk(AGOS)'))
@section('styles')
    @parent
    {{ Html::skinForVendor('jquery-textext/all.css') }}
@endsection
@section('scripts')
    @parent
    {{ Html::skinForVendor('jquery-textext/all.js') }}
@endsection
@section('content')
    <div class="admin-managing-member-index">
        <div class="row">
            <div class="col-sm-12">
                {{ Form::open(array('route' => array('agos::index'), 'class' => 'form-search')) }}
                <div class="row">
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'company';
                                $translate = Translator::transSmart('app.Company', 'Company');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            {{ Form::select($name, $companies->pluck('name', 'name'), Request::get($name), array('id' => $name, 'title' => $translate, 'class' => 'form-control', 'title' => $name, 'placeholder' => '')) }}
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'status';
                                $translate = Translator::transSmart('app.Status', 'Status');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            {{Form::select($name, Utility::constant('agos_status', true), Request::get($name), array('id' => $name, 'class' => 'form-control', 'title' => $translate, 'placeholder' => ''))}}
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'from_date';
                                $translate = Translator::transSmart('app.From', 'From');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            <div class="input-group schedule">
                                {{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'to_date';
                                $translate = Translator::transSmart('app.To', 'To');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            <div class="input-group schedule">
                                {{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12 toolbar">
                        <div class="btn-toolbar pull-right">
                            <div class="btn-group">
                                {{
                                    Html::linkRouteWithIcon(
                                        null,
                                        Translator::transSmart('app.Search', 'Search'),
                                        'fa-search',
                                       array(),
                                       [
                                           'title' => Translator::transSmart('app.Search', 'Search'),
                                           'class' => 'btn btn-theme search-btn',
                                           'onclick' => "$(this).closest('form').submit();"
                                       ]
                                    )
                                }}
                            </div>
                        </div>
                    </div>
                </div>
                {{ Form::close() }}
            </div>
        </div>
        <div class="row" >
            <div class="col-sm-12">
                <hr />
            </div>
        </div><br>

        <div class="row" style="background-color:#FFFFFF">
            <div class="col-sm-12">

                <div class="table-responsive">
                    <table class="table table-condensed table-crowded">
                        <thead>
                        <tr>
                            <th>{{Translator::transSmart('app.#', '#')}}</th>
                            <th></th>
                            <th>{{Translator::transSmart('app.Company', 'Company')}}</th>
                            <th>{{Translator::transSmart('app.Products', 'Products')}}</th>
                            <th>{{Translator::transSmart('app.Total Price', 'Total Price')}}</th>
                            <th>{{Translator::transSmart('app.Status', 'Status')}}</th>
                            <th>{{Translator::transSmart('app.Created At', 'Created At')}}</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @if($orders->isEmpty())
                            <tr>
                                <td class="text-center empty" colspan="14">
                                    --- {{ Translator::transSmart('app.No Record.', 'No Record.') }} ---
                                </td>
                            </tr>
                        @endif
                        <?php $count = 0;?>
                        @foreach($orders as $order)
                            <tr>
                                <td>{{++$count}}</td>
                                <td></td>
                                <td>{{$order->name}}</td>
                                <td>
                                    @php
                                        $json = $order->data;
                                        $json = json_decode($json, true);
                                        $products = $json['order_info']['products'];
                                        $data = '';
                                            foreach ($products as $hitsIndex => $hitsValue) {
                                                $data .= $hitsValue['name']. ',  ';
                                            }
                                        $data = rtrim($data, ', ');
                                    @endphp
                                    {{$data}}
                                </td>
                                <td>
                                    @if(empty($order->price) || $order->price == 0)
                                        {{'Quotation'}}
                                    @else
                                        {{CLDR::showPrice($order->price, $order->currency, Config::get('money.precision'))}}
                                    @endif
                                </td>
                                <td>{{Utility::constant(sprintf('agos_status.%s.name', $order->status))}}</td>
                                <td>{{$order->created_at}}</td>
                                <td class="item-toolbox">
                                    {{
                                           Html::linkRouteWithIcon(
                                             'agos::edit',
                                            Translator::transSmart('app.Edit', 'Edit'),
                                            'fa-pencil',
                                            ['id' => $order->id],
                                            [
                                            'title' => Translator::transSmart('app.Edit', 'Edit'),
                                            'class' => 'btn btn-theme'
                                            ]
                                           )
                                    }}
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="pagination-container">
                    @php
                        $query_search_param = Utility::parseQueryParams();
                    @endphp
                    {!! $orders->render() !!}
                </div>
            </div>
        </div>
    </div>
@endsection

控制器代码,

public function index(Request $request){
    try {
        $companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
        $orders = (new Agos())->index();
    } catch (InvalidArgumentException $e) {
        return Utility::httpExceptionHandler(500, $e);
    } catch (Exception $e) {
        return Utility::httpExceptionHandler(500, $e);
    }
    $view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));
    return $view;
}

有人可以帮助我吗?

尝试在结果中附加请求:

public function index(Request $request){
    try {
        $companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
        $orders = (new Agos())->index();
        $queryArgs = Input::only(['status','company','from_date', 'to_date']);
        $orders->appends($queryArgs);
    } catch (InvalidArgumentException $e) {
        return Utility::httpExceptionHandler(500, $e);
    } catch (Exception $e) {
        return Utility::httpExceptionHandler(500, $e);
    }
    $view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));
    return $view;
}

然后在模板中:

<div class="pagination-container">
      {!! $orders->render(); !!}
</div>

尝试在刀片视图中使用

$orders->links()

相关内容

  • 没有找到相关文章

最新更新