Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to display column data in Yii2 listview

Tags:

yii2

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'],
                    ],
            ]);
like image 219
asela daskon Avatar asked Dec 29 '15 17:12

asela daskon


3 Answers

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.

like image 156
GHopper Avatar answered Nov 18 '22 04:11

GHopper


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.

like image 24
ernie Avatar answered Nov 18 '22 04:11

ernie


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>";
}
like image 2
sim Avatar answered Nov 18 '22 05:11

sim