2amigos / yii2-selectize-widget

Selectize From Brian Reavis Yii2 Widget
Other
73 stars 41 forks source link

Please help this widget is not showing existing tags for the record in the form #7

Closed ram0973 closed 10 years ago

ram0973 commented 10 years ago

I am using your yii2-taggable extension with this widget, all working fine except one - when I editing my model, existing tags for this record is not showing, the tags field is empty. But autocomplete is working fine.

Here is my code:

form: <?= $form->field($model, 'tagNames')->widget(Selectize::className(), [ // calls an action that returns a JSON object with matched // tags 'url' => ['/news/tag/list'], 'options' => ['class' => 'form-control'], 'clientOptions' => [ 'plugins' => ['remove_button'], 'valueField' => 'name', 'labelField' => 'name', 'searchField' => ['name'], 'create' => true, ], ])->hint('Use commas to separate tags') ?>

tag model: public static function findAllByName($query) { return Tag::find()->where(['like', 'name', $query])->all(); }

tag controller: <?php

namespace app\modules\news\controllers; use yii\web\Response; use app\modules\news\models\Tag; use Yii;

class TagController extends \yii\web\Controller { public function actionList($query) { $models = Tag::findAllByName($query); $items = [];

    foreach ($models as $model) {
        $items[] = ['name' => $model->name];
    }
    // We know we can use ContentNegotiator filter
    // this way is easier to show you here :)
    Yii::$app->response->format = Response::FORMAT_JSON;

    return $items;
}

}

Here is tags field is empty, but I certainly know, that it have a 2 tags: 2014-07-15 16-54-42 Autocomplete is ok: 2014-07-15 16-57-31 2014-07-15 16-58-04

ram0973 commented 10 years ago

Sorry, this is my fault. Just needed to write action like this:

public function actionUpdate($id) {

    $model = $this->findModel($id);

    $items = $model->getTags()->all();
    $tags = [];
    foreach ($items as $item)
    {
        $tags[] = $item->name;
    };
    $model->tagNames = implode(', ',$tags); **

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id, 'slug' => $model->slug]);
    } else {
        return $this->render('update', [
            'model' => $model,
       ]);
    }
}