2017-04-06 262 views
1

我想在部門之後的下拉框中選擇一個部門後填入whoom-to-meet下拉框,但是當我點擊部門列表時,所有部門都會顯示,但是當我選擇一個部門,該部門的相關僱員沒有出現在whoom-to-meet下拉列表中。下面是我的兩張桌子。 部門表根據第一個下拉列表填充一個下拉列表

+----+-----------+ 
| id | dept_name | 
+----+-----------+ 
| 2 | hr  | 
+----+---------- + 

user_master表

+----+-------------+-----------+----------+ 
| id | emp_name | dept_id | type | 
+----+-------------+-----------+----------+ 
| 1 | dipti  |  2  | employee | 
+ ---+-------------+-----------+----------+ 

我控制器

public function create_pass() { 
    $pageid = 1; 
    $data['hh'] = $this->admin_model->get_dept(); 
    $this->load->view('header'); 
    $this->load->view('sidebar'); 
    $this->load->view('createpass', $data); 
    $this->load->view('footer'); 
} 

public function select_item_by_dept() { 
    if ($this->session->userdata('admin_logged_in')) { 
     $category_id = $this->input->post('category_id'); 
     $data['result'] = $this->admin_model->select_item_by_cat($category_id); 

     $HTML = "";    
     foreach ($data['result'] as $list) { 
      $HTML.="<option value='" . $list['dept_id'] . "'>" . $list['emp_name'] . "</option>"; 
     }   
     echo $HTML; 
    } else { 
     $this->load->view('login'); 
    } 
} 

我的模型

function select_item_by_cat($category_id) { 
    $this->db->where('dept_id', '$category_id'); 
    $query = $this->db->get('user_master'); 
    return $query->result_array(); 
} 
function get_dept() { 
    $this->db->select("*"); 
    $this->db->from('department'); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

我看來

<div class="form-group"> 
    <label for="inputPassword3" class="col-sm-3 control-label">Department to go </label> 
    <div class="col-sm-3"> 
    <select class="form-control select2" name="dptgo" id="dptogo" onchange="change_category(this.options[this.selectedIndex].value)" style="width: 100%;"> 
     <option selected="selected">Select</option>              
     <?php foreach($hh as $datas) 
     { ?> 
     <option value="<?php echo $datas['id']; ?>"><?php echo $datas['dept_name']; ?></option> 
<?php } ?> 
    </select> 
    <div class="text-danger"><?php echo form_error('dptgo'); ?></div> 
    </div> 
    <label for="inputPassword3" class="col-sm-2 control-label">Whom to Meet</label> 
    <div class="col-sm-4"> 
    <select class="form-control select2" id="whomtmt" name="wtomeet" style="width: 100%;"> 
     <option selected="selected">Select</option> 
     <option></option> 
    </select> 
    </div> 
</div> 

我的jQuery

<script> 
    function change_category(category_id) { 
    if (category_id == "Select") { 
     $("#whomtmt").html("<option>Select</option>"); 
     $("#whomtmt").trigger("chosen:updated"); 
     $("#whomtmt").trigger("liszt:updated"); 
    } else { 
     loadData(category_id); 
    } 
    } 
    function loadData(category_id) { 
    var dataString = 'category_id=' + category_id; 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo site_url(); ?>/admin/select_item_by_dept", 
     data: dataString, 
     cache: false, 
     success: function (result) { 
     $("#whomtmt").html("<option>Select</option>"); 
     $("#whomtmt").append(result); 
     $("#whomtmt").trigger("chosen:updated"); 
     $("#whomtmt").trigger("liszt:updated"); 
     } 
    }); 
    } 
</script> 

回答

1

在你的模型,在select_item_by_cat功能

這是錯誤的$this->db->where('dept_id', '$category_id');

查詢執行這樣

SELECT * FROM `user_master` WHERE `dept_id` = '$category_id' 

所以將其更改爲

$this->db->where('dept_id', $category_id); 

,並嘗試它可以幫助你:)

+0

你可以檢查你的最後一個查詢y放置這個echo $ this-> db-> last_query();出口();在返回之前$ query-> result_array();在您的select_item_by_cat() – Nobita

+0

後更改'$ category_id'爲$ category_id它工作正常,感謝您的幫助。 – dipti

相關問題