【问题标题】:Way to insert value into select list option by use Laravel Collection methods.使用 Laravel 集合方法将值插入选择列表选项的方法。
【发布时间】:2018-03-28 12:20:25
【问题描述】:

下面的代码通过从数据库中的 Job Table 中读取 job_type 字段来制作 job_type 选择框。

此外,如果在选择框中将列表选择为 Andre,那么 Modal 将如何显示。

问题。

  1. 下面的列表框显示了表中预定义值的列表。 我如何将列表值 Andre 预定义到列表中,即使它 不包含在数据库中。

控制器

public function edit($id)
{
  ---
  $job_types = Job::all()->pluck('job_type', 'job_type')->unique();
  ---
}

查看(选择框。)

<!-- Job Type Field -->
<div class="form-group col-sm-3 col-lg-3">
    {!! Form::label('job_type', 'Job Type:') !!}    
    {!! Form::select('job_types', $job_types, null, ['class' => 'form-control', 'id' => 'job_type']) !!}
</div>

查看(插入新job_type的模态框​​)

{{--  Modal box define start  --}}
<div class="modal fade" id="jobListModal">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span>
              </button>
              <h4 class="modal-title">Sett inn ny jobb emne</h4>
            </div>
            <div class="modal-body">
              <p>New Job Type:</p>
              <input type="text" id="textInput">
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" id="btnSave" class="btn btn-primary">Save</button>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
{{--  Modal box define end  --}}

Jquery 代码(用于模态)

$('#job_type').on('mouseleave touchend', function (e) {
    if ('Andre' == this.value) {
        $("#jobStopModal").modal("show");
    }
});
$('#btnSave').on('click', function () {
    var newType = $('input#textInput').val();
    $('select#job_type option:last')
        .after('<option value="' + newType + '" selected="selected" >' + newType + '</option>');
    $('#jobListModal').modal('hide');
});

听取建议后的测试结果

我听从了@Nouphal.M 的建议,所以我修改了控制器中的一些代码,如下所示,它运行良好,但我得到了下面的列表视图图像。它表示为包含在选项列表“0”的子类别中。

如何修改为超出选项列表?为什么会出现这个结果?

谢谢你。

$customData = array('**NY**'=>'**NY**');

$job_types = Job::whereNotNull('job_type')
->orderBy('job_type', 'desc')
->pluck('job_type', 'job_type')
->push($customData)
->unique();

【问题讨论】:

    标签: javascript laravel drop-down-menu selectlist


    【解决方案1】:

    我不确定,但请尝试以下方法:

      $job_types = Job::all()->pluck('job_type', 'job_type')->unique();
      $customData = (object) array('customValue'=>'customLabel');
      $job_types->push($customData);
    

    看看collections。希望对你有帮助

    【讨论】:

    • 我修改了问题,你能给出更详细的建议吗,谢谢:)
    【解决方案2】:

    如果您只需要一件物品:

    您可以使用“占位符”选项(它将作为选择占位符可见,但不可选择)。

    {{ Form::select('job_types', $job_types, null, ['class' => 'form-control', 'id' => 'job_type', 'placeholder' => 'Job Type...']) }}
    

    或添加收集方法

    {{ Form::select('job_types', $job_types->prepend('Job Type...', ''), null, ['class' => 'form-control', 'id' => 'job_type']) }}
    

    但如果您需要多个 - 您可以在控制器中使用 concat 收集方法:

    $job_types = Job::all()->pluck('job_type', 'job_type')->concat([1 => 'Job type 1', 2 => 'Job type 2'])->unique();
    

    【讨论】:

    • 您的建议很好,但在这种情况下不起作用。但我得到了未来发展的提示,:)
    【解决方案3】:

    我通过将push 替换为put 得到结果,如下代码所示。

        $job_types = Job::whereNotNull('job_type')
        ->orderBy('job_type', 'desc')
        ->pluck('job_type', 'job_type')
        ->put('**NY**', '**NY**')
        ->unique();
    

    【讨论】:

    • 或者 ->put(...$customData)
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2011-04-17
    • 1970-01-01
    • 2013-06-07
    • 2013-11-21
    • 2011-07-26
    • 2011-10-13
    • 2014-12-04
    相关资源
    最近更新 更多