arogachev / yii2-sortable

Sortable ActiveRecord for Yii 2 framework
Other
17 stars 9 forks source link

SortableColumn is not visible #22

Open rubenheymans opened 9 years ago

rubenheymans commented 9 years ago

I tried the most basic example, but I can't see any extra buttons in my Gridview

Behavior in model:

use arogachev\sortable\behaviors\numerical\ContinuousNumericalSortableBehavior;

[
                'class' => ContinuousNumericalSortableBehavior::className(),
                /*
                'scope' => function () {
                    return Team::find()->where(['location_id' => $this->location_id, 'department' => $this->department]);
                },
                */
                'sortAttribute' => 'position',
            ],

Index view:

use arogachev\sortable\grid\SortableColumn;

<div class="question-index" id="question-sortable">

    <?php // Gridview ?>
    <?php echo GridView::widget([
        'id' => 'sortable',
        'dataProvider'=> $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => SortableColumn::className(),
                'gridContainerId' => 'question-sortable',
            ],
            'name',
            [
                'attribute' => 'location',
                'value' => 'location.name',
                'filter' => ArrayHelper::map(Location::find()->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'),
                'width' => '15%',
            ],
            [
                'attribute' => 'department',
                'value' => function($model) {
                    return $model->getDepartments($model->department);
                },
                'filter' => Team::getDepartments(),
                'width' => '15%',
            ],
            [
                'class' => 'kartik\grid\BooleanColumn',
                'attribute' => 'active',
                'vAlign'=> 'middle'
            ],
            [
                'class' => 'kartik\grid\ActionColumn',
                'template' => '{update} {delete} {active}',
                'buttons' => [
                    'active' => function ($url, $model) {
                        if ($model->active == true) {
                            $icon = 'glyphicon-eye-open';
                        } else {
                            $icon = 'glyphicon-eye-close';
                        }

                        return Html::a('<span class="glyphicon ' . $icon . '"></span>', $url, [
                            'title' => Yii::t('app', 'Toggle active'),
                            'data-pjax' => '0',
                            'data-toggleable' => 'true',
                            'data-toggle-id' => $model->id,
                            'data-toggle' => 'tooltip',
                        ]);
                    },
                ],
                'updateOptions' => ['title' => Yii::t('app', 'Update'), 'data-toggle' => 'tooltip'],
                'deleteOptions' => ['title' => Yii::t('app', 'Delete'), 'data-toggle' => 'tooltip'],
                'width' => '120px',
            ],
        ],
    ]);
    ?>
    </div>
arogachev commented 9 years ago

There are certain conditions in order to SortableColumn to be visible:

In your case seems like you missed to add sort to getting models query.

It's a bit unclear from documentation, I will add these details into README later.

arogachev commented 9 years ago

Also you should rename <div class="question-index" id="question-sortable"> according to your model meaning, it's just an example. For example post-index and post-sortable.

arogachev commented 9 years ago

I'll consider adding an option to always show SortableColumn (except case when permissions check was failed).

arogachev commented 9 years ago

@rubenheymans Did you solve the problem?

rubenheymans commented 9 years ago

I did not solve it because I'm using another sortable behavior. This one looks better but I don't have time now to implement it.

aelsaidy commented 8 years ago

Hmm, got same problem , and I couldnt solve yet

antdel commented 8 years ago

I solved this by explicitly setting the ActiveDataProvider property 'defaultSort', like this (in controller):

$dataProvider->sort = [ 'defaultOrder'=>['display_order'=>SORT_ASC] ];