如何在yii2中的现有gridview列中添加复选框列?

时间:2018-12-02 14:48:13

标签: gridview yii2

我想向gridview添加一列,但我不想列出所有列(因为默认情况下有一些列)。 我知道我可以按照以下步骤添加一列:

$widget = Yii::createObject([
    'class' => 'yii\grid\GridView',
    'dataProvider' => $dataProvider,
    'columns' => [
        'col1',
        'col2',
        'class' => 'yii\grid\CheckboxColumn',
    ]
]);

但是我不想列出所有默认列,而只添加一个复选框列。就像这样:

$widget = Yii::createObject([
    'class' => 'yii\grid\GridView',
    'dataProvider' => $dataProvider,
    'columns' => [
        'class' => 'yii\grid\CheckboxColumn',
    ]
]);

然后它将显示在默认列的末尾。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

yii框架不支持此需求。我们可以通过其他方式做到这一点。

第一个:获取所有列名并在显示列表之前插入复选框列

const express = require('express');
const webpush = require('web-push');
const cors = require('cors');
const bodyParser = require('body-parser');

const PUBLIC_VAPID = 'BJPrg7jbhWkWZn5mhg0Wti8031cHjsLGyN1G4pmfeippmEsXHo53wnRiqqjApVkA1KQyIz0IYK4ln0ie7RLrsiI';
const PRIVATE_VAPID = 'D1njq6Y7ny2QexJ-JZXbUpufCkfIywLSMvO6s-iSNoQ';

const fakeDatabase = [];

const app = express();

app.use(cors());
app.use(bodyParser.json());

webpush.setVapidDetails('mailto:mailto@gmail.com', PUBLIC_VAPID, PRIVATE_VAPID);

app.post('/subscription', (req, res) => {
  const subscription = req.body;
  fakeDatabase.push(subscription);

  const notificationPayload = {
    notification: {
      title: 'New Notification',
      body: 'This is the body of the notification',
      icon: 'assets/icons/icon-512x512.png'
    }
  };

  const promises = [];
  fakeDatabase.forEach(subscription => {
    promises.push(webpush.sendNotification(subscription, JSON.stringify(notificationPayload)));
  });
  fakeDatabase.length =0
  Promise.all(promises).then(() => res.sendStatus(200));
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

]);

第二:扩展yii框架的<mat-option *ngFor="let champ of championList" [value]="champ">方法。例如:

$query = DataModel::find()->select('...')->asArray();

$columns = array_keys($query->one()); // if you know that all column names can also be assigned directly without dynamic acquisition
array_unshift(['class' => \yii\grid\CheckboxColumn::class], $columns);

$widget = Yii::createObject([
    'class' => 'yii\grid\GridView',
    'dataProvider' => new \yii\data\ActiveDataProvider([
        'query' => $query,
    ]),
    'columns' => $columns,
]);

回答Google Translate的翻译,希望对您有所帮助。