CSS 如何在 Yii2 Gridview 中更改 1 行的背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29745947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:11:30  来源:igfitidea点击:

How to change background of 1 row in Yii2 Gridview

cssgridviewrowyii2

提问by Paramone

I'm working with Yii 2, and it's grid view to show information.

我正在使用 Yii 2,它是显示信息的网格视图。

Now, my problem is that whenever a user scans two identical serial numbers and/or mac addresses, it should highlight the row (change color to red), and show some error sign or whatever.

现在,我的问题是,每当用户扫描两个相同的序列号和/或 mac 地址时,它应该突出显示该行(将颜色更改为红色),并显示一些错误标志或其他内容。

Screenshot:Current Grid View

截屏:当前网格视图

What I want it to look like:

我希望它看起来像什么:

Desired Grid Highlighting

Desired Grid Highlighting

I'm new to Yii2 and don't exactly know how they do it with the grid view. I've been researching on this specific problem but couldn't find anything.

我是 Yii2 的新手,并不完全知道他们是如何使用网格视图来做到这一点的。我一直在研究这个特定问题,但找不到任何东西。

Code for Gridview

Gridview 的代码

 <?= GridView::widget([
    'id' => 'scan-batch-grid',
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        ['class' => 'yii\grid\CheckboxColumn'],
        [
            'attribute' => 'product_ID',
            'value' => 'product.name'
        ],
        'SN',
        'MAC',
        [
            'class' => 'yii\grid\ActionColumn',
            'urlCreator' => function ($action, $model, $key, $index) {
                    return Url::to(['scan-batch/view',  'id' => $key, 'scan' => $model->scan_batch_ID]);
            },
            'buttons' => [
                'update' => function ($url, $model, $key) {
                    return '';
                },
                'delete' => function ($url, $model, $key) {
                    return '';
                },
            ],
        ],
    ],
]); ?>

EDIT

编辑

I only want to know how to change the color of only one row.

我只想知道如何更改仅一行的颜色。

回答by Paramone

Got it!

知道了!

Yii2 : Adding Classes to Rows in the GridView (YouTube)

Yii2:向 GridView 中的行添加类 (YouTube)

Yii2 Gridview row by row css expression

Yii2 Gridview 逐行 css 表达式

Simply add rowOptions to your gridview.

只需将 rowOptions 添加到您的 gridview。

<?= GridView::widget([
    'id' => 'scan-batch-grid',
    'dataProvider' => $dataProvider,
    'rowOptions'=>function($model){
            if($a == $b){
                return ['class' => 'danger'];
            }
    },

回答by DrBorrow

Thanks for posting your answer Paramone. Worked great.

感谢您发布您的回答 Paramone。工作得很好。

Here is my implementation:

这是我的实现:

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'rowOptions' => function ($model) {
        if ($model->name == 'test') {
            return ['class' => 'info'];
        }
    },