[英]Display the dropdown text using laravel
I'm working on a reporting page using php Laravel framework.我正在使用 php Laravel 框架处理报告页面。 I select the options from the comboboxes, these values are sent to the controller to replace them in some queries used to display the reports.我从组合框中选择选项,这些值被发送到控制器以在用于显示报告的某些查询中替换它们。
When I click the submit button, the page refreshes and the name shown is the last element of the dropdown.当我单击提交按钮时,页面会刷新,显示的名称是下拉列表的最后一个元素。
My Controller:我的控制器:
$negocioDive = DB::table('ma_leads_reporte')
->selectRaw('NEGOCIO, year(FEC_CREACION) as AÑO')
->groupBy('NEGOCIO', 'AÑO')
->get()->toArray();
$selectFecha = DB::table('param_fecha')
->selectRaw('mesNum, mesNom')
->groupBy('mesNum', 'mesNom')
->get()->toArray();
$año = $request ->input('año');
$mes = $request -> input('mes');
$negocio = $request -> input('negocio');
//Solicitud de Cotización versus mes anterior
$mesAnt = DB::table('ma_leads_reporte')
->selectRaw('count(*) as C, day(FEC_CREACION) AS DIA, monthname(FEC_CREACION) AS MES')
->whereMonth('date_identified', '=', $mes-1)
->whereYear('date_identified', '=', $año)
->groupBy('MES', 'DIA')
->get()->toArray();
$vscont = array_column($mesAnt, 'C');
$antMes = array_column($mesAnt, 'MES');
$dia = array_column($mesAnt, 'DIA');
My View:我的看法:
<div class="container">
<div class="row justify-content-center">
<div class="content mt-3">
<div class="animated fadeIn">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<form action="{{route('reportes')}}" method="GET" id="fecha">
{{Form::label('', 'Año')}}
<select id="año" name="año">
@foreach($negocioDive as
$negocioD)
<option value="{{$negocioD->AÑO}}"
selected="selected">{{$negocioD->AÑO}}</option>
@endforeach
</select>
{{Form::label('', 'Mes')}}
<select id="mes" name="mes">
@foreach($selectFecha as $fecha)
<option value="{{$fecha->mesNum}}" name="{{$fecha->mesNom}}">{{$fecha->mesNom}}</option>
@endforeach
</select>
{{Form::label('', 'Negocio')}}
<select id="negocio" name="negocio" >
@foreach($negocioDive as $negocioD)
<option value="{{$negocioD->NEGOCIO}}" selected="selected">{{$negocioD->NEGOCIO}}</option>
@endforeach
</select>
<button type="submit"class="btn btn-default" id="filter">Filtrar
<i class="fa fa-filter"></i>
</button>
</form>
<?php if(isset($_GET['año']) && isset($_GET['mes']) && isset($_GET['negocio'])){
echo "<h4> Año: $año | Mes: {$fecha->mesNom} | Negocio: $negocio </h4>";
}?><!--
<p id="mesJS"></p> -->
</div>
</div>
</div>
After receiving the request variables, it should show the correct values instead of the last index of the dropdown.收到请求变量后,它应该显示正确的值而不是下拉列表的最后一个索引。 What can I do?我能做什么? dropdown options:下拉选项:
After submit:提交后:
The reason it is showing the last index of the dropdown is that you are looping on your various items and creating your options within each select box with ' selected
' overwritten every time all the way to the last one in the loop.它显示下拉列表的最后一个索引的原因是您正在循环访问各种项目并在每个选择框中创建您的选项,每次都覆盖“ selected
”,直到循环中的最后一个。
To fix this, you'll need to add some type of conditional on the option box to tell it if the loop item is the correct selected
one.要解决此问题,您需要在选项框上添加某种类型的条件,以告诉它循环项目是否是正确selected
项目。 If the new model has the key within this loop, then, and only then, make it selected.如果新模型在此循环中具有键,那么,并且只有这样,才将其选中。 Something like this (You'll need to make this code your own):像这样(您需要将此代码设为您自己的):
@foreach($negocioDive as $id=>name)
<option value="{{$id}}" {{$theThingYouAreSelecting->id === $id?"selected":''}}>$name</option>
@endforeach
NOTE - I'm assuming you are sending a key->value array to the foreach as this is what the option
expects - the key would be the id
and the value would be the name
for the item.注意 - 我假设您正在向 foreach 发送一个 key->value 数组,因为这是option
期望的 - 键将是id
,值将是项目的name
。
Once you get this working, I strongly recommend looking at LaravelCollective HTML - it binds the model to the form and automates a huge chunk of the conditionals.一旦您开始工作,我强烈建议您查看 LaravelCollective HTML - 它会将模型绑定到表单并自动化大量条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.