Binding Select Options In Dropdown Dynamically
Binding Select Options In Dropdown Dynamically
rizwansultan88
Posts: 7Questions: 2Answers: 0
After populating dropdown dynamically in DataTable Editable not able to post data on submit.
Here is my code for binding data in dropdown
editor.dependent('country_id', function(val,data,callback){
$.ajax ({
url : '/AccountsBankDetails/getStates',
data : {
"action" : "getRecords4Select_All",
"country_id" : data.rows[0].country_id,
"state_id":data.rows[0].region_id,
"YII_CSRF_TOKEN" :"<?php echo Yii::app()->request->csrfToken;?>"
},
type : 'post',
dataType : 'text',
success : function ( json ) {
$('#'+data.rows[0].DT_RowId+' #DTE_Field_state_id').html(json);
$('#'+data.rows[0].DT_RowId+' #DTE_Field_state_id').attr("selected", "selected");
}
});
});
I'm Sending Ajax request like that
public function actiongetStates(){
if(isset($_REQUEST['action']) && $_REQUEST['action']=="getRecords4Select_All"){
$criteria = new CDbCriteria(
[
'select'=>['RegionID','RegionName'],
'condition'=>'Deleted=:Deleted and CountryID=:CountryID',
'params'=>[':Deleted'=>0,'CountryID'=>$_REQUEST['country_id']]
]
);
$records = Regions::model()->findAll($criteria);
$html="<option value=''>Select States</option>";
$rows=array();
foreach($records as $row)
{
$html.="<option value='".$row->RegionID."'>".$row->RegionName."</option>";
}
echo $html; exit;
}
}
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You can't update the
select
element directly I'm afraid. Editor uses a custom DOM property for the value allowing it to correctly handle typing information - e.g.true
v'true'
.To update the options in a select list for Editor you need to use the
field().update()
method. This is detailed in theselect
documentation.Allan
@allan can you please share any example or sample code.
There is an example of how to use
field().update()
in theselect
documentation (right at the bottom of the page).Allan