Skip to content

Commit 63ab6ce

Browse files
committed
initial commit
0 parents  commit 63ab6ce

File tree

11 files changed

+845
-0
lines changed

11 files changed

+845
-0
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# cache directories
2+
Thumbs.db
3+
*.DS_Store
4+
*.empty
5+
6+
#phpstorm project files
7+
.idea
8+
9+
#netbeans project files
10+
nbproject
11+
12+
#eclipse, zend studio, aptana or other eclipse like project files
13+
.buildpath
14+
.project
15+
.settings
16+
17+
18+
# mac deployment helpers
19+
switch
20+
index

EditableColumn.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
4+
* @link http://2amigos.us
5+
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6+
*/
7+
namespace dosamigos\grid;
8+
9+
10+
use dosamigos\editable\EditableAddressAsset;
11+
use dosamigos\editable\EditableBootstrapAsset;
12+
use dosamigos\editable\EditableComboDateAsset;
13+
use dosamigos\editable\EditableDatePickerAsset;
14+
use dosamigos\editable\EditableDateTimePickerAsset;
15+
use dosamigos\editable\EditableSelect2Asset;
16+
use dosamigos\editable\EditableWysiHtml5Asset;
17+
use yii\base\InvalidConfigException;
18+
use yii\grid\DataColumn;
19+
use yii\helpers\ArrayHelper;
20+
use yii\helpers\Html;
21+
use yii\helpers\Url;
22+
23+
/**
24+
* EditableColumn adds X-Editable capabilities to a column
25+
*
26+
* @author Antonio Ramirez <[email protected]>
27+
* @link http://www.ramirezcobos.com/
28+
* @link http://www.2amigos.us/
29+
* @package common\extensions\grid
30+
*/
31+
class EditableColumn extends DataColumn
32+
{
33+
/**
34+
* @var array the options for the X-editable.js plugin.
35+
* Please refer to the X-editable.js plugin web page for possible options.
36+
* @see http://vitalets.github.io/x-editable/docs.html#editable
37+
*/
38+
public $editableOptions = [];
39+
/**
40+
* @var string suffix substituted to a name class of the tag <a>
41+
*/
42+
public $classSuffix;
43+
/**
44+
* @var string the url to post
45+
*/
46+
public $url;
47+
/**
48+
* @var string the type of editor
49+
*/
50+
public $type = 'text';
51+
52+
/**
53+
* @inheritdoc
54+
* @throws \yii\base\InvalidConfigException
55+
*/
56+
public function init()
57+
{
58+
if ($this->url === null) {
59+
throw new InvalidConfigException("'Url' property must be specified.");
60+
}
61+
62+
parent::init();
63+
64+
$this->format = 'raw';
65+
66+
$rel = $this->attribute . '_editable' . $this->classSuffix;
67+
$this->options['pjax'] = '0';
68+
$this->options['rel'] = $rel;
69+
70+
$this->registerClientScript();
71+
}
72+
73+
/**
74+
* @inheritdoc
75+
*/
76+
protected function renderDataCellContent($model, $key, $index)
77+
{
78+
79+
$value = parent::renderDataCellContent($model, $key, $index);
80+
81+
foreach ($this->editableOptions as $prop => $v) {
82+
$this->options['data-' . $prop] = $v;
83+
}
84+
$url = (array)$this->url;
85+
$this->options['data-url'] = Url::to($url);
86+
$this->options['data-pk'] = $key;
87+
$this->options['data-name'] = $this->attribute;
88+
$this->options['data-type'] = $this->type;
89+
90+
return Html::a($value, null, $this->options);
91+
}
92+
93+
/**
94+
* Registers required script to the columns work
95+
*/
96+
protected function registerClientScript()
97+
{
98+
$view = $this->grid->getView();
99+
$language = ArrayHelper::getValue($this->editableOptions, 'language');
100+
101+
switch ($this->type) {
102+
case 'address':
103+
EditableAddressAsset::register($view);
104+
break;
105+
case 'combodate':
106+
EditableComboDateAsset::register($view);
107+
break;
108+
case 'date':
109+
if ($language) {
110+
EditableDatePickerAsset::register(
111+
$view
112+
)->js[] = 'vendor/js/locales/bootstrap-datetimepicker.' . $language . '.js';
113+
} else {
114+
EditableDatePickerAsset::register($view);
115+
}
116+
break;
117+
case 'datetime':
118+
if ($language) {
119+
EditableDateTimePickerAsset::register(
120+
$view
121+
)->js[] = 'vendor/js/locales/bootstrap-datetimepicker.' . $language . '.js';
122+
} else {
123+
EditableDateTimePickerAsset::register($view);
124+
}
125+
break;
126+
case 'select2':
127+
EditableSelect2Asset::register($view);
128+
break;
129+
case 'wysihtml5':
130+
$language = $language ? : 'en-US';
131+
EditableWysiHtml5Asset::register(
132+
$view
133+
)->js[] = 'vendor/locales/bootstrap-wysihtml5.' . $language . '.js';
134+
break;
135+
default:
136+
EditableBootstrapAsset::register($view);
137+
}
138+
139+
EditableColumnAsset::register($view);
140+
$rel = $this->options['rel'];
141+
$selector = "a[rel=\"$rel\"]";
142+
$grid = "#{$this->grid->id}";
143+
$js[] = ";jQuery('$selector').editable();";
144+
$js[] = "yii.editableColumn.registerHandler('$grid', '$selector');";
145+
$view->registerJs(implode("\n", $js));
146+
}
147+
}

