下一页单击分页后Laravel过滤器重置
Posted
技术标签:
【中文标题】下一页单击分页后Laravel过滤器重置【英文标题】:Laravel filter reset after next page click in paginate 【发布时间】:2020-03-18 06:40:02 【问题描述】:我已经创建了一个表来显示数据。我还添加了一些过滤器来过滤数据。使用分页,我每页显示 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;
有人可以帮我吗?
【问题讨论】:
在刀片文件的分页链接中使用 $agos->appends(Request::all())->links()
@ab.in 已经完成,分页也可以正常工作。当我使用过滤器第一页过滤所有记录但单击下一页过滤器后被重置
可以添加刀片文件吗?
你为什么要在闭包里放这么多代码?你应该在->where(function($query)
之外写那些!!!!!
@ab.in 我添加刀片文件代码
【参考方案1】:
尝试在结果中追加请求:
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>
【讨论】:
【参考方案2】:尝试在您的 Blade 视图中使用
$orders->links()
【讨论】:
以上是关于下一页单击分页后Laravel过滤器重置的主要内容,如果未能解决你的问题,请参考以下文章
如果使用 useEffect 更改了另一个过滤器,则重置为第一页
jqgrid - 水平滚动条 - 位置 - 分页后 - 从左到右