mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	@ -1,48 +1,50 @@
 | 
				
			|||||||
分页
 | 
					分页
 | 
				
			||||||
==========
 | 
					==========
 | 
				
			||||||
 | 
					
 | 
				
			||||||
当一次要在一个页面上显示很多数据时,通过需要将其分为几部分,每个部分都包含一些数据
 | 
					当一次要在一个页面上显示很多数据时,
 | 
				
			||||||
列表并且一次只显示一部分。这些部分在网页上被称为 *分页*。
 | 
					通常需要将其分成几部分,
 | 
				
			||||||
 | 
					每个部分都包含一些数据列表并且一次只显示一部分。
 | 
				
			||||||
 | 
					这些部分在网页上被称为 *分页*。
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
Yii uses a [[yii\data\Pagination]] object to represent the information about a pagination scheme. In particular,
 | 
					Yii 使用 [[yii\data\Pagination]] 对象来代表分页方案的有关信息。
 | 
				
			||||||
 | 
					特别地,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* [[yii\data\Pagination::$totalCount|total count]] specifies the total number of data items. Note that this
 | 
					* [[yii\data\Pagination::$totalCount|total count]] 指定数据条目的总数。注意,这个数字通常远远大于需要在一个页面上展示的数据条目。
 | 
				
			||||||
  is usually much more than the number of data items needed to display on a single page.
 | 
					* [[yii\data\Pagination::$pageSize|page size]] 指定每页包含多少数据条目。默认值为20。
 | 
				
			||||||
* [[yii\data\Pagination::$pageSize|page size]] specifies how many data items each page contains. The default
 | 
					* [[yii\data\Pagination::$page|current page]] 给出当前的页码。默认值为0,表示第一页。
 | 
				
			||||||
  value is 20.
 | 
					 | 
				
			||||||
* [[yii\data\Pagination::$page|current page]] gives the current page number (zero-based). The default value
 | 
					 | 
				
			||||||
  value is 0, meaning the first page.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
With a fully specified [[yii\data\Pagination]] object, you can retrieve and display data partially. For example,
 | 
					通过一个已经完全明确的 [[yii\data\Pagination]] 对象,
 | 
				
			||||||
if you are fetching data from a database, you can specify the `OFFSET` and `LIMIT` clause of the DB query with
 | 
					你可以部分地检索并且展示数据。
 | 
				
			||||||
the corresponding values provided by the pagination. Below is an example, 
 | 
					比如,如果你正在从数据库取回数据,
 | 
				
			||||||
 | 
					你可以使用分页对象提供的对应值来指定 DB 查询语句中的 `OFFSET` 和  `LIMIT` 子句。
 | 
				
			||||||
 | 
					下面是个例子,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
use yii\data\Pagination;
 | 
					use yii\data\Pagination;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// build a DB query to get all articles with status = 1
 | 
					// 创建一个 DB 查询来获得所有 status 为 1 的文章
 | 
				
			||||||
