Yii2 显示每个循环使用的数据

2022-01-07 00:00:00 loops php mysql yii2 model-view-controller

使用 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.

相关文章