Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing sorted item causes it to move to the end of list #28

Open
Red-Teapot opened this issue Aug 25, 2016 · 4 comments
Open

Editing sorted item causes it to move to the end of list #28

Red-Teapot opened this issue Aug 25, 2016 · 4 comments

Comments

@Red-Teapot
Copy link

I have a problem. Assume that we have Tag model with following fields (some fields aren't listed as they're not needed):

  • ord - index of this item used for sorting
  • section_number - tags are split into several sections, so this field contains section number

And here is behavior config:

'class' => IntervalNumericalSortableBehavior::className(),
'sortAttribute' => 'ord',
'scope' => function() {
    return Tag::find()->where(['section_number' => $this->section_number]);
},

So, we use our ord for sorting and section_number as scope.

Sorting itself works fine. But if, for example, I will pull item from the end to the beginning of list and then edit it (I can change section_number while editing, but I am not doing it actually), item appears at the end.

Sorry for my bad English.

@arogachev
Copy link
Owner

What exactly do you edit? And please show code related with editing.

@Red-Teapot
Copy link
Author

My Tag model contains following fields:

  • id
  • name - string, tag name
  • description_name - string, used in some places
  • hint - string, tooltip text (used in some places)
  • well_text - string, used in some places
  • section_number - tag's section number
  • ord - tag order

Behavior config is shown in my comment above.

This is my tag list view fragment:

<?php Pjax::begin(); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => SortableColumn::className(),
                'gridContainerId' => 'tag-sortable',
                'baseUrl' => '/sort/',
                'confirmMove' => false,
            ],
            [
                'attribute' => 'name',
                'format' => 'html',
                'value' => function($model) {
                    return Html::tag('span', $model->name, [
                        'title' => $model->hint,
                    ]);
                },
            ],
            'description_name',
            'section_number',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end(); ?>

And here is my tag edit form:

<div class="tag-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'well_text')->textarea() ?>

    <?= $form->field($model, 'description_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'hint')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'section_number')->dropDownList([
        '1' => '1',
        '2' => '2',
        '3' => '3',
        '4' => '4'
    ]); ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ?
            Yii::t('modules/catalog/admin', 'add_button') :
            Yii::t('modules/catalog/admin', 'update_button'),
            ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

Controller's update action:

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

So, I can edit tag's name, well_text, hint, description_name, section_number. And only section_number is related to sorting. But, as said previously, tag will move to the end even if section_number stays same.

@arogachev
Copy link
Owner

If you are modifying scope attribute (section_number in your case), this is expected behavior, but if it stays unchanged - seems like a bug. I will try to reproduce this issue.

@mrblc
Copy link

mrblc commented Mar 14, 2018

Value loaded from database is nubmer, but value submited from form is string.

You are checking
$this->model->isAttributeChanged($attribute)
but, that defaults to strict compare.

$this->model->isAttributeChanged($attribute, false)
would compare with automatic casting and same value submited wouldn't trigger change.

Other possible solution, on the user end, is to define filters to cast value in model rules().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants