I've try to display my table columns data in listview but it's not working. In gridview it's allow to insert fields name. can't i do it in listview ?
I try this code.
$feedbackFound = new SqlDataProvider([
'sql'=> "SELECT * FROM feedback WHERE module_id='$tutorModule->id'",
]);
ListView::widget([
'dataProvider' => $feedbackFound,
//'filterModel' => $searchModel,
'itemView' => [
'PositiveComment',
'GoodAt',
'MakeMost',
//['class' => 'yii\grid\ActionColumn'],
],
]);
Unfortunately, sim`s answer doesn't work for me, that is why I have changed it a little:
$colsCount = 4;
echo ListView::widget([
'dataProvider' => $itemDataProvider,
'layout' => '{items}{pager}',
'options' => ['class' => 'TOP_CLASS_HERE'],
'itemOptions' => ['class' => "ITEM_CLASS_HERE"],
'beforeItem' => function ($model, $key, $index, $widget) use ($colsCount) {
if ($index % $colsCount === 0) {
return "<div class='row'>";
}
},
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('@frontend/views/item/_card.php', ['item' => $model]);
},
'afterItem' => function ($model, $key, $index, $widget) use ($colsCount) {
$content = '';
if (($index > 0) && ($index % $colsCount === $colsCount - 1)) {
$content .= "</div>";
}
return $content;
},
]);
if ($itemDataProvider->count % $colsCount !== 0) {
echo "</div>";
}
I tested it on big amount of data and it works perfect. I like this solution, hope it helps somebody.
ListView doesn't have the fancy handling that gridview does - it takes data and passes it to a view, giving you some convenience items (pagination, sorting, filtering, etc).
The presentation and use of the convenience items is up to you. itemView
is not analogous to the columns
key for gridview. It's supposed to be a partial view (i.e. a php file you've created or a callback), so you need to define that partial view. That gives you a lot of flexibility in how each item from your data model is shown, but it requires some work.
Consider this code, it displays items from left to right, from top to bottom in a three columns. This code uses the following bootstrap css classes: col-sm-?
and row
to make a proper grid.
$columns = 3;
$cl = 12 / $columns;
echo ListView::widget([
'dataProvider' => $dataProvider,
'layout' => '{items}{pager}',
'itemOptions' => ['class' => "col-sm-$cl"],
'itemView' => '_view_list_item',
'options' => ['class' => 'grid-list-view' ],
'beforeItem' => function ($model, $key, $index, $widget) use ($columns) {
if ( $index % $columns == 0 ) {
return "<div class='row'>";
}
},
'afterItem' => function ($model, $key, $index, $widget) use ($columns) {
if ( $index != 0 && $index % ($columns - 1) == 0 ) {
return "</div>";
}
}
]);
if ( $dataProvider->totalCount % $columns != 0 ) {
echo "</div>";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With