| 
2 | 2 | 
 
  | 
3 | 3 | namespace richardfan\sortable;  | 
4 | 4 | 
 
  | 
5 |  | -use yii\base\Action;  | 
6 |  | -use yii\web\HttpException;  | 
7 |  | -use yii\base\InvalidConfigException;  | 
 | 5 | +use Closure;  | 
8 | 6 | 
 
  | 
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;  | 
10 | 12 | 
 
  | 
 | 13 | +class SortableGridView extends GridView {  | 
11 | 14 |     /**  | 
12 |  | -     * (required) The ActiveRecord Class name  | 
 | 15 | +     * (required) The URL of related SortableAction  | 
13 | 16 |      *  | 
 | 17 | +     * @see \richardfan1126\sortable\SortableAction  | 
14 | 18 |      * @var string  | 
15 | 19 |      */  | 
16 |  | -    public $activeRecordClassName;  | 
 | 20 | +    public $sortUrl;  | 
17 | 21 | 
 
  | 
18 | 22 |     /**  | 
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.  | 
20 | 25 |      *  | 
21 | 26 |      * @var string  | 
22 | 27 |      */  | 
23 |  | -    public $orderColumn;  | 
 | 28 | +    public $sortingPromptText = 'Loading...';  | 
24 | 29 | 
 
  | 
25 | 30 |     /**  | 
26 |  | -     * Start index/position default 0  | 
 | 31 | +     * (optional) The text shown in alert box when sorting failed.  | 
27 | 32 |      *  | 
28 |  | -     * @var integer  | 
 | 33 | +     * @var string  | 
29 | 34 |      */  | 
30 |  | -    public $startPosition = 0;  | 
 | 35 | +    public $failText = 'Fail to sort';  | 
31 | 36 | 
 
  | 
32 | 37 |     public function init(){  | 
33 | 38 |         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);  | 
36 | 60 |         }  | 
37 | 61 | 
 
  | 
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;  | 
40 | 66 |         }  | 
 | 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);  | 
41 | 72 |     }  | 
 | 73 | + | 
42 | 74 |     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;  | 
55 | 78 |         }  | 
 | 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);");  | 
56 | 91 |     }  | 
57 | 92 | }  | 
0 commit comments