E
E
Eugene2017-06-24 14:23:49
Yii
Eugene, 2017-06-24 14:23:49

What is the best way to do this kind of sorting in yii2?

What is the best way to do this sorting?

<form class="form-inline order-by">
<div class="form-group">
<label>Sort by:</label>
</div>
<div class="form-group">
<select class="form-control">
<option selected="selected">Default</option>
<option>Popularity</option>
<option>Average rating</option>
<option>Newness</option>
<option>Price: low to high</option>
<option>Price: high to low</option>
</select>
</div>
</form>

I came up with an idea:
<option value="http://localhost/mysite/frontend/web/index.php/catalog/index?sort=-view_count">
Popularity
</option>

but it's a very dull idea...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2017-06-26
@e236286

General principle:

<form action="">
    <select name="order_by">
        <option value=""></option>
        <option value="view_count">По популярности</option>
    </select>
</form>

class NewsSearch extends Model {
        public $order_by;
        public function rules(){
            return [
                ['order_by', 'in', 'range' => ['view_count', 'created_at']],
            ];
        }
        public function search(){
            // ...
            $query->orderBy([$this->order_by => SORT_ASC]);
            // ...
        }
    }

Further refine the presence of a minus sign "-" for reverse sorting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question