【问题标题】:Yii2 sorting indirectly related column which is not part of gridview modelYii2对不属于gridview模型的间接相关列进行排序
【发布时间】:2015-01-26 12:51:07
【问题描述】:

我有一个显示公司数据的网格视图。每个公司都有一个 tehsil_id 作为外键。下面是从父到子的表结构:

Country->province->city->district->tehsil

每家公司都位于特定的 tehsil 中。可以使用 tehsil ID 向后追踪一个地区或城市或省。

我面临的问题是我想列出不仅可以按 Tehsil ID(是 Company 表中的一列)而且可以按城市 ID/省 ID/国家/地区 ID 排序的公司(这些 ID 都不是一部分公司表)。

我可以在 gridview 中列出这些相关的地区/城市/省,但无法使用这些值进行排序。您的帮助将不胜感激。

<?php
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [

        'id',
        'name',
        // generated by schmunk42\giiant\crud\providers\RelationProvider::columnFormat
        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "tehsil_id",
            'filter' => ArrayHelper::map(Tehsil::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/tehsil/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],

        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "district_id",
            'filter' => ArrayHelper::map(District::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()->getDistrict()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/district/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],


        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "city_id",
            'filter' => ArrayHelper::map(City::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()->getDistrict()->one()->getCity()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/city/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],
        // generated by schmunk42\giiant\crud\providers\RelationProvider::columnFormat
        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "company_id",
            "value" => function ($model) {
                if ($rel = $model->getCompany()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/company/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],
        'phone',
        'fax',
        'mobile',
        'email:email',
        /*
        'created_on',
        'updated_on',
        'created_by',
        'updated_by',
        */
        [
            'class' => 'yii\grid\ActionColumn',
            'urlCreator' => function ($action, $model, $key, $index) {
                // using the column name as key, not mapping to 'id' like the standard generator
                $params = is_array($key) ? $key : [$model->primaryKey()[0] => (string)$key];
                $params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;
                return \yii\helpers\Url::toRoute($params);
            },
            'contentOptions' => ['nowrap' => 'nowrap']
        ],
    ],
]);

【问题讨论】:

    标签: gridview yii


    【解决方案1】:

    您只能对原始查询中存在的列进行排序。很可能你有一个 CompanySearch 类,它有一个 search() 方法。在其中,将更多表连接到您的原始查询:

    $query = Company::find()->joinWith([
        'tehsil',
        'tehsil.district',
        'tehsil.district.city',
        'tehsil.district.city.province',
        'tehsil.district.city.province.country'
    ]);
    

    然后将新列添加到网格中:

    echo GridView::widget([
        ...
        'columns' => [
            'id',
            'name',
            'tehsil',
            'tehsil.district',
            'tehsil.district.city',
            'tehsil.district.city.province',
            'tehsil.district.city.province.country',
            ...
    

    它们应该是可排序的。

    另外,请查看本教程: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
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多