-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEditableGridColumn.php
82 lines (71 loc) · 2.4 KB
/
EditableGridColumn.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
<?php
/**
* EditableGridColumn class file.
* Makes editable grid column.
*
* @author Dyomin Dmitry <[email protected]>
* @link http://size.perm.ru/yii-editable-grid
* @copyright 2014 SiZE
*/
class EditableGridColumn extends CDataColumn {
/**
* @var string Cell tag. Supported: textField, dropDownList
*/
public $tag;
/**
* @var array For generating the list options (value=>display)
*/
public $tagData;
/**
* @var array The HTML options for the cell tag.
*/
public $tagHtmlOptions = array();
/**
* @var int Grid row counter
*/
private static $_prevRowNum;
/**
* Renders the data cell content.
* This method evaluates {@link value} or {@link name} and renders the result.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent( $row, $data ){
if ( $this->tag === null ) {
parent::renderDataCellContent( $row, $data );
} else {
$is_model = ( $data instanceof CModel ) ? true : false;
// Запишем идентификатор для существующей записи
if ( self::$_prevRowNum !== $row ) {
self::$_prevRowNum = $row;
$primary_name = str_replace( array( '{gridNum}', '{rowNum}' ), array( $this->grid->getGridCounter(), $row ), $this->grid->primaryKey );
$primary_real = preg_replace( '#.*\](.*)$#', '\\1', $this->grid->primaryKey );
if ( isset( $data[ $primary_real ] ) ) {
if ( $is_model ) {
echo CHtml::activeHiddenField( $data, $primary_name, $this->grid->primaryKeyHtmlOptions );
} else {
echo CHtml::hiddenField( $primary_name, $data[ $primary_real ], $this->grid->primaryKeyHtmlOptions );
}
}
}
$real = preg_replace( '#.*\](.*)$#', '\\1', $this->name );
$name = str_replace( array( '{gridNum}', '{rowNum}' ), array( $this->grid->getGridCounter(), $row ), $this->name );
switch( $this->tag ){
case 'textField':
if ( $is_model ) {
echo CHtml::activeTextField( $data, $name, $this->tagHtmlOptions );
} else {
echo CHtml::textField( $name, $data[ $real ], $this->tagHtmlOptions );
}
break;
case 'dropDownList':
if ( $is_model ) {
echo CHtml::activeDropDownList( $data, $name, $this->tagData, $this->tagHtmlOptions );
} else {
echo CHtml::dropDownList( $name, $data[ $real ], $this->tagData, $this->tagHtmlOptions );
}
break;
}
}
}
}