【发布时间】:2016-08-04 07:09:59
【问题描述】:
**表** - ServiceAreaCategory
字段 - id,name
表格 - 技能
字段 - id,skill_name,service_area_category_id
在选择 ServiceAreaCategory id 时,我正在从技能表中获取技能名称。
现在,我想在状态列表框的 optgroup 中显示 ServiceAreaCategory 表的名称。
我有一个依赖下拉列表框,例如国家和州...在选择国家时,我正在通过 ajax 获取所选国家的状态。
现在我想在 optgroup of states 列表框中显示国家标签。
for ex - 当我从国家列表框中选择印度和美国时..
状态列表框显示如下结果..
india
--abc
--def
America
--abc
--def
谁能帮我获取并在选择框中显示我所选状态的 optgroup 标签。
下面是我的代码..
// 用于显示国家/地区
<?php
echo $this->Form->input('UserLogDetail.service_area_category_id', array(
'id' => 'shipping_type',
'required' => false,
'multiple' =>'multiple',
'type' => 'select',
'class' => 'form-control',
'label' => false,
'options' => $serviceCategory
));
?>
**// for displaying states of selected countries**
<?php
echo $this->Form->input('UserLogDetail.skills', array(
'class' => 'selectpicker',
'required' => false,
'multiple' =>'multiple',
'id' => 'skills',
'label' => false,
'options' => '$skills'
));
?>
**//contoller functions**
public function getServiceArea(){
$this->loadModel('ServiceAreaCategory');
$serviceCategory = $this->ServiceAreaCategory->find('list',array('conditions'=>array('is_active'=>1),'fields'=>array('ServiceAreaCategory.id','ServiceAreaCategory.name'),'order'=>'name ASC'));
$this->set('serviceCategory',$serviceCategory);
}
public function loadSkills() {
$this->loadModel('Skill');
$skills = array();
if (isset($this->request['data']['id'])) {
$ids = explode(",",$this->request['data']['id']);
if(count($ids)>1){
$skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
'Skill.service_area_category_id IN' => $ids)));
} else {
$skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
'Skill.service_area_category_id' => $ids)));
}
}
echo json_encode($skills);
exit();
}
**Ajax function**
<script type="text/javascript">
$(document).ready(function() {
$("#shipping_type").on('change', function() {
var id = $(this).val();
if (id) {
var dataString = 'id=' + id;
$.ajax({
type: "POST",
url: '<?php echo Router::url(array("controller" => "Profiles", "action" => "loadSkills"),true); ?>',
data: dataString,
dataType: 'JSON',
cache: false,
success: function(html) {
$("#skills").html("");
$.each(html, function(key, value) {
$('<option>').val('').text('select');
$('<option>').val(key).text(value).appendTo($("#skills"));
});
$('#skills').selectpicker('refresh');
}
});
}
});
});
</script>
【问题讨论】:
-
请分享你的数据库表
-
我用我的问题中的字段名称和场景更新了我的表格...