PHP code example of richardfan1126 / yii2-sortable-gridview

1. Go to this page and download the library: Download richardfan1126/yii2-sortable-gridview 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/ */

    

richardfan1126 / yii2-sortable-gridview example snippets


use richardfan\sortable\SortableAction;

public function actions(){
    return [
        'sortItem' => [
            'class' => SortableAction::className(),
            'activeRecordClassName' => YourActiveRecordClass::className(),
            'orderColumn' => 'name_of_field_storing_ordering',
            'startPosition' => 1, // optional, default is 0
        ],
        // your other actions
    ];
}

use richardfan\sortable\SortableGridView;

<?= SortableGridView::widget([
    'dataProvider' => $dataProvider,
    
    // you can choose how the URL look like,
    // but it must match the one you put in the array of controller's action()
    'sortUrl' => Url::to(['sortItem']),
    
    'columns' => [
        // Data Columns
    ],
]); 

$dataProvider->pagination = false;

use richardfan\sortable\SortableAction;

public function actions(){
    return [
        'sortItem' => [
            'class' => SortableAction::className(),
            'activeRecordClassName' => Articles::className(),
            'orderColumn' => 'sortOrder',
        ],
        // your other actions
    ];
}

use richardfan\sortable\SortableGridView;

<?= SortableGridView::widget([
    'dataProvider' => $dataProvider,
    
    // SortableGridView Configurations
    'sortUrl' => Url::to(['sortItem']),
    'sortingPromptText' => 'Loading...',
    'failText' => 'Fail to sort',
    'moveItem' => '.moveItem', 
    
    'columns' => [
        [
            'content' => function(){
                return "<span class='glyphicon glyphicon-resize-vertical'></span>";
            },
            'contentOptions' => ['style'=>'cursor:move;', 'class' => 'moveItem'],
        ],

        // Data Columns
    ],
]);