M
M
Mr_Romanov2017-10-05 14:57:40
Yii
Mr_Romanov, 2017-10-05 14:57:40

Yii2 GridView + sqldataprovider how to make a table?

Lord! Good afternoon.
I comprehend yii2, and I ran into a problem that I can’t find any normal documentation that shows an example of how to turn data from a database into a simple table with filtering using gridview.
In my understanding, it is necessary to make 2 models, in one the data from the table in the second filter rules.
In the controller to prescribe an action that will set the input? parameters in these models?
And also create a view with the gridview itself.
But no matter how much I tried, it doesn't work, and that's it!
Can someone explain using an elementary example with a two-column table that you just need to display on a page with the ability to filter?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mr_Romanov, 2017-10-05
@Mr_Romanov

Thanks to all! It worked :)
use yii\data\SqlDataProvider;
$count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM clients')->queryScalar();
$provider = new SqlDataProvider([
'sql' => 'SELECT * FROM clients',
'totalCount' => $count,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes ' => [
'id',
'name',
'phone',
],
],
]);
$models = $provider->getModels();
echo GridView::widget([
'
]);

D
Dmitry, 2017-10-05
@slo_nik

Good afternoon.
Use ActiveDataProvider() and you can easily create a filtered table.
GridView just accepts the result of the ActiveDataProvider operation in the "dataProvider" parameter.

M
Maxim Fedorov, 2017-10-05
@qonand

Что-то не то Вы в документации смотрите. Вот простой пример с кучей кода о выводе и данных в таблице и фильтрации по ним

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question