1

我可以仅对模型中的数据使用 taggest 支持吗?我不需要用户创建的新标签

echo '<label class="control-label">Tag Content</label>';
echo Select2::widget([
'name' => 'color_1', 
'options' => ['placeholder' => 'Select a color ...', 'class'=>'form-control'],
'pluginOptions' => [
    'tags' => ["red", "green", "blue", "orange", "white", "black", "purple", "cyan",            "teal"],
    'maximumInputLength' => 10
],

]);

现在用户可以添加“gr”、“r”等标签,但我不想要它。我只需要由我的模型创建的标签(如 stackoverflow)

4

1 回答 1

2

在你的 _form.php

echo '<label class="control-label">Tag Content</label>';
echo Select2::widget([
    'model' => $model,
    'attribute' => 'tags',
    'name' => 'tags',
    'data' => common\models\Tag::getOptions(),
    'options' => [
        'placeholder' => 'Select a color ...',
        'class' => 'form-control',
        'multiple' => true
    ], ]);

然后在您的模型中添加以下代码

public static function getOptions(){
    $data=  static::find()->all();
   $value=(count($data)==0)? [''=>'']: \yii\helpers\ArrayHelper::map($data, 'id','name'); //id = your ID model, name = your caption

    return $value;
}
于 2014-05-10T05:54:39.043 回答