select2扩展名,更新所选值

时间:2015-09-29 19:55:11

标签: php jquery json yii jquery-select2

我陷入困境,请解开这个谜,我正在使用select2扩展,幸运的是它是一个可搜索的下拉菜单,就像我开始输入时一样,它从我的business表中加载存储的数据。只是查询如何在更新视图中显示我选择的业务,我目前在地址视图中,其中有四个字段sector, city, business, street。我正在使用select2扩展获取商家名称,它正在工作,但是当我更新地址时,除了业务之外,每个存储的字段的数据都会出现。 这是我的地址代码/ views / _form

<?php
/* @var $this AddressController */
/* @var $model Address */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
    'id'=>'address-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">

        <?php echo $form->textFieldControlGroup($model,'street_number',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($model,'street_number'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'business_id'); ?>
         <?php

  $this->widget('ext.select2.ESelect2',array(
  'name'=>'Address[business_id]',
  'data'=>CHtml::listData(Business::model()->findAll(), 'id', 'business_name'), //the whole available list
  'htmlOptions'=>array(
       'placeholder'=>' search business name?',
    //'options'=>$options, //the selected values
    //'multiple'=>'multiple',
    'style'=>'width:530px',
  ),
  ));
    ?>
    </div>
       </br>

    <div class="row">

        <?php echo $form->textFieldControlGroup($model,'sector',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($model,'sector'); ?>
    </div>

    <div class="row">

        <?php echo $form->textFieldControlGroup($model,'city',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($model,'city'); ?>
    </div>

    <div class="row buttons">
        <?php echo BsHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

如何在更新视图中获取所选值(业务)?

1 个答案:

答案 0 :(得分:1)

对于单个值,在您的视图和select2定义和select2 version <= 3.5.3中,我们需要使用Javascript / JQuery来使用initSelection函数。来自select2 documentation

Called when Select2 is created to allow the user to initialize the selection based on the value of the element select2 is attached to.

Essentially this is an id->object mapping function.

initSelection(element, callback)

Parameter  Type         Description

element    jQuery array Element Select2 is attached to.
callback   function     Callback function that should be called with the data which is either an object in case of a single select or an array of objects in case of multi-select.

This function will only be called when there is initial input to be processed.
Here is an example implementation used for tags. Tags are the simplest form of data where the id is also the text:
$("#tags").select2({
    initSelection : function (element, callback) {
        var data = [];
        $(element.val().split(",")).each(function () {
            data.push({id: this, text: this});
        });
        callback(data);
    }
});

// Or for single select elements:
$("#select").select2({
    initSelection : function (element, callback) {
        var data = {id: element.val(), text: element.val()};
        callback(data);
    }
});

现在对于Yii&lt; = 1.x,使用单个select2值,以Yii所需的方式将initSelection函数添加到您的select2定义:

echo $form->textField($model, 'business_id');
$this->widget('ext.select2.ESelect2',array(
    'options'=>array(
        //Html input for related model_id field
        'selector'=>'Address[business_id]',
        'data'=>CHtml::listData(Business::model()->findAll(), 'id', 'business_name'),
        'initSelection'=>'js:function(element,callback) {
               // here your code to load and build your values,
               // this is very basic sample. $model comes from 
               // your yii Controller
               var id='.$model->business_id.'; 
               var text='.$model->business_name.'; 
               data = {
                 "id": id,
                 "text": text
               }
               callback(data);                       
        }',
      ),
    ));

使用多选:

echo $form->textField($model, 'business_id');

$this->widget('ext.select2.ESelect2',array(
    'options'=>array(
    //In $model->business_id, you must first to load values in 
    //in format: 1,4,6,7,8. You can perform this on controller or
    //model
        'selector'=>'Address[business_id]',
        'data'=>CHtml::listData(Business::model()->findAll(), 'id', 'business_name'),

        'multiple'=>'multiple',
        'initSelection'=>'js:function(element,callback) {
             //$dataMultiselect, loaded from controller or model
             callback('.$dataMultiSelect.');
        }',
    ),
));

如何加载$ dataMultiSelect。读取控制器中的数据集的示例。您需要使用id,text所需的JSON对象initSelection填充$ dataMultiSelect:

$dataMultiSelect = array();
foreach($listRelatedData as $relatedData):
    $dataMultiSelect[] = array(
                   'id'=>$relatedData->id, 
                   'text'=>$relatedData->description);
endforeach;
...
...
$this->render('update', array(
                'model' => $model,
                'dataMultiSelect' => CJSON::encode($dataMultiSelect)
  ));