【问题标题】:Yii2: How to sort a GridView's column that doesn't belong to its modelYii2:如何对不属于其模型的 GridView 列进行排序
【发布时间】:2017-12-14 13:47:29
【问题描述】:

我使用 Gii 从名为 vacations 的表中创建了一个模型及其 CRUD。它的 index.php 有一个 GridView,它有两列,分别称为 Column 1Column 3。然后我添加了一个名为 Person 的列,它来自一个名为 persons 的表。

问题是列Person不可排序。

所以我有这个表(column1_idperson_id 是他们表的主键):

vacations: column1_id, column3.
persons: person_id, person_name, column1_id.

也许我必须在 VacationsSearch 文件中添加一些内容。

【问题讨论】:

标签: php sorting gridview yii2 gii


【解决方案1】:

基本上你应该扩展你的搜索功能来为相关列添加查询和排序

   $dataProvider->setSort([
       .....
          'your_related_column' => [
              'asc' => ['related_table.your_related_column' => SORT_ASC],
              'desc' => ['related_table.your_related_column' => SORT_DESC],
              'label' => 'Your Label'
          ]
      ]
  ]);

   .....

  // filter by country name
  $query->joinWith(['yor_relation' => function ($q) {
      $q->where('related_table.your_related_column LIKE "%' . $this->yourRelatedAttribute . '%"');
  }]);

如需更广泛的解释,您可以查看此示例的场景 2 http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多