Advertisement
Guest User

Select2 Widget

a guest
Feb 24th, 2016
1,511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2.   $row = array();  
  3.   $query = ContactGroups::find();
  4.   $conditions = ['group_status'=>'ACTIVE'];
  5.     // default data when Insert Process / model are newRecord
  6.   $data = ArrayHelper::map($query->where($conditions)->asArray()->all(),'group_id', 'group_name');
  7.     // data return :
  8.     // [
  9.     //      "group_id"=>"group_name",
  10.     //      "group_id"=>"group_name",
  11.     //      "group_id"=>"group_name",
  12.     //  ];
  13.   if (!$model->isNewRecord)
  14.   {
  15.     $conditions = array_merge($conditions,['contact_contact_groups.contact_id'=>$model->contact_id]);
  16.     $dataModel = ArrayHelper::map($query->joinWith('contactContactGroups')->where($conditions)->asArray()->all(),'group_id', 'group_name');
  17.     foreach($dataModel as $k=>$d)
  18.       $row[]=$k;
  19.   }
  20. ?>
  21.  
  22. <div class="form-group field-contacts-group_id">
  23.   <label class="col-sm-2 control-label" for="contacts-group_id">Group</label>
  24.   <div class="col-sm-10">
  25.     <?php
  26.       echo Select2::widget([
  27.           'name' => 'Contacts[group_id]',
  28.           'value' => $row, //
  29.           'data' => $data,
  30.           'options' => ['placeholder' => 'Select a group ...', 'multiple' => true],
  31.           'pluginOptions' => [
  32.               'tags' => true,
  33.               'maximumInputLength' => 10
  34.           ],
  35.       ]);
  36.     ?>
  37.  
  38.   </div>
  39. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement