forked from sdelfi/yii2-widget-datatables
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataTables.php
125 lines (104 loc) · 3.74 KB
/
DataTables.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
<?php
/**
* @copyright Federico Nicolás Motta
* @author Federico Nicolás Motta <[email protected]>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @package yii2-widget-datatables
*/
namespace paskuale75\datatables;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Datatables Yii2 widget
* @author Federico Nicolás Motta <[email protected]>
*/
class DataTables extends \yii\grid\GridView
{
/**
* @var array the HTML attributes for the container tag of the datatables view.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array the HTML attributes for the datatables table element.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $tableOptions = ["class" => "table table-striped table-bordered", "cellspacing" => "0", "width" => "100%"];
/**
* @var array the HTML attributes for the datatables table element.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $clientOptions = [];
/**
* Runs the widget.
*/
public function run()
{
$clientOptions = $this->getClientOptions();
$view = $this->getView();
$id = $this->tableOptions['id'];
//Bootstrap4 Asset by default
DataTablesBootstrapAsset::register($view);
//Language
$language = DataTablesLanguage::run();
$clientOptions["language"] = $language;
//TableTools Asset if needed
if (isset($clientOptions["tableTools"])) {
DataTablesButtonsBs4Asset::register($view);
DataTablesSelectBs4Asset::register($view);
}
$options = Json::encode($clientOptions);
$view->registerJs("jQuery('#$id').DataTable($options);");
//base list view run
if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
$content = preg_replace_callback("/{\\w+}/", function ($matches) {
$content = $this->renderSection($matches[0]);
return $content === false ? $matches[0] : $content;
}, $this->layout);
} else {
$content = $this->renderEmpty();
}
$tag = ArrayHelper::remove($this->options, 'tag', 'div');
echo Html::tag($tag, $content, $this->options);
}
/**
* Initializes the datatables widget disabling some GridView options like
* search, sort and pagination and using DataTables JS functionalities
* instead.
*/
public function init()
{
parent::init();
//disable filter model by grid view
$this->filterModel = null;
//disable sort by grid view
$this->dataProvider->sort = false;
//disable pagination by grid view
$this->dataProvider->pagination = false;
//layout showing only items
$this->layout = "{items}";
//the table id must be set
if (!isset($this->tableOptions['id'])) {
$this->tableOptions['id'] = 'datatables_' . $this->getId();
}
}
/**
* Returns the options for the datatables view JS widget.
* @return array the options
*/
protected function getClientOptions()
{
return $this->clientOptions;
}
public function renderTableBody()
{
$models = array_values($this->dataProvider->getModels());
if (count($models) === 0) {
return "<tbody>\n</tbody>";
} else {
return parent::renderTableBody();
}
}
}