【问题标题】:Display the dropdown text using laravel使用 laravel 显示下拉文本
【发布时间】:2020-01-02 02:27:16
【问题描述】:

我正在使用 php Laravel 框架制作报告页面。我从组合框中选择选项,这些值被发送到控制器以在用于显示报告的某些查询中替换它们。

当我点击提交按钮时,页面刷新并且显示的名称是下拉列表的最后一个元素。

我的控制器:

$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');

我的观点:

<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>

收到请求变量后,它应该显示正确的值,而不是下拉列表的最后一个索引。 我能做些什么? 下拉选项:

提交后:

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    它显示下拉列表的最后一个索引的原因是您正在循环您的各种项目并在每个选择框中创建您的选项,每次都覆盖“selected”直到循环中的最后一个.

    要解决此问题,您需要在选项框上添加某种类型的条件,以告诉它循环项是否是正确的 selected 之一。如果新模型在此循环中具有密钥,那么并且只有在那时,才将其选中。像这样的东西(你需要自己制作这段代码):

     @foreach($negocioDive as $id=>name)
          <option value="{{$id}}" {{$theThingYouAreSelecting->id === $id?"selected":''}}>$name</option>
     @endforeach
    

    注意 - 我假设您正在向 foreach 发送一个 key->value 数组,因为这是 option 所期望的 - 键将是 id,值将是 name项目。

    一旦你完成了这项工作,我强烈建议你查看 LaravelCollective HTML——它将模型绑定到表单并自动执行大量条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多