如何自定义yii2网格视图过滤行


<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,                            
'tableOptions' => ['class' => 'table custom-table-view table-striped table-bordered'],
'columns' => [
['class' => 'yiigridSerialColumn'],
[
'label' => 'User name',
'attribute' => 'name',
'filterInputOptions' => [
'class' => 'form-control custom-form-control pr-5',
'placeholder' => 'Search...',

],

],
],
])
?>

这是我正在使用的代码,使用这些代码我可以将类添加到其中,这就是输出。

<table class="table custom-table-view table-striped table-bordered">
<thead>
<tr id="w0-filters" class="filters">
<td>&nbsp;</td>
<td><input type="text" id="search-name" class="form-control custom-form-control pr-5" name="Search[name]" placeholder="Search..."></td>
</tr>

但我想要的是

<tr id="w0-filters" class="filters">
<td>&nbsp;</td>
<td>
<div class"my_class">
<input type="text" id="search-name" class="form-control custom-form-control pr-5" name="Search[name]" placeholder="Search..."><i class="fa fa-user"></i>
</div>
</td>

如何在输入字段外添加my_classdiv。?

问题是:你为什么要这么做?对我来说,这听起来像是你在寻找GridView afterHeader函数。

如果你真的需要这样的东西,你必须扩展GridView类并覆盖renderFilters((。

<?php
namespace appcommonoverrideyii2;
class GridView extends kartikgridGridView
{
/**
* @inheritdoc
*/
public function renderFilters()
{
/* Just send the Original Filter for demonstration */
return parent::renderFilters();
}
}

你会发现原来的renderFilters函数只返回一个字符串:

return Html::tag('tr', implode('', $cells), $this->filterRowOptions);

更改字符串的最佳策略可能是更改renderFilterCell((函数,更简单的方法可能是

$cells[] = Html::tag('div',$column->renderFilterCell(),['class'=>'my_class']);

最新更新