Yii2 display data using for each loop(Yii2 显示每个循环使用的数据)
问题描述
使用 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);
");
我有一个使用以下控制器操作呈现的索引页.
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 显示每个循环使用的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii2 显示每个循环使用的数据


基础教程推荐
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的foreach复选框POST 2021-01-01
- php中的PDF导出 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01