简体   繁体   中英

Yii CGridView add class or style for header cell

I want to set some style or css class for header cell in specific column.

This changes css only for data cells in a column.

        'columns'=>array(
            array(
                'name'=>'id',
                'header'=>'#',
                'htmlOptions'=>array('style'=>'width: 50px; text-align: center;', 'class'=>'zzz'),
            ),

How to set css or style in header cell of this column?

Use headerHtmlOptions .

'columns'=>array(
        array(
            'name'=>'id',
            'header'=>'#',
            'htmlOptions'=>array('style'=>'width: 50px; text-align: center;', 'class'=>'zzz'),
            'headerHtmlOptions'=>array(...),
        ),

filterHtmlOptions

If you want to style the content that the user enters in the filterbox - for example "text-align : right" - then

'filterHtmlOptions'=>array('style'=>'text-align: right'),

is not going to work, because it will only style the outer table cell (td), and not the inner filter-container (div) or input element:

<td style="text-align: right;">
    <div class="filter-container">
        <input>
    </div>
</td>

What you can do is add a class to the outer table cell:

'filterHtmlOptions'=>array('class'=>'filterBoxRight'),

which will result in this:

<td class="filterBoxRight">
    <div class="filter-container">
        <input>
    </div>
</td>

Then run the following code:

$(document).on('ready', function(){
    $('.filterBoxRight').find('.filter-container').find(':input').css({
        'text-align': 'right',
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM