将样式标记放在php echo中

时间:2015-12-07 03:04:19

标签: php html css yii styles

是否可以将<style>标记放在echo内?我的情况是我有一个报销表,并且有这个“类型”列,如果类型是“Ad Hoc Reimbursement”,则整个表<tr>的文本颜色和字体样式将被更改:< / p>

enter image description here

这就是它的样子: enter image description here

我正在使用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->type == 'Ad Hoc Reimbursement')并在其中,我认为echo "<style>.table-striped > tbody > tr { font-style: italic !important;color: #259A5A !important; }</style>";我认为不正确,因为它不起作用。

你对此有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您在列配置中应用了条件样式,这种样式无法正常工作。

您要做的是将样式定义为类,并有条件地更改<tr>

中的行类
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>