Sort filtered data


In gridview (Yii2) if I sort by column it unfilters underlying data.

Does anyone know how to keep the filter in a gridview while sorting?

Thank you.

Usually sorting won’t affect filtering. In a typical page with a gridview, parameters for sorting AND filtering are sent at the same time so that the user will enjoy the both.

If your page is suffering a problem between sorting and filtering, you should reconsider the way that you are using for sorting and/or filtering. Please check the logic of an “index” page that Gii’s CRUD generator has created.

Thank you Softark for your answer.

You are right.

The problem is that i’m using POST instead of GET, because GET makes long, and in my opinion ugly, URL’s.

I’ve solved the problem this way if anyone is interested.

    public function actionIndex()


        $searchModel = new sysactivitylogSearch();

        if (isset($_GET['sort'])): 

            $dataProvider = $searchModel->search(Yii::$app->session->get('sysActivitylog_filters'));


            $dataProvider = $searchModel->search(Yii::$app->request->post());



        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



Yeah, I agree that there are many people who is interested in your solution. Thanks for sharing.

But, well, I don’t mind the long url with filtering, paginating and sorting parameters that GET method creates. At the cost of the “ugliness” of the url, it gives much better experience to the end users than POST method: it enables book-marking of a certain search result, and it also enables back-and-forth navigation using keyboard. IMO, an operation that does not do writing but only do reading should use GET method instead of POST.

Thanks softark. You’re probably right.

For now I will try it with POST.

It seems to me more professional if the end user is not exposed to technicalities. But I also agree with you.

By the way, to all that may use my code above: Please replace

if (isset($_GET['sort'])):


if (isset($_GET['sort']) or isset($_GET['page'])):

Filters are also lost if you use grid pagination and change the page. in order to prevent that you must also check if the user is just changing the page.