$query = Article::find()->where(['status' => 1]);
 | 
					$query = Article::find()->where(['status' => 1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// get the total number of articles (but do not fetch the article data yet)
 | 
					// 得到文章的总数(但是还没有从数据库取数据)
 | 
				
			||||||
$count = $query->count();
 | 
					$count = $query->count();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// create a pagination object with the total count
 | 
					// 使用总数来创建一个分页对象
 | 
				
			||||||
$pagination = new Pagination(['totalCount' => $count]);
 | 
					$pagination = new Pagination(['totalCount' => $count]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// limit the query using the pagination and retrieve the articles
 | 
					// 使用分页对象来填充 limit 子句并取得文章数据
 | 
				
			||||||
$articles = $query->offset($pagination->offset)
 | 
					$articles = $query->offset($pagination->offset)
 | 
				
			||||||
    ->limit($pagination->limit)
 | 
					    ->limit($pagination->limit)
 | 
				
			||||||
    ->all();
 | 
					    ->all();
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Which page of articles will be returned in the above example? It depends on whether a query parameter named `page`
 | 
					上述例子中,文章的哪一页将被返回?它取决于是否给出一个名为 `page` 的参数。
 | 
				
			||||||
is given. By default, the pagination will attempt to set the [[yii\data\Pagination::$page|current page]] to be
 | 
					默认情况下,分页对象将尝试将 [[yii\data\Pagination::$page|current page]] 设置为 `page` 参数的值。
 | 
				
			||||||
the value of the `page` parameter. If the parameter is not provided, then it will default to 0.
 | 
					如果没有提供该参数,那么它将默认为0。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
To facilitate building the UI element that supports pagination, Yii provides the [[yii\widgets\LinkPager]] widget
 | 
					为了促成创建支持分页的 UI 元素,Yii 提供了 [[yii\widgets\LinkPager]] 挂件来展示一栏页码按钮,
 | 
				
			||||||
that displays a list of page buttons upon which users can click to indicate which page of data should be displayed.
 | 
					用户可以通过点击它来指示应该显示哪一页的数据。
 | 
				
			||||||
The widget takes a pagination object so that it knows what is the current page and how many page buttons should
 | 
					该挂件接收一个分页对象,因此它知道当前的页数和应该展示多少页码按钮。
 | 
				
			||||||
be displayed. For example,
 | 
					比如,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
use yii\widgets\LinkPager;
 | 
					use yii\widgets\LinkPager;
 | 
				
			||||||
@ -52,21 +54,22 @@ echo LinkPager::widget([
 | 
				
			|||||||
]);
 | 
					]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
If you want to build UI element manually, you may use [[yii\data\Pagination::createUrl()]] to create URLs that
 | 
					如果你想手动地创建 UI 元素,
 | 
				
			||||||
would lead to different pages. The method requires a page parameter and will create a properly formatted URL
 | 
					你可以使用 [[yii\data\Pagination::createUrl()]] 来创建指向不同页面的 URLs 。
 | 
				
			||||||
containing the page parameter. For example,
 | 
					该方法需要一个页码来作为参数,
 | 
				
			||||||
 | 
					并且将创建一个包含页码并且格式正确的 URL,
 | 
				
			||||||
 | 
					例如,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
// specifies the route that the URL to be created should use
 | 
					// 指定要被创建的 URL 应该使用的路由
 | 
				
			||||||
// If you do not specify this, the currently requested route will be used
 | 
					// 如果不指定,则使用当前被请求的路由
 | 
				
			||||||
$pagination->route = 'article/index';
 | 
					$pagination->route = 'article/index';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// displays: /index.php?r=article%2Findex&page=100
 | 
					// 显示: /index.php?r=article%2Findex&page=100
 | 
				
			||||||
echo $pagination->createUrl(100);
 | 
					echo $pagination->createUrl(100);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// displays: /index.php?r=article%2Findex&page=101
 | 
					// 显示: /index.php?r=article%2Findex&page=101
 | 
				
			||||||
echo $pagination->createUrl(101);
 | 
					echo $pagination->createUrl(101);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
> Tip: You can customize the name of the `page` query parameter by configuring the
 | 
					> Tip: 创建分页对象时,你可以通过配置 [[yii\data\Pagination::pageParam|pageParam]] 属性来自定义查询参数 `page` 的名字。
 | 
				
			||||||
  [[yii\data\Pagination::pageParam|pageParam]] property when creating the pagination object.
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,20 +1,18 @@
 | 
				
			|||||||
排序
 | 
					排序
 | 
				
			||||||
=======
 | 
					=======
 | 
				
			||||||
 | 
					
 | 
				
			||||||
When displaying multiple rows of data, it is often needed that the data be sorted according to some columns
 | 
					展示多条数据时,通常需要对数据按照用户指定的列进行排序。
 | 
				
			||||||
specified by end users. Yii uses a [[yii\data\Sort]] object to represent the information about a sorting schema.
 | 
					Yii 使用 [[yii\data\Sort]] 对象来代表排序方案的有关信息。
 | 
				
			||||||
In particular, 
 | 
					特别地,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* [[yii\data\Sort::$attributes|attributes]] specifies the *attributes* by which the data can be sorted.
 | 
					* [[yii\data\Sort::$attributes|attributes]] 指定 *属性*,数据按照其排序。一个属性可以就是简单的一个 [model attribute](structure-models.md#attributes),也可以是结合了多个 model 属性或者 DB 列的复合属性。下面将给出更多细节。
 | 
				
			||||||
  An attribute can be as simple as a [model attribute](structure-models.md#attributes). It can also be a composite
 | 
					* [[yii\data\Sort::$attributeOrders|attributeOrders]] 给出每个属性当前设置的排序方向。
 | 
				
			||||||
  one by combining multiple model attributes or DB columns. More details will be given in the following.
 | 
					* [[yii\data\Sort::$orders|orders]] 按照低级列的方式给出排序方向。
 | 
				
			||||||
* [[yii\data\Sort::$attributeOrders|attributeOrders]] gives the currently requested ordering directions for 
 | 
					 | 
				
			||||||
  each attribute.
 | 
					 | 
				
			||||||
* [[yii\data\Sort::$orders|orders]] gives the ordering directions in terms of the low-level columns.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
To use [[yii\data\Sort]], first declare which attributes can be sorted. Then retrieve the currently requested
 | 
					使用 [[yii\data\Sort]],首先要声明什么属性能进行排序。
 | 
				
			||||||
ordering information from [[yii\data\Sort::$attributeOrders|attributeOrders]] or [[yii\data\Sort::$orders|orders]]
 | 
					接着从 [[yii\data\Sort::$attributeOrders|attributeOrders]] 或者 [[yii\data\Sort::$orders|orders]] 取得当前设置的排序信息,
 | 
				
			||||||
and use them to customize the data query. For example,
 | 
					然后使用它们来自定义数据查询。
 | 
				
			||||||
 | 
					例如,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
use yii\data\Sort;
 | 
					use yii\data\Sort;
 | 
				
			||||||
@ -37,10 +35,10 @@ $articles = Article::find()
 | 
				
			|||||||
    ->all();
 | 
					    ->all();
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
In the above example, two attributes are declared for the [[yii\data\Sort|Sort]] object: `age` and `name`. 
 | 
					上述例子中,为 [[yii\data\Sort|Sort]] 对象声明了两个属性: `age` 和 `name`。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The `age` attribute is a *simple* attribute corresponding to the `age` attribute of the `Article` Active Record class.
 | 
					`age` 属性是 `Article` 与 Active Record 类中 `age` 属性对应的一个简单属性。
 | 
				
			||||||
It is equivalent to the following declaration:
 | 
					上述声明与下述等同:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
'age' => [
 | 
					'age' => [
 | 
				
			||||||
@ -51,39 +49,34 @@ It is equivalent to the following declaration:
 | 
				
			|||||||
]
 | 
					]
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The `name` attribute is a *composite* attribute defined by `first_name` and `last_name` of `Article`. It is declared
 | 
					`name` 属性是由 `Article` 的 `firsr_name` 和 `last_name` 定义的一个复合属性。
 | 
				
			||||||
using the following array structure:
 | 
					使用下面的数组结构来对它进行声明:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- The `asc` and `desc` elements specify how to sort by the attribute in ascending and descending directions, respectively.
 | 
					- `asc` 和 `desc` 元素指定了如何按照该属性进行升序和降序的排序。它们的值代表数据真正地应该按照什么列和方向进行排序。
 | 
				
			||||||
  Their values represent the actual columns and the directions by which the data should be sorted by. You can specify
 | 
					  你可以指定一列或多列来指出到底是简单排序还是多重排序。
 | 
				
			||||||
  one or multiple columns to indicate simple ordering or composite ordering.
 | 
					- `default` 元素指定了当一次请求时,属性应该按照什么方向来进行排序。它默认为升序方向,意味着如果之前没有进行排序,并且
 | 
				
			||||||
- The `default` element specifies the direction by which the attribute should be sorted when initially requested. 
 | 
					  你请求按照该属性进行排序,那么数据将按照该属性来进行升序排序。
 | 
				
			||||||
  It defaults to ascending order, meaning if it is not sorted before and you request to sort by this attribute, 
 | 
					- `label` 元素指定了调用 [[yii\data\Sort::link()]] 来创建一个排序链接时应该使用什么标签,如果不设置,将调用 
 | 
				
			||||||
  the data will be sorted by this attribute in ascending order.
 | 
					  [[yii\helpers\Inflector::camel2words()]] 来通过属性名生成一个标签。注意,它并不是 HTML编码的。
 | 
				
			||||||
- The `label` element specifies what label should be used when calling [[yii\data\Sort::link()]] to create a sort link.
 | 
					  
 | 
				
			||||||
  If not set, [[yii\helpers\Inflector::camel2words()]] will be called to generate a label from the attribute name.
 | 
					> Info: 你可以将 [[yii\data\Sort::$orders|orders]] 的值直接提供给数据库查询来构建其 `ORDER BY` 子句。不要使用 [[yii\data\Sort::$attributeOrders|attributeOrders]] ,因为一些属性可能是复合的,是不能被数据库查询识别的。
 | 
				
			||||||
  Note that it will not be HTML-encoded.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
> Info: You can directly feed the value of [[yii\data\Sort::$orders|orders]] to the database query to build
 | 
					你可以调用 [[yii\data\Sort::link()]] 来生成一个超链接,用户可以通过点击它来请求按照指定的属性对数据进行排序。
 | 
				
			||||||
  its `ORDER BY` clause. Do not use [[yii\data\Sort::$attributeOrders|attributeOrders]] because some of the
 | 
					你也可以调用 [[yii\data\Sort::createUrl()]] 来生成一个可排序的 URL。
 | 
				
			||||||
  attributes may be composite and cannot be recognized by the database query.
 | 
					例如,
 | 
				
			||||||
 | 
					 | 
				
			||||||
You can call [[yii\data\Sort::link()]] to generate a hyperlink upon which end users can click to request sorting
 | 
					 | 
				
			||||||
the data by the specified attribute. You may also call [[yii\data\Sort::createUrl()]] to create a sortable URL.
 | 
					 | 
				
			||||||
For example,
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
// specifies the route that the URL to be created should use
 | 
					// 指定被创建的 URL 应该使用的路由
 | 
				
			||||||
// If you do not specify this, the currently requested route will be used
 | 
					// 如果你没有指定,将使用当前被请求的路由
 | 
				
			||||||
$sort->route = 'article/index';
 | 
					$sort->route = 'article/index';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// display links leading to sort by name and age, respectively
 | 
					// 显示链接,链接分别指向以 name 和 age 进行排序
 | 
				
			||||||
echo $sort->link('name') . ' | ' . $sort->link('age');
 | 
					echo $sort->link('name') . ' | ' . $sort->link('age');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// displays: /index.php?r=article%2Findex&sort=age
 | 
					// 显示: /index.php?r=article%2Findex&sort=age
 | 
				
			||||||
echo $sort->createUrl('age');
 | 
					echo $sort->createUrl('age');
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[yii\data\Sort]] checks the `sort` query parameter to determine which attributes are being requested for sorting.
 | 
					[[yii\data\Sort]] 查看 `sort` 查询参数来决定哪一个属性正在被请求来进行排序。
 | 
				
			||||||
You may specify a default ordering via [[yii\data\Sort::defaultOrder]] when the query parameter is not present.
 | 
					当该参数不存在时,你可以通过 [[yii\data\Sort::defaultOrder]] 来指定默认的排序。
 | 
				
			||||||
You may also customize the name of the query parameter by configuring the [[yii\data\Sort::sortParam|sortParam]] property.
 | 
					你也可以通过配置 [[yii\data\Sort::sortParam|sortParam]] 属性来自定义该查询参数的名字。
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user