我想在View中的表格中显示数据,数据由dataProvider提供,但我不想使用Yii GridView,因为表格很难看,我想使用我选择的表格并在其中显示此数据,我尝试使用foreach循环来遍历dataprovider提供的数据,因为它是一个数组,但我得到一条错误消息(获取未知属性:yii \ db \ ActiveQuery :: product_id)下面是我的代码
Controller code
public function actionIndex()
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
view generated by CRUD
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\ProductSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Products';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Add new Product', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
'product_id',
//'type_id',
'name',
'descr',
//'status',
'price',
'image',
// 'added_at',
// 'updated_at',
'views',
// 'reviews',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
但我想使用下面的视图来显示我的数据,感谢您的帮助..
<?php
use yii\helpers\Html;
?>
<div class="box">
<div class="box-header">
<h3 class="box-title">Data Table With Full Features</h3>
</div><!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($dataProvider as $datas): ?>
<td><?= $datas->product_id?></td>
<td><?= Html::encode("{$datas->name}")?></td>
<td><?= Html::encode("{$datas->descr}")?></td>
<td><?= Html::encode("{$datas->price}")?></td>
<td><?= Html::encode("{$datas->image}")?></td>
<td>C</td>
<?php endforeach ?>
</tr>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Image</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
答案 0 :(得分:1)
对于dataProvider中的模型,您应该使用getModels()
posts = $provider->getModels();
$models = $dataProvider->getModels();
然后遍历这些模型
<?php foreach ($models as $datas): ?>
<td><?= $datas->product_id?></td>
<?php endforeach ?>