Sorting items Drag Drop AJAX

pceuropa/yii2-ajax-sort

Installation

composer require pceuropa/yii2-ajax-menu dev-master

Usage

use yii\helpers\Url;
use pceuropa\sort\SortWidget;

echo SortWidget::widget([
	'image_url' => Url::to('@web/images/'),
	'data' => [
		['id' => 1, 'image_name'  => 'firefox.jpg', 'title' => 'text alternative'],
		['id' => 2, 'image_name'  => 'opera.jpg'],
		['id' => 3, 'image_name'  => 'chrome.jpg'],
		['id' => 4, 'image_name'  => 'ie.jpg'],
		['id' => 5, 'image_name'  => 'safari.jpg'],
		['id' => 6, 'title'  => 'text element', 'url' => 'https://'],
	], 
]); 

Controller example

public function actionSort(){
    
	$request = Yii::$app->request;
	
	if ($request->isAjax && $request->post('serialize')){ // ajax sort request
	
		$array = $request->post('array');
		
		foreach ($array as $key => $val){
	
			$m = $this->findModel($val);
			$m->serialize = $key;
			$m->save();
		}
		return  \yii\helpers\Json::encode(['success' => true,]);
	}
	
	$model = new Model;
	
	if ($model->load($request->post()) && $model->save() ) { // ex. submit form upload file
		return $this->render('index', ['model' => $model]);
	} 
	
	return $this->render('index');
	
}

Demo

 Request Ajax:
Response Json: