如何在yii2网格视图的tbody和td中添加class?

How to add class in tbody and td of yii2 grid view?

使用 yii2 网格视图我可以添加额外的 classes 到 table。但如何添加到 tbody 和 td 中?

<table class="table table-striped table-bordered my-table">
   <thead>...</thead>
   <tbody class="tbl-body">
      <tr>
         <td class="emp-name">Emp 1</td>
         <td class="emp-age">25</td>
      </tr>
   </tbody>

 GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'filterPosition' => GridView::FILTER_POS_HEADER,
    'tableOptions' => ['class' => 'table table-striped table-bordered my-table'],
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

这是我用来添加额外 class 的内容,即我的-table 在 table 中,但是如何添加 tbl-body、emp-name、emp-age?

这样试试:

[
    'attribute' => 'attribute_name',
    'contentOptions' => ['class' => 'td-class'],
    'headerOptions' => ['class' => 'th-class']
],

参考文档:https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets#column-classes

遗憾的是,无法将 GridView 中的任何选项添加到正文标记中,但您可以将其用作 CSS 文件中 table 的子项:

table.my-table tbody {
    ...
}

上面@vvpanchev 的回答中描述的 td 标签的选项。