简体   繁体   English

Yii2如何使用select2小部件创建表格模板

[英]Yii2 how to create table template using select2 widget

I have a dropdown list using select2 widget to get the customer contact 我有一个使用select2小部件获得客户联系的下拉列表

$getcontact = ArrayHelper::map(OpContact::find()->all(),'id','code');

 <div class="col-md-10">'.
                     $form->field($model, 'contact_id')->widget(Select2::classname(), [
                          'data' => $getcontact,
                          'language' => 'en',
                          'options' => ['placeholder' => 'Select'],
                          'pluginOptions' => [
                              'allowClear' => true
                          ],                             
                      ])->label(false).' 

For now, its just a normal dropdownlist which show only the contact id.How I can like insert 2nd column for it and put all the data inside a table. 现在,它只是一个正常的下拉列表,只显示联系人ID。我如何为它插入第二列并将所有数据放入表中。 For example, I want the dropdown to look like this where I can see the id and the contact name together. 例如,我希望下拉菜单看起来像这样,在其中我可以同时看到ID和联系人姓名。

------------------------------
     ID         Contact ID
------------------------------
     21          contact1
     22          contact2
     23          contact3
     24          contact1

Anyone have any idea? 有人知道吗 Thanks 谢谢

if you want store only the id and see both id and contactID and contact ID is th concat of id and code you couuld use 如果您只想存储ID,并且同时看到ID和contactID,并且联系人ID是您应该使用的ID和代码的结合,

$getcontact = ArrayHelper::map(OpContact::find()
               ->select('id, concat(id, code) as contactID')
               ->all(),'id','contactID');

if you want store both you can 如果您要存储两者都可以

$getcontact = ArrayHelper::map(OpContact::find()
               ->select('concat(id, code) as id, concat(id, code) as contactID')
               ->all(),'id','contactID');

您不能全部显示它们,但是您可以在查询(如CONCAT)中以字符串模式合并该字段,或在您的foreach合并字符串中加上“。”

如下更改ArrayHelper

$getcontact = ArrayHelper::map(OpContact::find()->all(),'id',function($model){ return $model->id.'-'.$model->code; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM