【问题标题】:Yii2: how to modify data column of a GridView without using a modelYii2:如何在不使用模型的情况下修改 GridView 的数据列
【发布时间】:2020-06-10 13:29:31
【问题描述】:

我在控制器中创建了一个带有 productprice 列的 SqlDataProvider。

然后我在视图中使用 GridView 来显示两列,但我需要将 $price 连接起来,以便显示,例如 $ 75.

我知道如何使用模型,但在这种情况下我没有模型。

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'product',
        [
            'attribute'  => 'price',
            'value' => function($model) {
                return '$ ' . $model->price; // I don't have a model so it doesn't work here.
            }
        ],
    ],
]) ?>

【问题讨论】:

  • 如果不是 mdel,可能是数组(我不知道你的代码)所以 $model['price'].
  • 你是对的@Sfili_81。
  • 我希望它有用 ;)

标签: gridview model yii2 dataprovider


【解决方案1】:
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'product',
        [
            'attribute' => 'price',
            'value' => function ($model) {
                return '$ ' . $model['price'];
            }
        ],
    ],
]) ?>

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 2016-09-17
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多