Skip to content

Commit cde59b3

Browse files
authored
Merge pull request #19 from kuhdmitry/master
add attributes for select options
2 parents 59d8ffc + e2df593 commit cde59b3

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Form/Element/Select.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313

1414
class Select extends Element
1515
{
16-
protected $options = [];
17-
protected $multiple = false;
16+
protected $options = [];
17+
protected $optionsAttributes = [];
18+
protected $multiple = false;
1819

19-
public function setOptions($options)
20+
public function setOptions($options, $attributes = [])
2021
{
2122
if ($options instanceof Collection)
2223
{
2324
$options = $options->getDictionary();
2425
}
2526
$this->options = $options;
2627

28+
$this->optionsAttributes = $attributes;
29+
2730
return $this;
2831
}
2932

@@ -97,17 +100,18 @@ public function view()
97100
}
98101

99102
return view('form::' . $this->theme . '.element.select', [
100-
'label' => $this->label,
101-
'help' => $this->help,
102-
'placeholder' => $this->placeholder,
103-
'name' => $this->name,
104-
'elementName' => $this->elementName,
105-
'options' => $this->options,
106-
'error' => $this->error,
107-
'value' => $this->value,
108-
'multiple' => $this->multiple,
109-
'class' => $this->class,
110-
'attributes' => $this->attributes
103+
'label' => $this->label,
104+
'help' => $this->help,
105+
'placeholder' => $this->placeholder,
106+
'name' => $this->name,
107+
'elementName' => $this->elementName,
108+
'options' => $this->options,
109+
'optionsAttributes' => $this->optionsAttributes,
110+
'error' => $this->error,
111+
'value' => $this->value,
112+
'multiple' => $this->multiple,
113+
'class' => $this->class,
114+
'attributes' => $this->attributes
111115
]);
112116
}
113117

src/resources/views/default/element/select.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
>
88
@foreach($options as $key => $option)
99
<option value="{{$key}}"
10-
@if (($multiple && $value && in_array((string)$key, $value, true)) || $value === (string)$key) selected="selected" @endif>{{ $option }}</option>
10+
@if($optionAttributes = array_get($optionsAttributes, $key))
11+
@foreach($optionAttributes as $optionAttributeName => $optionAttributeValue) {{$optionAttributeName}}="{{$optionAttributeValue}}" @endforeach
12+
@endif
13+
@if (($multiple && $value && in_array((string)$key, $value, true)) || $value === (string)$key) selected="selected" @endif>{{ $option }}
14+
</option>
1115
@endforeach
1216
</select>
1317
<div class="form__help">{{ $help }}</div>

0 commit comments

Comments
 (0)