Answer the question
In order to leave comments, you need to log in
How to pass models to js file?
In the controller we take models:
public function actionIndex()
{
$searchModel = new BookSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render($this->getIndexByRole(), [
'searchModel' => $searchModel,
'models' => $dataProvider->getModels(),
]);
}
$script = <<< SCRIPT
var app4 = new Vue({
el: '#app-4',
data: {
todos: '$models'
}
});
console.log($models);
SCRIPT;
$this->registerJs($script);
<div id="app-4">
<ol>
<li v-for="todo in todos">
{{ todo.id }}
</li>
</ol>
</div>
Answer the question
In order to leave comments, you need to log in
return $this->asJson(ArrayHelper::map($models,'id',function($model){
return $model->attributes('id');
}));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question