LoginSignup
1
0

More than 5 years have passed since last update.

laravel5.3以下でselectタグ内のoptionタグにプロパティを追加出来るカスタムタグ

Last updated at Posted at 2017-06-20

5.4からはLaravelCollectiveのhtml helperが提供してくれる昨日なので以下は用済み。
5.3以下のlaravelを使っている場合はoptionタグにプロパティを追加することが出来ないため以下のようにカスタムタグを作成する必要がある。

コード自体は LaravelCollective から持ってきただけ。
なので基本的には5.4にアップデートをしてもカスタムタグを呼び出している箇所を通常のHtmlHelperのselectメソッドに書き換えれば問題なく動くはず。

ソースコード

カスタムタグ

resources/macros/select_addable_option_property.php
<?php
Form::macro('selectAddableOptionProperty', function(
        $name,
        $list = [],
        $selected = null,
        array $selectAttributes = [],
        array $optionsAttributes = []
    ) {
        $selected = $this->getValueAttribute($name, $selected);
        $selectAttributes['id'] = $this->getIdAttribute($name, $selectAttributes);
        if (! isset($selectAttributes['name'])) {
            $selectAttributes['name'] = $name;
        }
        $html = [];
        if (isset($selectAttributes['placeholder'])) {
            $html[] = $this->placeholderOption($selectAttributes['placeholder'], $selected);
            unset($selectAttributes['placeholder']);
        }
        // laravel 5.4 option method
        $option = function($display, $value, $selected, array $attributes = []) {
                $selected = $this->getSelectedValue($value, $selected);
                $options = ['value' => $value, 'selected' => $selected] + $attributes;
                return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';
        };
        // laravel 5.4 optionGroup method
        $optionGroup = function ($list, $label, $selected, array $attributes = []) use ($option){
            $html = [];
            foreach ($list as $value => $display) {
                $html[] = $option($display, $value, $selected, $attributes);
            }
            return '<optgroup label="'.e($label).'">'.implode('', $html).'</optgroup>';
        };
        // laravel 5.4 getSelectOption method
        $getSelectOption = function($display, $value, $selected, array $attributes = []) use ($option, $optionGroup){
            if (is_array($display)) {
                return $optionGroup($display, $value, $selected, $attributes);
            }
            return $option($display, $value, $selected, $attributes);
        };
        foreach ($list as $value => $display) {
            $optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : [];
            $html[] = $getSelectOption($display, $value, $selected, $optionAttributes);
        }
        $selectAttributes = $this->html->attributes($selectAttributes);
        $list = implode('', $html);
        return "<select{$selectAttributes}>{$list}</select>";
});

呼び出し側ソースコード

index.blade.php
{!!
Form::selectAddableOptionProperty(
    "name_property",    // name属性
    [                   // 選択項目
        1=>"one",
        2=>"two",
        3=>"three"
    ],
    null,               // selectedのindex
    $properties,        // selectタグのプロパティ
    [                   // 選択項目のvalue値 => ["属性名" => "属性値"]
        1=>["class"=>"one class name",
        2=>["class"=>"two class name"],
        3=>["class"=>"three class name"]
    ]
)
!!}

呼び出し側のコードは実務で作ったやつをサンプルように引数を加工したので動かなかったら勘弁な!!!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0