Skip to content

Commit

Permalink
Fixes yiisoft#16365: Added $filterOnFocusOut option for GridView
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lver authored and samdark committed Jul 4, 2018
1 parent a5c8b21 commit 80d0c2b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.16 under development
------------------------

- Enh #16365: Added $filterOnFocusOut option for GridView (s1lver)
- Enh #14289: Added `yii\db\Command::executeResetSequence()` to work with Oracle (CedricYii)
- Enh #9133: Added `yii\behaviors\OptimisticLockBehavior` (tunecino)
- Bug #16104: Fixed `yii\db\pgsql\QueryBuilder::dropIndex()` to prepend index name with schema name (wapmorgan)
Expand Down
8 changes: 6 additions & 2 deletions framework/assets/yii.gridView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist in jQuery.yiiGridView');
return false;
Expand All @@ -23,7 +23,8 @@

var defaults = {
filterUrl: undefined,
filterSelector: undefined
filterSelector: undefined,
filterOnFocusOut: true
};

var gridData = {};
Expand Down Expand Up @@ -82,6 +83,9 @@
var $e = $(this);
var settings = $.extend({}, defaults, options || {});
var id = $e.attr('id');
if (!settings.filterOnFocusOut) {
return false;
}
if (gridData[id] === undefined) {
gridData[id] = {};
}
Expand Down
11 changes: 8 additions & 3 deletions framework/grid/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ class GridView extends BaseListView
* This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field.
*/
public $filterErrorOptions = ['class' => 'help-block'];
/**
* @var bool whatever to apply filters on losing focus. Leaves an ability to manage filters via yiiGridView JS
* @since 2.0.16
*/
public $filterOnFocusOut = true;
/**
* @var string the layout that determines how different sections of the grid view should be organized.
* The following tokens will be replaced with the corresponding section contents:
Expand Down Expand Up @@ -288,10 +293,10 @@ public function init()
*/
public function run()
{
$id = $this->options['id'];
$options = Json::htmlEncode($this->getClientOptions());
$view = $this->getView();
GridViewAsset::register($view);
$id = $this->options['id'];
$options = Json::htmlEncode(array_merge($this->getClientOptions(), ['filterOnFocusOut' => $this->filterOnFocusOut]));
$view->registerJs("jQuery('#$id').yiiGridView($options);");
parent::run();
}
Expand Down Expand Up @@ -360,7 +365,7 @@ public function renderItems()
$tableFooterAfterBody = $this->renderTableFooter();
} else {
$tableFooter = $this->renderTableFooter();
}
}
}

$content = array_filter([
Expand Down
9 changes: 6 additions & 3 deletions tests/js/tests/yii.gridView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ describe('yii.gridView', function () {
var $gridView;
var settings = {
filterUrl: '/posts/index',
filterSelector: '#w0-filters input, #w0-filters select'
filterSelector: '#w0-filters input, #w0-filters select',
filterOnFocusOut: true
};
var commonSettings = {
filterUrl: '/posts/index',
filterSelector: '#w-common-filters input, #w-common-filters select'
filterSelector: '#w-common-filters input, #w-common-filters select',
filterOnFocusOut: true
};
var $textInput;
var $select;
Expand Down Expand Up @@ -143,7 +145,8 @@ describe('yii.gridView', function () {
describe('init', function () {
var customSettings = {
filterUrl: '/posts/filter',
filterSelector: '#w-common-filters input'
filterSelector: '#w-common-filters input',
filterOnFocusOut: true
};

withData({
Expand Down

0 comments on commit 80d0c2b

Please sign in to comment.