Skip to content

Commit 20284e0

Browse files
authored
wrongly replaced content
1 parent d5b101e commit 20284e0

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

SortableGridView.php

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,91 @@
22

33
namespace richardfan\sortable;
44

5-
use yii\base\Action;
6-
use yii\web\HttpException;
7-
use yii\base\InvalidConfigException;
5+
use Closure;
86

9-
class SortableAction extends Action {
7+
use yii\base\InvalidConfigException;
8+
use yii\grid\GridView;
9+
use yii\helpers\Json;
10+
use yii\grid\GridViewAsset;
11+
use yii\helpers\Html;
1012

13+
class SortableGridView extends GridView {
1114
/**
12-
* (required) The ActiveRecord Class name
15+
* (required) The URL of related SortableAction
1316
*
17+
* @see \richardfan1126\sortable\SortableAction
1418
* @var string
1519
*/
16-
public $activeRecordClassName;
20+
public $sortUrl;
1721

1822
/**
19-
* (required) The attribute name where your store the sort order.
23+
* (optional) The text shown in the model while the server is reordering model
24+
* You can use HTML tag in this attribute.
2025
*
2126
* @var string
2227
*/
23-
public $orderColumn;
28+
public $sortingPromptText = 'Loading...';
2429

2530
/**
26-
* Start index/position default 0
31+
* (optional) The text shown in alert box when sorting failed.
2732
*
28-
* @var integer
33+
* @var string
2934
*/
30-
public $startPosition = 0;
35+
public $failText = 'Fail to sort';
3136

3237
public function init(){
3338
parent::init();
34-
if(!isset($this->activeRecordClassName)){
35-
throw new InvalidConfigException("You must specify the activeRecordClassName");
39+
40+
if(!isset($this->sortUrl)){
41+
throw new InvalidConfigException("You must specify the sortUrl");
42+
}
43+
44+
GridViewAsset::register($this->view);
45+
SortableGridViewAsset::register($this->view);
46+
47+
$this->tableOptions['class'] .= ' sortable-grid-view';
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
* @see \yii\grid\GridView::renderTableRow()
53+
*/
54+
public function renderTableRow($model, $key, $index)
55+
{
56+
$cells = [];
57+
/* @var $column Column */
58+
foreach ($this->columns as $column) {
59+
$cells[] = $column->renderDataCell($model, $key, $index);
3660
}
3761

38-
if(!isset($this->orderColumn)){
39-
throw new InvalidConfigException("You must specify the orderColumn");
62+
if ($this->rowOptions instanceof Closure) {
63+
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
64+
} else {
65+
$options = $this->rowOptions;
4066
}
67+
68+
// $options['id'] = "items[]_{$model->primaryKey}";
69+
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
70+
71+
return Html::tag('tr', implode('', $cells), $options);
4172
}
73+
4274
public function run(){
43-
if(!\Yii::$app->request->isAjax){
44-
throw new HttpException(404);
45-
}
46-
if (isset($_POST['items']) && is_array($_POST['items'])) {
47-
$activeRecordClassName = $this->activeRecordClassName;
48-
foreach ($_POST['items'] as $i=>$item) {
49-
$page = $activeRecordClassName::findOne($item);
50-
//$page = $activeRecordClassName::find()->where($item);
51-
$page->updateAttributes([
52-
$this->orderColumn => $i + $startPosition,
53-
]);
54-
}
75+
foreach($this->columns as $column){
76+
if(property_exists($column, 'enableSorting'))
77+
$column->enableSorting = false;
5578
}
79+
parent::run();
80+
81+
$options = [
82+
'id' => $this->id,
83+
'action' => $this->sortUrl,
84+
'sortingPromptText' => $this->sortingPromptText,
85+
'sortingFailText' => $this->failText,
86+
'csrfTokenName' => \Yii::$app->request->csrfParam,
87+
'csrfToken' => \Yii::$app->request->csrfToken,
88+
];
89+
$options = Json::encode($options);
90+
$this->view->registerJs("jQuery.SortableGridView($options);");
5691
}
5792
}

0 commit comments

Comments
 (0)