PHP 多选标签

PHP 多选标签/选项(THINKPHP5)

数据库设计
PS:注意把字段类型设置为text

控制器代码

    protected function _initialize()
    {
        parent::_initialize();
        $user_label_list = UserLabelModel::order('sort desc')->select();
        $this->assign([
            'user_label_list'  => $user_label_list,
        ]);
    }

模型代码

    //修改器
    protected function setLabelIdsAttr($value){
        $sid = "";
        if ($value) {
            foreach ($value as $val) {
                $sid.=$val . ",";
            }
            return $sid = trim($sid, ",");
        }
        return ;
    }
    //获取器
    protected function getLabelIdsAttr($value){

        if ($value) {
            return explode(',',$value);
        }
        return ;
    }
    /**
     * 获取标签集名称
     * @param $ids  标签ID数组集
     * @return string
     */
    public static function getLabelNameByIdSet($ids){
        $nameSet = '';

        foreach ($ids as $key=>$value){
            $name = M_Label::where('id',$value)->value('name');
            $nameSet = $nameSet.",".$name;
        }
        return trim($nameSet,',');
    }

添加模板代码

                    <!--添加多选-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签</label>
                        <div class="layui-input-block">
                          {foreach name="user_label_list" item="vo"}
                         <input type="checkbox" name="user_label_ids[]" value="{$vo['id']}">{$vo.name}
                         {/foreach}
                        </div>
                    </div>
                    <!--添加多选-->
                    

编辑模板代码

                    <!--编辑多选-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签</label>
                        <div class="layui-input-block">
                          {foreach name="user_label_list" item="vo"}
                         <input type="checkbox" name="user_label_ids[]" value="{$vo['id']}"
                         {if in_array($vo['id'],$data['user_label_ids'])}
                         checked 
                         {/if}
                         >{$vo.name}
                         {/foreach}
                        </div>
                    </div>
                    <!--编辑多选-->

Pasa吴技术博客
请先登录后发表评论
  • latest comments
  • 总共0条评论