如何使用 Laravel Collective 将自定义数据属性设置为选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41895464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:13:06  来源:igfitidea点击:

How to set custom data-attribute to option with Laravel Collective

phplaravellaravel-5.2laravelcollective

提问by Sredny M Casanova

I have a form, inside I have a select with some options and I'm using Laravel Collective Forms to build it, I have something like:

我有一个表单,里面有一个带有一些选项的选择,我正在使用 Laravel Collective Forms 来构建它,我有类似的东西:

{!! Form::select('size', $data, $selecteds, ['multiple' => true]) !!}

All going well until here, but now I need to set a data-sectionattribute to each option, how can I make it?

一切顺利,直到这里,但现在我需要data-section为每个选项设置一个属性,我该怎么做?

回答by J-F

I had to do the same thing recently. After inspecting the FormBuilder class to write my own marco I found out that the select() method actually has an undocumented fifth parameter for option attributes:

我最近不得不做同样的事情。在检查 FormBuilder 类以编写我自己的 marco 之后,我发现 select() 方法实际上有一个未记录的第五个选项属性参数:

Form::select('size', $data, $selecteds, ['multiple' => true], $optionAttributes)

Form::select('size', $data, $selecteds, ['multiple' => true], $optionAttributes)

The index must match the value of the option, e.g.:

索引必须与选项的值匹配,例如:

$optionAttributes = [
    'S' => [
        'data-title' => 'Small',
        'data-surcharge' => '0',
    ],
    'M' => [
        'data-title' => 'Medium',
        'data-surcharge' => '5',
    ],
    'L' => [
        'data-title' => 'Large',
        'data-surcharge' => '10',
    ],
];

So I ended up writing a marco which generates this array based on a collection and then uses the default select() method. Something like that:

所以我最终编写了一个基于集合生成这个数组的 marco,然后使用默认的 select() 方法。类似的东西:

\Form::macro('locationSelect', function ($name, $value = null, $attributes = []) {
    // Get all locations from the DB
    $locations = \App\Location::all();

    // Make an id=>title Array for the <option>s
    $list = $locations->pluck('title', 'id')->toArray();

    // Generate all data-attributes per option
    $optionAttributes = [];
    foreach ($locations as $location) {
        $optionAttributes[$location->id] = [
            'data-icon' => $location->icon,
            'data-something' => $location->some_attribute,
        ];
    }

    // Use default select() method of the FormBuilder
    return $this->select($name, $list, $value, $attributes, $optionAttributes);
});

Very convenient.

很方便。

{{ Form::locationSelect('location_id') }}

{{ Form::locationSelect('location_id') }}

回答by Alexey Mezenin

Add it to a 4th argument which is an array:

将其添加到数组的第四个参数中:

{!! Form::select('size', $data, $selecteds, ['data-attribute' => 'John Smith', 'multiple' => true]) !!}