feat: add list with search

This commit is contained in:
b1ek 2024-08-20 14:27:17 +10:00
parent 97e287e731
commit c0edeca406
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 100 additions and 42 deletions

View File

@ -0,0 +1,27 @@
<?php
namespace app\controllers;
use app\models\Parameter;
use app\models\ParameterSearch;
use yii\data\ActiveDataProvider;
use yii\web\Request;
class ListController extends \yii\web\Controller
{
public function actionIndex(Request $request)
{
$searchModel = new ParameterSearch();
$dataProvider = $searchModel->search($request->queryParams);
return $this->render(
'index',
[
'provider' => $dataProvider,
'searchModel' => $searchModel,
]
);
}
}

View File

@ -12,48 +12,6 @@ use app\models\ContactForm;
class SiteController extends Controller class SiteController extends Controller
{ {
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/** /**
* Displays homepage. * Displays homepage.
* *

View File

@ -0,0 +1,47 @@
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class ParameterSearch extends Parameter
{
public $id;
public $title;
public function rules()
{
return [
[[ 'id' ], 'integer' ],
[[ 'title' ], 'string' ]
];
}
public function scenarios()
{
return Model::scenarios();
}
public function search($params): ActiveDataProvider
{
$query = Parameter::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
if ($this->title) {
$query->andFilterWhere(['like', 'title', $this->title]);
}
if ($this->id) {
$query->andFilterWhere(['like', 'id', $this->id]);
}
return $dataProvider;
}
}

26
views/list/index.php Normal file
View File

@ -0,0 +1,26 @@
<?php
use yii\helpers\Html;
use yii\bootstrap5\ActiveForm;
use yii\grid\GridView;
/** @var yii\web\View $this */
$this->title = 'params list';
?>
<h1>Parameters</h1>
<?php $searchForm = ActiveForm::begin([
'action' => '/list/index',
'method' => 'get',
]); ?>
<?= Html::submitButton('Run search', [ 'class' => 'btn btn-l btn-success w-100' ]) ?>
<p class="small">Set your search query in the grid view</p>
<?php ActiveForm::end(); ?>
<?= GridView::widget([
'dataProvider' => $provider,
'filterModel' => $searchModel,
]); ?>