-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathXEditableColumn.php
86 lines (68 loc) · 1.99 KB
/
XEditableColumn.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
<?php
/**
* @inheritdoc
*/
namespace mcms\xeditable;
use yii\grid\DataColumn;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
class XEditableColumn extends DataColumn
{
/**
* @var array defaults for editable configuration
*/
public $pluginOptions = [];
public $dataType = 'text';
public $pk='id';
public $dataTitle = '';
public $editable = '';
private $view = null;
public $url = null;
public function init()
{
parent::init();
$this->registerAssets();
}
/**
* @inheritdoc
*/
protected function getDataCellContent($model, $key, $index)
{
if (empty($this->url)) {
$this->url = \Yii::$app->urlManager->createUrl($_SERVER['REQUEST_URI']);
}
if (empty($this->value)) {
$value = ArrayHelper::getValue($model, $this->attribute);
} else {
$value = call_user_func($this->value, $model, $index, $this);
}
$value = '<a href="#" data-name="'.$this->attribute.'" data-value="' . $model->{$this->attribute} . '" class="editable" data-type="' . $this->dataType . '" data-pk="' . $model->{$this->pk} . '" data-url="' . $this->url . '" data-title="' . $this->dataTitle . '">' . $value . '</a>';
return $value;
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
return $this->grid->formatter->format($this->getDataCellContent($model, $key, $index), $this->format);
}
/**
* @inheritdoc
*/
public function registerAssets()
{
$config = new XEditableConfig();
if(isset($this->pluginOptions['mode']) && is_array($this->pluginOptions)){
$config->mode = $this->pluginOptions['mode'];
}
if(isset($this->pluginOptions['form']) && is_array($this->pluginOptions)){
$config->form = $this->pluginOptions['form'];
}
$config->registerDefaultAssets();
$this->view = \Yii::$app->getView();
XEditableAsset::register($this->view);
$this->editable = Json::encode($this->editable);
$this->view->registerJs('$(".editable[data-name=' . $this->attribute . ']").editable(' . $this->editable . ');');
}
}