【问题标题】:How to set default value in 2amigos/yii2-taggable-behavior如何在 2amigos/yii2-taggable-behavior 中设置默认值
【发布时间】:2017-08-02 07:06:50
【问题描述】:

我想为我的更新页面设置可标记的默认值,但我做不到!

这是我在_form.php 中的代码。

https://github.com/2amigos/yii2-taggable-behavior

 <?=
    $form->field($model, 'tags')->widget(SelectizeTextInput::className(), [
        // calls an action that returns a JSON object with matched
        // tags

        'loadUrl' => ['tags/list'],
        'options' => ['class' => 'form-control'],
        'clientOptions' => [
            'plugins' => ['remove_button'],
            'valueField' => 'name',
            'labelField' => 'name',
            'searchField' => ['name'],
            'create' => true,
        ],
    ])->hint('Use commas to separate tags')
    ?>

这是我的模型:

class Post extends \yii\db\ActiveRecord
{

    public $category;
    public $prime;
    public $metaNames;
    public $metaDec;
    public $tags;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'post';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
                [['menu', 'prime', 'slideshow', 'special', 'visit', 'deleted', 'active'], 'integer'],
                [['name', 'summary', 'text', 'sutitr', 'reference', 'slug', 'menu', 'slideshow', 'special', 'visit', 'created', 'modified', 'deleted', 'active'], 'required'],
                [['summary', 'tagDec', 'metaDec', 'tags', 'text', 'sutitr'], 'string'],
                [['created', 'modified'], 'safe'],
                ['category', 'each', 'rule' => ['integer']],
                [['tagNames'], 'safe'],
                [['headline'], 'string', 'max' => 255],
                [['name', 'reference'], 'string', 'max' => 100],
                [['slug'], 'string', 'max' => 200],
                [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

我想在更新页面设置默认标签。

【问题讨论】:

    标签: yii2 yii2-extension


    【解决方案1】:

    在SelectizeTextInput 中真正使用属性value 设置选定值:

    SelectizeTextInput::widget([
        'name' => 'tags',
        'value' => 'love, this, game',
        'clientOptions' => [
            // ...
        ],
    ]);
    

    您需要像example 那样设置与表格帖子的关系,以获取您之前设置的值。

    public function getTags(){
        return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('post_tag_assn', ['post_id' => 'id']);
    }
    

    【讨论】:

    • 我要设置静态值!我无法理解!
    • 那么您的代码形式如下:&lt;?= $form-&gt;field($model, 'tags')-&gt;widget(SelectizeTextInput::className(), [ 'value'=&gt;'some, static, value' 'loadUrl' =&gt; ['tags/list'], 'options' =&gt; ['class' =&gt; 'form-control'], 'clientOptions' =&gt; [ 'plugins' =&gt; ['remove_button'], 'valueField' =&gt; 'name', 'labelField' =&gt; 'name', 'searchField' =&gt; ['name'], 'create' =&gt; true, ], ])-&gt;hint('Use commas to separate tags') ?&gt;
    • 不!默认选择值。打开页面时自动选择一个标签!
    • 如果这不是新记录,您是否尝试在模型中设置默认值?公共 $tags = !$this->isNewRecord ? '一些,静态,价值':空;如果你想要一些更复杂的条件,请使用 yii2 getters
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2014-12-13
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    相关资源
    最近更新 更多