Class rootlocal\widgets\sortable\SortableGridAction

Inheritancerootlocal\widgets\sortable\SortableGridAction » yii\base\Action

Action for sortable Yii2 GridView widget.

example:

public function actions()
{
   return [
      'sort' => [
         'class' => SortableGridAction::class,
         'model' => Model::class,
      ],
  ];
}

Public Properties

Hide inherited properties

Property Type Description Defined By
$model \yii\db\ActiveRecordInterface|array|string rootlocal\widgets\sortable\SortableGridAction

Property Details

Hide inherited properties

$model public property
public \yii\db\ActiveRecordInterface|array|string $model null

Method Details

Hide inherited methods

getModel() public method

public rootlocal\widgets\sortable\SortableGridBehaviorInterface|\yii\db\ActiveRecord getModel ( )

                public function getModel()
{
    return $this->_model;
}

            
run() public method

public array|string[] run ( )
throws \yii\web\BadRequestHttpException
throws \yii\base\InvalidConfigException
throws Throwable

                public function run(): array
{
    Yii::$app->response->format = Response::FORMAT_JSON;
    $request = Json::decode(Yii::$app->request->getRawBody());
    $result = [];
    if (!$this->_model->hasMethod('gridSort') || !$this->_model->hasMethod('gridSortUpOrDownButton')) {
        throw new InvalidConfigException('Not found right `SortableGridBehavior` behavior Model class.');
    }
    if (!array_key_exists('action', $request)) {
        return ['status' => 'error', 'message' => 'parameter "action" not requested'];
    }
    switch ($request['action']) {
        case 'up':
        case 'down':
            $result = $this->getModel()->gridSortUpOrDownButton($request['action'], $request['id']);
            break;
        case 'sortable':
            if (array_key_exists('items', $request) && !empty($request['items'])) {
                $result = $this->getModel()->gridSort($request['items']);
            } else {
                return ['status' => 'error', 'message' => 'parameter "items" not requested'];
            }
            break;
    }
    return ['status' => empty($result) ? 'error' : 'success', 'result' => Json::htmlEncode($result), 'action' => $request['action']];
}

            
setModel() public method

public void setModel ( $model )
$model string|\yii\db\ActiveRecord|null
throws \yii\base\InvalidConfigException

                public function setModel($model): void
{
    if (is_string($model)) {
        /** @var SortableGridBehaviorInterface $obj */
        $obj = Yii::createObject(['class' => $model]);
        $this->_model = $obj;
        return;
    }
    if (is_array($model) && array_key_exists('class', $model)) {
        /** @var SortableGridBehaviorInterface $obj */
        $obj = Yii::createObject($model);
        $this->model = $obj;
        return;
    }
    if ($model instanceof ActiveRecord) {
        /** @var SortableGridBehaviorInterface $obj */
        $obj = $model;
        $this->model = $obj;
        return;
    }
    throw new  InvalidConfigException('No valid "model" attribute');
}