-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabularInput.php
executable file
·145 lines (112 loc) · 4.23 KB
/
TabularInput.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
<?php
namespace wrt\tabular;
use Yii;
use yii\base\InvalidConfigException;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\i18n\Formatter;
use yii\widgets;
class TabularInput extends GridView{
/**
* @var array options for FooTables
*
'fooTableOptions'=>[
'columns'=>[
2=>'phone',
3=>'phone',
4=>'phone,tablet',
5=>'phone,tablet',
],
'configuration'=>[
'this is json encoded and passed directly to footables -- see footable documentation'
],
],
*/
public $fooTableOptions;
/**
* @var ActiveRecord that will be used for posting the data.
*/
public $postModel;
/**
* @var ActiveForm that will be posted.
*/
public $form;
/**
* @var integer the min number of rows to be displayed. 0 if not set
*/
public $minRows;
/**
* @var integer the max number of rows that can be created. no limit if not set
*/
public $maxRows;
/**
* @var array this will put an add new button at the bottom of the table
*/
public $footerAddButton;
/**
* @var string the content of the footer button
*/
private $footerButtonContent;
/**
* @var string will hold the markup for an entire row of the table with placeholders for the row index numbers.
*/
private $tableRowMarkup;
public function run(){
TabularAsset::register($this->view);
//Get rid of pagination.
$this->dataProvider->pagination->pageSize=500000;
//make sure we have a post model
if (empty($this->postModel)){
throw new InvalidConfigException("You need a postModel for the TabularInput widget");
}
if ($this->fooTableOptions){
$this->tableOptions['class'].=' footable';
/**
* :TODO: Footables does not work with 2 header rows. So the filter is disabled if we use footables
*/
$this->filterPosition=false;
foreach ($this->fooTableOptions['columns'] as $column => $device){
if(!empty($this->columns[$column-1])){
$this->columns[$column-1]->headerOptions['data']['hide']=$device;
}else{
Yii::error('You have assigned Foo Table options to non-existent columns');
}
}
$this->registerFooScript();
}
//if there are fewer rows that our $minRows then add some empty models
if ($this->dataProvider->totalCount<$this->minRows){
$nRowsToAdd=$this->minRows-$this->dataProvider->totalCount;
for ($x=1;$x<=$nRowsToAdd;$x++){
$className=$this->filterModel->className();
$newModel=new $className;
$newModels[]=$newModel;
}
$this->dataProvider->setModels($newModels);
}
if ($this->footerAddButton){
$this->showFooter=true;
if (empty($this->footerAddButton['options'])){
$this->footerAddButton['options']=['class' => 'btn btn-primary','id'=>$this->id.'-addbutton-footer'];//StringHelper::basename($this->postModel->className()).'-tabular-addbutton'];
}
$this->footerButtonContent=Html::button($this->footerAddButton['label'],$this->footerAddButton['options']);
}
$this->tableRowMarkup=$this->renderTableRow($this->postModel,'key','1');
Yii::info($this->tableRowMarkup);
parent::run();
}
public function init(){
parent::init();
}
protected function registerFooScript(){
$configure = !empty($this->fooTableOptions['configuration'])?Json::encode($this->fooTableOptions):'';
$this->view->registerJs("
jQuery('#{$this->options['id']} table').footable({$configure});
");
}
public function renderTableFooter(){
return '<tfoot><tr><td colspan="'.sizeof($this->columns).'">'.$this->footerButtonContent.'</td></tr></tfoot>';
}
}