EditableColumnAsset.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
*
4+
* ToggleColumnAsset.php
5+
*
6+
* Date: 03/04/14
7+
* Time: 23:19
8+
* @author Antonio Ramirez <[email protected]>
9+
* @link http://www.ramirezcobos.com/
10+
* @link http://www.2amigos.us/
11+
*/
12+
13+
namespace dosamigos\grid;
14+
15+
use yii\web\AssetBundle;
16+
17+
class EditableColumnAsset extends AssetBundle
18+
{
19+
public $sourcePath = '@2amigos/yii2-grid-view-library/assets/editable';
20+
21+
public $js = ['js/dosamigos-editable.column.js'];
22+
23+
public $depends = [
24+
'yii\web\YiiAsset',
25+
];
26+
27+
}

LICENSE.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2013, 2amigOS! Consulting Group LLC.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
Redistributions in binary form must reproduce the above copyright notice, this
11+
list of conditions and the following disclaimer in the documentation and/or
12+
other materials provided with the distribution.
13+
14+
Neither the name of 2amigOS! Consulting Group, LLC. nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
GridView Extensions Library for Yii2
2+
====================================
3+
4+
This library holds extensions specifically created to enhance Yii2's GridView Widget. We expect it to grow... :)
5+
6+
7+
Installation
8+
------------
9+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
10+
11+
Either run
12+
13+
```
14+
php composer.phar require "2amigos/yii2-grid-view-library" "*"
15+
```
16+
or add
17+
18+
```json
19+
"2amigos/yii2-grid-view-library" : "*"
20+
```
21+
22+
to the require section of your application's `composer.json` file.
23+
24+
25+
Usage
26+
-----
27+
28+
***Using ToggleColumn and ToggleAction***
29+
30+
```
31+
// on your controller
32+
33+
public function actions()
34+
{
35+
return [
36+
// ...
37+
'toggle' => [
38+
'class' => ToggleAction::className(),
39+
'modelClass' => Lang::className(),
40+
'onValue' => Lang::STATUS_ACTIVE,
41+
'offValue' => Lang::STATUS_NOT_ACTIVE
42+
],
43+
// ...
44+
];
45+
}
46+
47+
48+
// on your grid
49+
// ... other columns above?
50+
[
51+
'class' => \dosamigos\grid\ToggleColumn::className(),
52+
'attribute' => 'status',
53+
'onValue' => Status::STATUS_ACTIVE,
54+
'onLabel' => 'Active',
55+
'offLabel' => 'Not active',
56+
'contentOptions' => ['class' => 'text-center'],
57+
'afterToggle' => 'function(r, data){if(r){console.log("done", data)};}',
58+
'filter' => [
59+
Status::STATUS_ACTIVE => 'Active',
60+
Status::STATUS_DELETED => 'Not active'
61+
]
62+
],
63+
// ...
64+
```
65+
***Using EditableColumn and EditableAction***
66+
As you will soon realize, this column requires [2amigos/yii2-editable-widget](https://github.com/2amigos/yii2-editable-widget). So, for further information about the options you have to configure the Column please go to [the widget's repository](https://github.com/2amigos/yii2-editable-widget).
67+
68+
```
69+
// on your controller
70+
71+
public function actions()
72+
{
73+
return [
74+
// ...
75+
'editable' => [
76+
'class' => EditableAction::className(),
77+
'modelClass' => Lang::className(),
78+
'forceCreate' => false
79+
]
80+
// ...
81+
];
82+
83+
84+
// on your grid
85+
// ... other columns above?
86+
[
87+
'class' => \dosamigos\grid\EditableColumn::className(),
88+
'attribute' => 'name',
89+
'url' => ['editable'],
90+
'type' => 'date',
91+
'editableOptions' => [
92+
'mode' => 'inline',
93+
]
94+
],
95+
// ...
96+
```
97+
98+
99+
> [![2amigOS!](http://www.gravatar.com/avatar/55363394d72945ff7ed312556ec041e0.png)](http://www.2amigos.us)
100+
<i>Web development has never been so fun!</i>
101+
[www.2amigos.us](http://www.2amigos.us)

0 commit comments

Comments
 (0)