Yii2使用每个循环显示数据 [英] Yii2 display data using for each loop
问题描述
使用yii2,我已经使用gii创建了一个模型和CRUD.
Using yii2 I have created a Model and CRUD using gii.
我想在VIEW中使用foreach或while循环以以下格式显示数据
I want to use a foreach or while loop in my VIEW to display the data in the following format
对于数据库表中的每一行
For each row in database table
echo("addMarker($lat_field, $lon_field);\n");
我有一个使用以下控制器操作呈现的索引页.
I have an index page which is rendered using the following controller action.
public function actionIndex()
{
$this->layout = 'directory';
$searchModel = new ShopDirectorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
我可以使用以下方法通过listview显示数据,该视图显示数据库中的所有数据/行,但是它周围带有html,显然不会以我希望的格式输出.
I can use the following to display the data using listview which displays all the data/rows within database however it has html around it and obviously isn't outputted in the format I wish it to be.
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'col-xs-6 col-sm-3'],
'itemView' => '_index',
]);?>
推荐答案
无需在此处使用ListView,您只需尝试:
No need to use ListView here, you should simply try :
foreach ($dataProvider->models as $model) {
echo "addMarker({$model->lat_field}, {$model->lon_field});";
}
如果您确实要使用ListView,则只需编辑_index
视图文件.
If you really want to use ListView, you could simply edit _index
view file.
这篇关于Yii2使用每个循环显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!