根据第一个下拉列表填充下拉列表

时间:2017-04-06 12:36:27

标签: jquery codeigniter

  

我想在从逐个部门下拉框中选择一个部门之后填充下一个会见下拉框,但是当我点击部门列表时,所有部门都会显示但是当我选择一个部门时,该部门的相关员工没有显示在满足下降的下拉列表中。下面是我的两张桌子。   部门表

+----+-----------+
| 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 个答案:

答案 0 :(得分: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);

并尝试它可以帮助你:)