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
b5e5b029
Commit
b5e5b029
authored
Dec 28, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Guide to sorting
parent
22376910
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
4 deletions
+51
-4
README.md
docs/guide/README.md
+1
-1
output-sorting.md
docs/guide/output-sorting.md
+50
-3
No files found.
docs/guide/README.md
View file @
b5e5b029
...
...
@@ -98,7 +98,7 @@ Displaying Data
*
[
Data Formatting
](
output-formatter.md
)
*
[
Pagination
](
output-pagination.md
)
*
**TBD**
[
Sorting
](
output-sorting.md
)
*
[
Sorting
](
output-sorting.md
)
*
[
Data Providers
](
output-data-providers.md
)
*
[
Data Widgets
](
output-data-widgets.md
)
*
[
Working with Client Scripts
](
output-client-scripts.md
)
...
...
docs/guide/output-sorting.md
View file @
b5e5b029
Sorting
=======
> Note: This section is under development.
>
> It has no content yet.
Sometimes data to be displayed should be sorted according to one or several attributes. If you are using
[
data provider
](
output-data-providers.md
)
with one of the
[
data widgets
](
output-data-widgets.md
)
it is
handled for you automatically. If not, you should create
[
[\yii\data\Sort
]
] instance in controller, configure it
apply it to the query and then pass it to the view where it can be used to create links to sort by attributes.
A typical usage example is as follows,
```
php
function
actionIndex
()
{
$sort
=
new
Sort
([
'attributes'
=>
[
'age'
,
'name'
=>
[
'asc'
=>
[
'first_name'
=>
SORT_ASC
,
'last_name'
=>
SORT_ASC
],
'desc'
=>
[
'first_name'
=>
SORT_DESC
,
'last_name'
=>
SORT_DESC
],
'default'
=>
SORT_DESC
,
'label'
=>
'Name'
,
],
],
]);
$models
=
Article
::
find
()
->
where
([
'status'
=>
1
])
->
orderBy
(
$sort
->
orders
)
->
all
();
return
$this
->
render
(
'index'
,
[
'models'
=>
$models
,
'sort'
=>
$sort
,
]);
}
```
In the view:
```
php
// display links leading to sort actions
echo
$sort
->
link
(
'name'
)
.
' | '
.
$sort
->
link
(
'age'
);
foreach
(
$models
as
$model
)
{
// display $model here
}
```
In the above, we declare two attributes that support sorting:
`name`
and
`age`
.
We pass the sort information to the Article query so that the query results are
sorted by the orders specified by the Sort object. In the view, we show two hyperlinks
that can lead to pages with the data sorted by the corresponding attributes.
\ No newline at end of file
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