【问题标题】:Put style tag in php echo将样式标签放入php echo
【发布时间】:2016-03-11 14:17:15
【问题描述】:

是否可以在里面放<style>标签和echo?我的场景是我有一个报销表,并且有这个“类型”列,如果类型是“临时报销”,整个表<tr>的文本颜色和字体样式将被更改:

它应该是这样的:

我正在使用 Yii 2.0 php 框架。这是我的代码:

echo GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'], 
        [
            'header' => 'Employee ID',
            'value' => 'employeeId'
        ],
        [
            'header' => 'Identification <br> Number',
            'value' => 'IDnumber'
        ],
        [
            'header' => 'Employee <br> Name',
            'format' => 'html',
            'value' => 'fullName'
        ],
        [
            'header' => 'Attachment',
            'format' => 'html',
            'value' => function ($model) {
                 return !empty($model->attachment) ? Html::img($model->getImageUrl(), ['class' => 'reim-attach']): 'No Attachment';
            },
        ],
        [
            'attribute' => 'receipt_company',
            'header' => 'Merchant',
        ],
        'description',
        [
            'attribute' => 'date',
            'header' => 'Date <br><span style= "color:gray;font-size:8pt;"> (dd-mm-yyyy)</span>',
        ],
        'currency',
        [
            'attribute' => 'amount',
            'format'=>['decimal',2],
            'value' => function ($model){ 
                return !empty($model['amount']) ? $model['amount'] : 0.00;
            }
        ],
        [
            'attribute' => 'exchange_rate',
            'header' => 'Exchange <br> Rate',
            'format'=>['decimal',2],
            'value' => function ($model){ 
                return !empty($model['exchange_rate']) ? $model['exchange_rate'] : 0.00;
            }
        ],
        [
            'attribute' => 'converted_amount',
            'header' => 'Converted <br> Amount',
            'format'=>['decimal',2],
            'value' => function ($model){ 
                return !empty($model['converted_amount']) ? $model['converted_amount'] : 0.00;
            }
        ],
        [
            'attribute' => 'chargeable',
            'header' => 'Chargeable to <br> Client',
            'value' => function ($model) {
                return $model['chargeable'] ? 'Chargeable' : 'Non-chargeable';
            },
        ],
        [
            'attribute' => 'date_noted',
            'header' => 'Date Modified <br><span style= "color:gray;font-size:8pt;"> (dd-mm-yyyy)</span>',
        ],
        [
            'attribute' => 'status',
            'label' => 'Status',
            'content' => function ($model, $key, $index, $column) {
                if ($model['status'] == "Pending") {
                    return Html::button('Pending', ['class' => 'status-pending']);
                } elseif ($model['status'] == "Draft") {
                    return Html::button('Draft', ['class' => 'status-draft']);
                } elseif ($model['status'] == "Approved") {
                    return Html::button('Approved', ['class' => 'status-approved']);
                } elseif ($model['status'] == "Rejected") {
                    return Html::button('Rejected', ['class' => 'status-rejected']);
                } elseif ($model['status'] == "Reimbursed") {
                    return Html::button('Reimbursed', ['class' => 'status-reimbursed']);
                }
            }
        ],
        'type',
        [
            'label' => 'Action',
            'content' => function ($model, $key, $index, $column) {
                if($model['status'] == "Pending") {
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' =>  $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve'])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-ban"></i> Reject', ['value' =>  $model['_id'], 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject']);
                }  elseif ($model['status'] == "Draft") {
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                    .'&nbsp'
                    . Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3', 'data-toggle'=>'tooltip', 'title'=>'Update'])
                    .'&nbsp'
                    . Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive','data-toggle'=>'tooltip', 'title'=>'Delete', 'data' => ['confirm' => 'Are you sure you want to delete this reimbursement?', 'method' => 'post']]);
                } elseif ($model['status'] == "Approved")  {
                    if ($model->type == 'Ad Hoc Reimbursement') {
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-check-square-o"></i> Reimburse', ['value' =>  $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'reimburse(value)', 'data-toggle'=>'tooltip','title'=>'Reimburse']);
                    } else {
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Approve'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-ban"></i> Reject', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Reject']);
                    }
                } else {
                    if ($model->type == 'Ad Hoc Reimbursement') {
                        echo "<style>.table-striped > tbody > tr { font-style: italic !important;color: #259A5A !important; }</style>";
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-check-square-o"></i> Reimburse', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reimburse(value)', 'disabled' => true, 'data-toggle'=>'tooltip','title'=>'Reimburse']);
                    } else {
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Approve'])
                        .'&nbsp'
                        .Html::button('<i class="fa fa-ban"></i> Reject', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Reject']);
                    }                
                }
            }
        ]
    ],
]);

在最后一部分,你可以看到if ($model-&gt;type == 'Ad Hoc Reimbursement'),在里面我放了echo "&lt;style&gt;.table-striped &gt; tbody &gt; tr { font-style: italic !important;color: #259A5A !important; }&lt;/style&gt;";,我认为这是不对的,因为它不起作用。

您对此有什么想法吗?

【问题讨论】:

  • 我认为您的风格不正确或被覆盖。尝试使用该页面打开 chrome 开发人员工具并创建该样式值以查看它是否真的影响任何内容。玩弄直到你能得到那种效果,复制那种风格,然后在你的代码中回显有效的东西。也尝试将它放在返回的字符串的末尾。

标签: php html css yii styles


【解决方案1】:

您在列配置中应用了条件样式,这是行不通的。

您要做的是将样式定义为一个类,并有条件地更改&lt;tr&gt;中的行类

echo GridView::widget([
  'dataProvider' => $dataProvider,
  'rowOptions' => function ($model, $index, $widget, $grid){
    return ['class'=> ($model->type == 'Ad Hoc Reimbursement') ? 'ad-hoc-reimbursement' : null];
  },
]);

<style> 
  tr.ad-hoc-reimbursement {font-style: italic !important;color: #259A5A !important; }
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多