Save the new order of rows in the Sortable table database

I have researched many such problems, but I cannot find a suitable solution for me. I am using Yii2 framework in PHP nad MongoDB for my database. I am also using Yii2 sorted extension for GridView and have already installed it in my web app using composer.

Here's my GridView in my view :

<?php echo SortableGridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'code_type',
        'payroll_name',
        'cpf_type',
        [    
            'label' => 'Action',    
            'content' => function ($model, $key, $index, $column) {
                 if ( ($model->company_id != null) && ($model->active == 1) ) {
                    if(in_array($model->payroll_name, array('Basic Salary', 'Overtime Pay', 'Daily/Hourly Wage', 'Commission', 'Bonus', 'Housing Allowance', 'Educational Allowance', 'Travel Allowance', 'Marriage Allowance', 'Leave Encashment', 'Reimbursement'))){
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2']);
                    } else {
                        return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
                        .'&nbsp'
                        . Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3'])
                        .'&nbsp'
                        . Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete-earnings', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive', 'data' => ['confirm' => 'Are you sure you want to delete this earnings item?', 'method' => 'post']])
                        .'&nbsp'
                        . Html::button('Unset as Default', ['value' => (string)$model->_id, 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'unset(value)']);
                    }
                } else { 
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
                    .'&nbsp'
                    . Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3'])
                    .'&nbsp'
                    . Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete-earnings', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive', 'data' => ['confirm' => 'Are you sure you want to delete this earnings item?', 'method' => 'post']])
                    .'&nbsp'
                    . Html::button('Set as Default', ['value' => (string)$model->_id, 'class' => 'btn btn-info btn-responsive', 'onclick'=>'set(value)']);
                }
            }
        ]
    ],   
    'sortableAction' => Yii::$app->request->baseUrl . '/index.php?r=payroll-items/earnings-index'
]); ?>

      

Model:

public function behaviors()
{
    return [
        'sort' => [
            'class' => SortableGridBehavior::className(),
            'sortableAttribute' => 'sortOrder'
        ],
    ];
}

      

Controller:

public function actions()
{
    return [
        'sort' => [
            'class' => SortableGridAction::className(),
            'modelName' => PayrollItems::className(),
        ],
    ];
}

      

I found this problem but I don't know what to put inside updateworkshops.php

(see main answer) since I am using MongoDB and not MySQL.

As with my current progress, I can only drag and drop the rows of the GridView, but the new order still cannot be saved in my database.

Can someone please explain to me how to do this?

+3


source to share





All Articles