1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\jquery; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Behavior; |
12
|
|
|
use yii\base\Widget; |
13
|
|
|
use yii\helpers\Json; |
14
|
|
|
use yii\helpers\Url; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* GridViewClientScript is a behavior for [[\yii\grid\GridView]] widget, which allows automatic filter submission via jQuery component. |
18
|
|
|
* |
19
|
|
|
* A basic usage looks like the following: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* <?= yii\grid\GridView::widget([ |
23
|
|
|
* 'dataProvider' => $dataProvider, |
24
|
|
|
* 'as clientScript' => [ |
25
|
|
|
* 'class' => yii\jquery\GridViewClientScript::class |
26
|
|
|
* ], |
27
|
|
|
* 'columns' => [ |
28
|
|
|
* 'id', |
29
|
|
|
* 'name', |
30
|
|
|
* 'created_at:datetime', |
31
|
|
|
* // ... |
32
|
|
|
* ], |
33
|
|
|
* ]) ?> |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* @see \yii\grid\GridView |
37
|
|
|
* @see GridViewAsset |
38
|
|
|
* |
39
|
|
|
* @property \yii\grid\GridView $owner the owner of this behavior. |
40
|
|
|
* |
41
|
|
|
* @author Qiang Xue <[email protected]> |
42
|
|
|
* @author Paul Klimov <[email protected]> |
43
|
|
|
* @since 2.1.0 |
44
|
|
|
*/ |
45
|
|
|
class GridViewClientScript extends Behavior |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var string additional jQuery selector for selecting filter input fields. |
49
|
|
|
*/ |
50
|
|
|
public $filterSelector; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
1 |
|
public function events() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
1 |
|
Widget::EVENT_BEFORE_RUN => 'beforeRun' |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handles [[Widget::EVENT_BEFORE_RUN]] event, registering related client script. |
65
|
|
|
* @param \yii\base\Event $event event instance. |
66
|
|
|
*/ |
67
|
1 |
|
public function beforeRun($event) |
|
|
|
|
68
|
|
|
{ |
69
|
1 |
|
$id = $this->owner->options['id']; |
70
|
1 |
|
$options = Json::htmlEncode($this->getClientOptions()); |
71
|
1 |
|
$view = $this->owner->getView(); |
72
|
1 |
|
GridViewAsset::register($view); |
73
|
1 |
|
$view->registerJs("jQuery('#$id').yiiGridView($options);"); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the options for the grid view JS widget. |
78
|
|
|
* @return array the options |
79
|
|
|
*/ |
80
|
1 |
|
protected function getClientOptions() |
81
|
|
|
{ |
82
|
1 |
|
$filterUrl = isset($this->owner->filterUrl) ? $this->owner->filterUrl : Yii::$app->request->url; |
83
|
1 |
|
$id = $this->owner->filterRowOptions['id']; |
84
|
1 |
|
$filterSelector = "#$id input, #$id select"; |
85
|
1 |
|
if (isset($this->filterSelector)) { |
86
|
|
|
$filterSelector .= ', ' . $this->filterSelector; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [ |
90
|
1 |
|
'filterUrl' => Url::to($filterUrl), |
91
|
1 |
|
'filterSelector' => $filterSelector, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.