Yii2 - 在Yii2中自定义复选框列表

时间:2017-05-30 19:05:51

标签: php yii2

我有一个基于数组的查询:

$modelPipe = Pipe::find()
        ->select([
            "size",
            "container",
            "seal",
            "concat(container_seal, '<br>' , size) as container_seal",
            "count(size) as bundles",
            "sum(piece) as pieces",
            "bundle_group"
        ])

        ->where(['outgoing_pipe_id' => NULL])
        ->groupBy(['bundle_group'])
        ->orderBy(['bundle_group' => SORT_ASC])
        ->all();

像这样的查询结果

+-----------------------+--------------------------------------------------+---------+--------+--------------+
| size                  | container_seal                                   | bundles | pieces | bundle_group |
+-----------------------+--------------------------------------------------+---------+--------+--------------+
| 35 x 35 x 0.85 x 6000 | EITU1268671/ EMCCSQ5920<br>35 x 35 x 0.85 x 6000 |      24 |   2400 |            1 |
| 15 x 35 x 0.75 x 6000 | EITU1268671/ EMCCSQ5920<br>15 x 35 x 0.75 x 6000 |      20 |   2000 |            2 |
| 35 x 35 x 0.85 x 6000 | EITU1374987/ EMCCSQ6086<br>35 x 35 x 0.85 x 6000 |      24 |   2400 |            3 |
| 15 x 35 x 0.75 x 6000 | EITU1374987/ EMCCSQ6086<br>15 x 35 x 0.75 x 6000 |      20 |   2000 |            4 |
| 35 x 35 x 0.75 x 6000 | EITU1842100/ EMCCSQ6096<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            5 |
| 15 x 35 x 0.75 x 6000 | EITU1842100/ EMCCSQ6096<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |            6 |
| 35 x 35 x 0.75 x 6000 | DRYU9850060/ EMCCSQ6056<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            7 |
| 15 x 35 x 0.75 x 6000 | DRYU9850060/ EMCCSQ6056<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |            8 |
| 35 x 35 x 0.75 x 6000 | DFSU6646389/ EMCCSQ6626<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            9 |
| 15 x 35 x 0.75 x 6000 | DFSU6646389/ EMCCSQ6626<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |           10 |
| 35 x 35 x 0.75 x 6000 | TCLU4970557/ EMCCSQ6006<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |           11 |
| 15 x 35 x 0.75 x 6000 | TCLU4970557/ EMCCSQ6006<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |           12 |
| 35 x 35 x 0.75 x 6000 | FCIU9214153/ EMCCSQ6556<br>35 x 35 x 0.75 x 6000 |      24 |   2400 |           13 |
| 15 x 35 x 0.85 x 6000 | FCIU9214153/ EMCCSQ6556<br>15 x 35 x 0.85 x 6000 |      22 |   2200 |           14 |
| 35 x 35 x 0.75 x 6000 | DFSU6912998/ EMCCSQ6536<br>35 x 35 x 0.75 x 6000 |      24 |   2400 |           15 |
| 15 x 35 x 0.85 x 6000 | DFSU6912998/ EMCCSQ6536<br>15 x 35 x 0.85 x 6000 |      22 |   2200 |           16 |
| 35 x 35 x 0.85 x 6000 | TEMU6099067/ EMCCSQ6566<br>35 x 35 x 0.85 x 6000 |       4 |    400 |           17 |
| 35 x 35 x 0.75 x 6000 | TEMU6099067/ EMCCSQ6566<br>35 x 35 x 0.75 x 6000 |      20 |   2000 |           18 |
| 15 x 35 x 0.85 x 6000 | TEMU6099067/ EMCCSQ6566<br>15 x 35 x 0.85 x 6000 |      20 |   2000 |           19 |
| 35 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>35 x 35 x 0.85 x 6000 |       6 |    600 |           20 |
| 15 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.85 x 6000 |      17 |   1700 |           21 |
| 15 x 35 x 0.75 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.75 x 6000 |      30 |   3000 |           22 |
| 15 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.85 x 6000 |       1 |      8 |           23 |
| 15 x 35 x 0.75 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.75 x 6000 |       1 |     26 |           24 |
+-----------------------+--------------------------------------------------+---------+--------+--------------+

问题是,我需要使用一个checkboxlist来插入上面的数据。 所以,我这样写:

