android gridview失去焦点,yii2中类似gridview的一系列表格的检索框,怎么不让他鼠标失去焦点的时候搜索...

本文介绍了一种在Yii2框架中对GridView组件的renderFilters方法进行重写的方法,以便于实现更灵活的搜索框功能。通过继承GridView类并修改相关属性及方法,可以定制搜索框的表现形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我也遇到了相同的问题。继承GridView类重写 renderFilters 方法

namespace app\modules\admin\components;

use Yii;

use yii\base\InvalidConfigException;

use yii\grid\GridView;

use yii\grid\GridViewAsset;

use yii\helpers\Html;

use yii\helpers\Json;

use yii\helpers\Url;

class ZGridView extends GridView {

public $layout = "{items}\n{summary}\n{pager}";

public $options = ['class' => 'grid-view form-inline'];

/**

* Creates column objects and initializes them.

*/

protected function initColumns() {

if (empty($this->columns)) {

$this->guessColumns();

}

foreach ($this->columns as $i => $column) {

if (is_string($column)) {

$column = $this->createDataColumn($column);

} else {

// 设置搜索默认值

$label = '';

if (isset($column['label'])) {

$label = $column['label'];

}

$column = Yii::createObject(array_merge([

'class' => $this->dataColumnClass ?: ZDataColumn::className(),

'grid' => $this,

], $column));

if ($label != '') {

$column->filterInputOptions['prompt'] = $label;

}

}

if (!$column->visible) {

unset($this->columns[$i]);

continue;

}

$this->columns[$i] = $column;

}

}

public function renderTableHeader() {

$cells = [];

foreach ($this->columns as $column) {

/* @var $column Column */

$cells[] = $column->renderHeaderCell();

}

$content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);

return "\n" . $content . "\n";

}

public function renderItems() {

$content = '';

if ($this->filterPosition === self::FILTER_POS_HEADER) {

$content = $this->renderFilters() . $content;

} elseif ($this->filterPosition === self::FILTER_POS_BODY) {

$content .= $this->renderFilters();

}

$res = parent::renderItems();

return $content . $res;

}

/**

* 重写搜索框 加前缀

*/

public function renderFilters() {

if ($this->filterModel !== null) {

$cells = [];

$inputStr = '';

foreach ($this->columns as $key => $column) {

$renderFilterCell = $column->renderFilterCell();

if($renderFilterCell != false){

$inputStr = Html::tag('div', $column->renderFilterCell(), ['class' => 'form-group']);

$cells[] = $inputStr;

}

}

// sd($cells);

$cells[] = '搜索';

$content = Html::tag('form', implode('', $cells), $this->filterRowOptions);

$content = Html::tag('div', $content, ['class' => 'btn-group pull-left']);

$content = Html::tag('div', $content, ['class' => 'table-toolbar']);

return $content;

} else {

return '';

}

}

/**

* Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label".

* @param string $text the column specification string

* @return DataColumn the column instance

* @throws InvalidConfigException if the column specification is invalid

*/

protected function createDataColumn($text) {

if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {

throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');

}

$label = $this->getLabelName($text);

return Yii::createObject([

'class' => $this->dataColumnClass ?: ZDataColumn::className(),

'grid' => $this,

'filterInputOptions' => ['class' => 'form-control', 'id' => null, 'placeholder' => $label],

'attribute' => $matches[1],

'format' => isset($matches[3]) ? $matches[3] : 'text',

'label' => isset($matches[5]) ? $matches[5] : null,

]);

}

public function getLabelName($attr) {

$provider = $this->dataProvider;

$model = new $provider->query->modelClass;

$label = $model->getAttributeLabel($attr);

return $label;

}

/**

* Returns the options for the grid view JS widget.

* @return array the options

*/

protected function getClientOptions()

{

$filterUrl = isset($this->filterUrl) ? $this->filterUrl : Yii::$app->request->url;

$id = $this->filterRowOptions['id'];

$filterSelector = "#$id select, #$id button";

if (isset($this->filterSelector)) {

$filterSelector .= ', ' . $this->filterSelector;

}

return [

'filterUrl' => Url::to($filterUrl),

'filterSelector' => $filterSelector,

];

}

/**

* Runs the widget.

*/

public function run() {

$id = $this->options['id'];

$options = Json::htmlEncode($this->getClientOptions());

$view = $this->getView();

GridViewAsset::register($view);

// $view->registerJs("jQuery('#$id').yiiGridView($options);");

$view->registerCss(".filters>.form-group {padding-right: 10px;}");

$view->registerCss(".pagination{ position: absolute; right: 21px; bottom: -5px; }");

\yii\widgets\BaseListView::run();

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值