2015-09-29 66 views
0

我被困在一个点上,请解开这个谜,我使用选择2延伸好在它是一个可搜索的下拉,就像当我开始打字,它从我的business表加载存储的数据。只是一个查询如何显示我选择的业务更新视图中,我目前在地址视图中,有四个字段,sector, city, business, street。我正在使用select2扩展名获得商家名称,但它正在工作,但当我更新地址时,每个存储字段的数据都会出现,但业务除外。 这里是我的地址/视图/ _form选择2扩展,更新所选值

<?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 --> 

我怎样才能在更新视图选定值(业务)的代码?

+0

希望这有助于你http://stackoverflow.com/questions/22972985/how-to-load-model-data-to-select2 -dropdown,其中用途阿贾克斯滤波功能于警予/ 22979412#22979412 –

+0

我已经访问过的链接,有没有什么办法来解决这个没有Ajax?因为我不太了解ajax编码。 –

+0

是的,使用initSelection可以自定义如何加载select2。 JavaScript是必需的,Ajax是可选 –

回答

1

对于单值,在你看来并选择2定义和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的< = 1.x中,单选择2值,添加initSelection功能Yii中你选择2定义所需的方法:

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。示例读取控制器中的数据集。您需要填写$ dataMultiSelect使用JSON对象:id,text要求initSelection

$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) 
));