<?php
   $data = ArrayHelper::map($modelPipe, "bundle_group", "container_seal");?>

   <?= $form->field($model, 'nomors_urut_per_bundle')
       ->label(false)
       ->checkboxList($data); 
   ?>

我是这样的: image

我如何获取视图给我的用户只是一个普通的表,第一列是复选框?

请指教。 谢谢。

+-----------------------+--------------------------------------------------+---------+--------+--------------+
| check/uncheck all | size                  | container_seal                                   | bundles | pieces | bundle_group |
+-----------------------+--------------------------------------------------+---------+--------+--------------+
| check/uncheck     | 35 x 35 x 0.85 x 6000 | EITU1268671/ EMCCSQ5920<br>35 x 35 x 0.85 x 6000 |      24 |   2400 |            1 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | EITU1268671/ EMCCSQ5920<br>15 x 35 x 0.75 x 6000 |      20 |   2000 |            2 |
| check/uncheck     | 35 x 35 x 0.85 x 6000 | EITU1374987/ EMCCSQ6086<br>35 x 35 x 0.85 x 6000 |      24 |   2400 |            3 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | EITU1374987/ EMCCSQ6086<br>15 x 35 x 0.75 x 6000 |      20 |   2000 |            4 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | EITU1842100/ EMCCSQ6096<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            5 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | EITU1842100/ EMCCSQ6096<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |            6 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | DRYU9850060/ EMCCSQ6056<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            7 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | DRYU9850060/ EMCCSQ6056<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |            8 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | DFSU6646389/ EMCCSQ6626<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |            9 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | DFSU6646389/ EMCCSQ6626<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |           10 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | TCLU4970557/ EMCCSQ6006<br>35 x 35 x 0.75 x 6000 |      18 |   1800 |           11 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | TCLU4970557/ EMCCSQ6006<br>15 x 35 x 0.75 x 6000 |      34 |   3400 |           12 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | FCIU9214153/ EMCCSQ6556<br>35 x 35 x 0.75 x 6000 |      24 |   2400 |           13 |
| check/uncheck     | 15 x 35 x 0.85 x 6000 | FCIU9214153/ EMCCSQ6556<br>15 x 35 x 0.85 x 6000 |      22 |   2200 |           14 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | DFSU6912998/ EMCCSQ6536<br>35 x 35 x 0.75 x 6000 |      24 |   2400 |           15 |
| check/uncheck     | 15 x 35 x 0.85 x 6000 | DFSU6912998/ EMCCSQ6536<br>15 x 35 x 0.85 x 6000 |      22 |   2200 |           16 |
| check/uncheck     | 35 x 35 x 0.85 x 6000 | TEMU6099067/ EMCCSQ6566<br>35 x 35 x 0.85 x 6000 |       4 |    400 |           17 |
| check/uncheck     | 35 x 35 x 0.75 x 6000 | TEMU6099067/ EMCCSQ6566<br>35 x 35 x 0.75 x 6000 |      20 |   2000 |           18 |
| check/uncheck     | 15 x 35 x 0.85 x 6000 | TEMU6099067/ EMCCSQ6566<br>15 x 35 x 0.85 x 6000 |      20 |   2000 |           19 |
| check/uncheck     | 35 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>35 x 35 x 0.85 x 6000 |       6 |    600 |           20 |
| check/uncheck     | 15 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.85 x 6000 |      17 |   1700 |           21 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.75 x 6000 |      30 |   3000 |           22 |
| check/uncheck     | 15 x 35 x 0.85 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.85 x 6000 |       1 |      8 |           23 |
| check/uncheck     | 15 x 35 x 0.75 x 6000 | EITU1153733/ EMCCSQ6956<br>15 x 35 x 0.75 x 6000 |       1 |     26 |           24 |
+-----------------------+--------------------------------------------------+---------+--------+--------------+

1 个答案:

答案 0 :(得分:0)

您只需使用yii\grid\CheckboxColumn http://www.yiiframework.com/doc-2.0/yii-grid-checkboxcolumn.html

即可在GridView中使用普通['class' => 'yii\grid\CheckboxColumn']

最终你可以使用回调函数来管理正确的选项,例如:

  echo GridView::widget([
     'id' => 'griditems',
     'dataProvider' => $dataProvider,
     'columns' => [
         .......
        [
            'class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model) {
                  return ['value' => $model->your_value];
              },
        ],
     ],
  ]);