BcFormHelper::selectText PHP Method

selectText() public method

文字列保存用複数選択コントロール
public selectText ( string $fieldName, array $options = [], mixed $selected = null, array $attributes = [], mixed $showEmpty = '' ) : string
$fieldName string id,nameなどの名前
$options array optionタグの値
$selected mixed selectedを付与する要素
$attributes array htmlの属性
$showEmpty mixed 空要素の表示/非表示、初期値
return string
    public function selectText($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '')
    {
        $_attributes = array('separator' => '<br />', 'quotes' => true);
        $attributes = Hash::merge($_attributes, $attributes);
        // $selected、$showEmptyをFormHelperのselect()に対応
        $attributes += array('value' => $selected, 'empty' => $showEmpty);
        $quotes = $attributes['quotes'];
        unset($attributes['quotes']);
        $_options = $this->_initInputField($fieldName, $options);
        if (empty($attributes['multiple'])) {
            $attributes['multiple'] = 'checkbox';
        }
        $id = $_options['id'];
        $_id = $_options['id'] . '_';
        $name = $_options['name'];
        $out = '<div id="' . $_id . '">' . $this->select($fieldName . '_', $options, $attributes) . '</div>';
        $out .= $this->hidden($fieldName);
        $script = <<<DOC_END
\$(document).ready(function() {
    aryValue = \$("#{$id}").val().replace(/\\'/g,"").split(",");
    for(key in aryValue){
        var value = aryValue[key];
        \$("#"+camelize("{$id}_"+value)).prop('checked',true);
    }
    \$("#{$_id} input[type=checkbox]").change(function(){
        var aryValue = [];
        \$("#{$_id} input[type=checkbox]").each(function(key,value){
            if(\$(this).prop('checked')){
                aryValue.push("'"+\$(this).val()+"'");
            }
        });
        \$("#{$id}").val(aryValue.join(','));
    });
});
DOC_END;
        $out .= $this->Js->buffer($script);
        return $out;
    }