[英]How can I use set_select with Ajax in Codeigniter
我需要使用set_select,但是select2的選項是使用ajax從控制器調用的。 我能怎么做?
我的控制器:
public function getCategoryByLocation()
{
$location_id = $this->input->post('location_id');
$locationscats = $this->Categories_model->getCategorylocal($location_id);
if(count($locationscats) > 0)
{
$select = '';
$select .= '<option value="">'.$this->lang->line('text_none').'</option>';
foreach ($locationscats as $locationscat) {
$select .='<option value="'.$locationscat->category_id.'">'.$locationscat->name.'</option>';
}
echo json_encode($select);
}else{
$select = '';
$select .= '<option value="">'.$this->lang->line('text_none').'</option>';
echo json_encode($select);
}
}
我的模特:
public function getCategorylocal($location_id)
{
$query = $this->db->get_where('categories', array('location_id' => $location_id));
return $query->result();
}
我的觀點:
<select name="parent_id" id="category" class="form-control">
<option value=""><?php echo lang('text_none'); ?></option>
</select>
還有Ajax電話
$(document).ready(function() {
$('#input-location').on('change', function() {
var location_id = this.value;
$.ajax({
url:"<?php echo site_url("/categories/getCategoryByLocation"); ?>",
type: "POST",
data: {'location_id' : location_id},
dataType: 'json',
success: function(data){
$('#category').html(data);
},
error: function(){
alert('Error occur...!!');
}
});
});
$("#input-location").trigger('change');});
可以從控制器中使用set_select嗎?...我在視圖中使用set_select,但在控制器中沒有...
我看不到在視圖中使用set_select的位置,但是由於您要返回選項的HTML片段,因此無需執行json編碼就可以返回視圖。
$this->load->view('view_that_builds_options', $this->locationscats);
或構建$ select var並回顯...
public function getCategoryByLocation() {
$this->load->helper('form');
$location_id = $this->input->post('location_id');
$locationscats = $this->Categories_model->getCategorylocal($location_id);
$select .= '<option value="">'.$this->lang->line('text_none').'</option>';
if(count($locationscats) > 0) {
foreach ($locationscats as $locationscat) {
$select .='<option value="'.$locationscat->category_id.'">'
// assume your select is named 'category' by the id='category'.
$select .= set_select('category', $NoIdeaWhatValueYouAreCheckingForGoesHere);
$select .= $locationscat->name.'</option>';
}
}
echo $select;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.