-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEditableGrid.php
175 lines (143 loc) · 4.74 KB
/
EditableGrid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
Yii::import('zii.widgets.grid.CGridView');
/**
* EditableGrid class file.
*
* @author Demin Dmitry <[email protected]>
* @link http://size.perm.ru/yii-editable-grid
* @copyright 2014 SiZE
*/
class EditableGrid extends CGridView {
/**
* @var int Grids counter
*/
private static $_grid_counter = 0;
/**
* @var int Manual grid number
*/
public $gridNum;
/**
* @var string The label for the add row button. Defaults to "Add new row"
*/
public $buttonCreateRowLabel;
/**
* @var string A javascript function that will be invoked after the removing row
*/
public $afterCreateRow;
/**
* @var string A javascript function to be invoked when the button create row is clicked
*/
public $buttonCreateRowClick;
/**
* @var array The HTML options for the create row button tag
*/
public $buttonCreateRowOptions = array(
'class' => 'new-grid-row'
);
/**
* @var string The HTML template for the new row
*/
public $rowTemplate;
/**
* @var string the HTML TextArea tag attribute ID, generated to get template
*/
protected $rowTemplateId;
/**
* @var string Row primary key. Used to generate hidden field with primaryKey from DB
*/
public $primaryKey = '[{gridNum}][{rowNum}]id';
/**
* @var array the HTML options for the hidden field primaryKey
*/
public $primaryKeyHtmlOptions = array();
/**
* @var int the number of table body rows that can be selected
*/
public $selectableRows = 0;
/**
* @var bool whether to restore deleted rows. Set this property to true to
* enable lazy delete.
*/
public $restoreDeletedRows = false;
protected $markDeletedRowsInputId;
public function init(){
parent::init();
self::$_grid_counter++;
$this->rowTemplateId = 'row-template-'.$this->id;
if ( strpos( $this->template, '{buttonCreateRow}' ) !== false ) {
$this->initButtonCreateRow();
if( !isset( $this->buttonCreateRowOptions['class'] ) ) {
$this->buttonCreateRowOptions['class'] = 'buttonCreateRow';
}
if ( !( $this->buttonCreateRowClick instanceof CJavaScriptExpression ) ) {
$this->buttonCreateRowClick = new CJavaScriptExpression( $this->buttonCreateRowClick );
}
}
if ( $this->restoreDeletedRows ) {
$this->markDeletedRowsInputId = 'mark-deleted-'.$this->id;
$this->initRestoreDeletedRowsInput();
}
}
/**
* @return int
*/
public function getGridCounter(){
if ( $this->gridNum === null ) {
return self::$_grid_counter;
} else {
return $this->gridNum;
}
}
public function initButtonCreateRow(){
if ( $this->afterCreateRow === null ) {
$this->afterCreateRow = 'function(){}';
}
$class = '';
// @todo rowCssClassExpression support (@see CGridView.renderTableRow)
if ( is_array( $this->rowCssClass ) && ($n = count( $this->rowCssClass ) ) > 0 ) {
$class = "
var rowCssClass = ".CJSON::encode( $this->rowCssClass ).",
rowClass = rowCssClass[ ( rowNum % ".$n." ) ];
if ( /<tr[^>]+class[^>]+/.test(rowTpl) ) {
rowTpl = rowTpl.replace(/<tr([^>]+)class=(\"|')([^'\"]*)(\"|')/,'<tr$1class=$2$3 '+rowClass+'$4');
} else {
rowTpl = rowTpl.replace(/(<tr[^>]*)/,'$1 class=\"'+rowClass+'\"');
}
";
}
$this->buttonCreateRowClick = <<<EOD
function(){
var th = this,
afterCreateRow = {$this->afterCreateRow},
rowTpl = $('#{$this->rowTemplateId}').val(),
rowNum = $('#{$this->id} tbody tr').length,
placeholders = [{p:'gridNum',v:'{$this->getGridCounter()}'},{p:'rowNum',v:rowNum}];
{$class}
for(var i=0; i<placeholders.length; i++){
rowTpl = rowTpl.replace(new RegExp("\{"+placeholders[i].p+"\}", "g"), placeholders[i].v);
}
$('#{$this->id} tbody').append( rowTpl );
afterCreateRow();
}
EOD;
}
public function registerClientScript(){
parent::registerClientScript();
if ( strpos( $this->template, '{buttonCreateRow}' ) !== false ) {
$function = CJavaScript::encode( $this->buttonCreateRowClick );
$class = preg_replace( '/\s+/', '.', $this->buttonCreateRowOptions[ 'class' ] );
$js = "jQuery(document).on('click','#{$this->id} .{$class}',$function);";
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, $js);
}
}
public function renderButtonCreateRow(){
if ( $this->buttonCreateRowLabel === null ) {
$this->buttonCreateRowLabel = Yii::t( 'editablegrid.editablegrid', 'Add new row' );
}
echo CHtml::button( $this->buttonCreateRowLabel, $this->buttonCreateRowOptions );
echo CHtml::textArea( '', $this->rowTemplate, array( 'id' => $this->rowTemplateId, 'style' => 'display: none;' ) );
}
public function initRestoreDeletedRowsInput(){
echo CHtml::hiddenField( 'delete', '', array( 'id' => $this->markDeletedRowsInputId ) );
}
}