2015-09-29 68 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) 
));