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

Modify yii\data\Sort to work also with $_POST #5498

Closed
wants to merge 1 commit into from
Closed

Modify yii\data\Sort to work also with $_POST #5498

wants to merge 1 commit into from

Conversation

SDKiller
Copy link
Contributor

Currently Sort works only with $_GET sort parameter.
Thus, for example combination of GridView and search form with method post does not work at all.

Proposed change is an attempt to make Sort accept $_POST also.

Example usage

Search model:

...
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'transaction_date' => SORT_DESC
                ],
                'dataMethod' => 'post',
                'formName' => $this->formName()
            ]
        ]);
...

GridView:

...
            'columns' => [
               ...
                [
                    'attribute' => 'transaction_date',
                    'sortLinkOptions' => [
                        'data-method' => 'post',
                        'href' => '#',
                        'onclick' => 'jQuery(\'#'.Html::getInputId($searchModel, $dataProvider->getSort()->sortParam).'\').val(jQuery(this).data(\'sort\')).closest(\'form\').submit();'
                    ],
                ],
...

Search form:

...
<?php echo Html::hiddenInput(Html::getInputName($model, $dataProvider->getSort()->sortParam), '', ['id' => Html::getInputId($model, $dataProvider->getSort()->sortParam)]); ?>
...

Currently `Sort` works only with `$_GET` sort parameter.
Thus, for example combination of `GridView` and search form with method `post` does not work at all.

Proposed change is an attempt to make `Sort` accept `$_POST` also.

Example usage

Search model:

```
...
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'transaction_date' => SORT_DESC
                ],
                'dataMethod' => 'post',
                'formName' => $this->formName()
            ]
        ]);
...
```

GridView:
```
...
            'columns' => [
               ...
                [
                    'attribute' => 'transaction_date',
                    'sortLinkOptions' => [
                        'data-method' => 'post',
                        'href' => '#',
                        'onclick' => 'jQuery(\'#'.Html::getInputId($searchModel, $dataProvider->getSort()->sortParam).'\').val(jQuery(this).data(\'sort\')).closest(\'form\').submit();'
                    ],
                ],
...
```

Search form:

```
...
<?php echo Html::hiddenInput(Html::getInputName($model, $dataProvider->getSort()->sortParam), '', ['id' => Html::getInputId($model, $dataProvider->getSort()->sortParam)]); ?>
...
```
@samdark samdark added this to the 2.0.1 milestone Oct 12, 2014
@SDKiller
Copy link
Contributor Author

I'm afraid something similar will be nesessary to make pagination and forms with method post work together.

@qiangxue
Copy link
Member

I don't think we will support this by default. The params property is here for you to assign the POST or other type of inputs.

@qiangxue qiangxue closed this Oct 12, 2014
@SDKiller
Copy link
Contributor Author

The params property is here for you to assign the POST or other type of inputs.

????

$request->getQueryParams()

@qiangxue
Copy link
Member

[
    'params' => $request->post(),
]

@SDKiller
Copy link
Contributor Author

Ok then, even it is not so obvious at first glance.

But at list

$url = isset($options['href']) ? $options['href'] : $this->createUrl($attribute);

could be implemented in \common\overyii\data\Sort::link().

@qiangxue
Copy link
Member

Not quite sure. Why would you provide href in the first place? What is the difference between # and a URL in your example?

@SDKiller
Copy link
Contributor Author

Take a look at yii.js handleAction() for example.

var action = $e.attr('href');

But as I dig into the GridView - assigning href # and preserving another attributes for sort link is not a trivial case - actually its easier to assign everything manually in label because of construct of \common\overyii\data\Sort::link

@cebe cebe removed this from the 2.0.1 milestone Oct 12, 2014
@SDKiller SDKiller deleted the patch-3 branch November 24, 2014 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants