Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
5985896c
Commit
5985896c
authored
Apr 11, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added docs about gridview filtering
parent
7946f453
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
0 deletions
+65
-0
data-grid.md
docs/guide/data-grid.md
+65
-0
No files found.
docs/guide/data-grid.md
View file @
5985896c
...
...
@@ -192,3 +192,68 @@ Filtering data
--------------
-
https://github.com/yiisoft/yii2/issues/1581
TBD
For filtering data the GridView needs a
[
model
](
model.md
)
that takes the input from the filtering
form and adjusts the query of the dataprovider to respect the search criteria.
A common practice when using
[
active records
](
active-record.md
)
is to create a search Model class
that extends from the active record class. This class then defines the validation rules for the search
and provides a
`search()`
method that will return the data provider.
To add search capability for the
`Post`
model we can create
`PostSearch`
like in the following example:
```
php
<?php
namespace
app\models
;
use
Yii
;
use
yii\base\Model
;
use
yii\data\ActiveDataProvider
;
class
PostSearch
extends
Post
{
public
function
rules
()
{
// only fields in rules() are searchable
return
[
[[
'id'
],
'integer'
],
[[
'title'
,
'creation_date'
],
'safe'
],
];
}
public
function
scenarios
()
{
// bypass scenarios() implementation in the parent class
return
Model
::
scenarios
();
}
public
function
search
(
$params
)
{
$query
=
Post
::
find
();
$dataProvider
=
new
ActiveDataProvider
([
'query'
=>
$query
,
]);
// load the seach form data and validate
if
(
!
(
$this
->
load
(
$params
)
&&
$this
->
validate
()))
{
return
$dataProvider
;
}
// adjust the query by adding the filters
$query
->
andFilterWhere
([
'id'
=>
$this
->
id
]);
$query
->
andFilterWhere
([
'like'
,
'title'
,
$this
->
name
])
->
andFilterWhere
([
'like'
,
'creation_date'
,
$this
->
creation_date
]);
return
$dataProvider
;
}
}
```
Filtering by related columns
----------------------------
TBD
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment