PHP code example of ale10257 / yii2-sortable-widget

1. Go to this page and download the library: Download ale10257/yii2-sortable-widget library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ale10257 / yii2-sortable-widget example snippets



use ale10257\sortable\SortableWidget;
use yii\helpers\Url;
use yii\grid\GridView;

// the data-id attribute is save the sort order after dragging

SortableWidget::widget([
//    'cssSelector' => '#my-id', // default .sortable
//     Details for pluginOptions at https://github.com/SortableJS/Sortable
//    'pluginOptions' => [
//        'delay' => 150, 
//        'onSort' => '(e) => {}',
//        'onMove' => '(e) => {
//            if (e.dragged.dataset.excluded !== undefined) {
//               return false;
//            }
//        }'  
//        ...
//    ]
]);

    public function actions(): array
    {
        return [
            'sort' => [
                'class' => \ale10257\sortable\SortAction::class,
                'modelClass' => MyActiveRecordModel::class
            ]
        ];
    }


class MyModel extends \yii\db\ActiveRecord implements \ale10257\sortable\ISortableModel 
{

    public function behaviors(): array
    {
        return [
        ...
            [
                'class' => \ale10257\sortable\SortableBehavior::class,
                'serviceClass' => \ale10257\sortable\SortableServicePostgres::class // default SortableService,
                'sortField' => 'my_field', // sorting field, default sort
                'step' => 1, // default 10
                'addToBeginning' => true // default false, new entries are added to the end of the list
            ]
        ...    
        ];
    }
    
    /**
    * @return array|string|null condition WHERE for sort, example ['parent_id' => $this->parent_id]
    */
    public function sortableCondition() 
    {
        return null;
    }
}

    public function events(): array
    {
        return [
            BaseActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
            BaseActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
        ];
    }

    public fuction afterDelete
    {
        ...
        $service = ale10257\sortable\ServiceFactory::getServiceFromModel($this);
        $service->delete();
    }


docker-compose up -d && docker-compose run --rm php composer install && docker-compose run --rm php bash -c './vendor/bin/codecept run unit' && docker-compose down