1
votes

I want to display a data in a table in View and the data is provided by dataProvider but i don't want to use Yii GridView because the table is hard to see, i want to use a table of my choice and display this data in it, I try use foreach loop to loop through the data provided by dataprovider since it is an array, but i got an error message (Getting unknown property: yii\db\ActiveQuery::product_id) below is my code

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>

But i would like to use this below view to display my data, thanks for any help..

 <?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 -->
1

1 Answers

1
votes

For retrive the models in dataProvider you should use getModels()

  posts = $provider->getModels();

  $models = $dataProvider->getModels();

and then loop over these models

   <?php foreach ($models as $datas): ?>
              <td><?= $datas->product_id?></td>
   <?php endforeach ?>