简体   繁体   English

根据下拉菜单中的选定值更改数据表值(CodeIgniter)

[英]Change data table values based on selected value from dropdown (CodeIgniter)

so I have a datatable displaying all courses from the database and I want to sort the data using values from dropdown (which contains department of courses) I want to sort all courses depending on the selected department. 所以我有一个数据表显示数据库中的所有课程,我想使用下拉列表中的值对数据进行排序(包含课程的部门),我想根据所选部门对所有课程进行排序。 For example, I selected ICT department, all courses from that department will be displayed on the data table. 例如,我选择了ICT部门,该部门的所有课程都将显示在数据表中。 How will I do this? 我该怎么做?

Model: 模型:

 public function getCoursesByDepartment($DepartmentID)
    {
        $this->dbi->select('course.CourseCode, course.CourseTitle');
        $this->dbi->from('course');
        $this->dbi->where('course.DepartmentID', $DepartmentID);
        $res = $this->dbi->get()->result();

        return $res;
    }



public function get_departments() { 
        $result = $this->dbi->select('DepartmentID, DepartmentName')->get('department')->result_array(); 

        $DepartmentID = array(); 
        foreach($result as $r) { 
            $DepartmentID[$r['DepartmentID']] = $r['DepartmentName']; 
        } 

        $DepartmentID[''] = 'Select Department...'; 
        return $DepartmentID; 
    } 

Controller: 控制器:

 public function getcoursesbydepartment()
 {

        $this->load->model('Admin_model');
        $allcourses = $this->Admin_model->getCoursesByDepartment($DepartmentID);
        $data['courses'] = $allcourses;
        $this->load->view('Admin/managecourses_view', $data);


     }

 public function getcourses() 
{ 
    $data['DepartmentID'] = $this->Admin_model->get_departments(); 
    $this->load->view('Admin/addcourse_view', $data); 
} 

In my view, my dropdown are populated with values from the database. 在我看来,我的下拉列表中填充了数据库中的值。 View: 视图:

  <div class="card-pf-body" style="margin-top: 5px;">

      <form class="needs-validation" novalidate >


   <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom03"  style="margin-top: 15px!important;margin-left: -20px!important">Sort by department:</label>

        <div class="panel-body" style="margin-left: -36px;margin-top: -15px;"> 
                <!--dropdown input--> 

       <!--HERE IS MY DROPDOWN (VALUES RETRIEVED FROM DATABASE)-->

        <?php echo form_dropdown('DepartmentID', $DepartmentID, '', 'class="form-control"', 'name="DepartmentID"', '#', 'id=departmentid', (isset($_POST['DepartmentID']) ? $_POST['DepartmentID'] : ''), 'id="DepartmentID"') ?>



            </div> 
    </div>    

     <div class="col-md-4 mb-3">

    </div>    
  </div>


   <p style="margin-top: 20px;margin-right: 1000px;margin-left: 5px;width: 80px;margin-bottom: 10px"><font style="color: #ffffff">p</font></p>

</form>

    <table class="table table-responsive table-bordered table-striped" id="example"> 
      <thead>
    <tr>
      <th>Course</th>
      <th>Title</th>
     <!--  <th>Description</th> -->

      <th>Option</th>
    </tr>
    </thead>
    <?php 
    $i = 0;
    foreach ($courses AS $course): ?>
    <tr>

      <td><?php echo $course->CourseCode; ?></td>
      <td><?php echo $course->CourseTitle; ?></td>

    <!--   <td><a href="<?php echo $course->CourseDescription; ?>">View Description</a></td> -->
      <!-- <td><?php echo $course->CourseDescription; ?></td> -->
      <td>
        <a href="#"" onClick="editcourse(<?php echo $course->CourseID;?>)">Edit<i class="fa fa-pencil" style="margin-left: 5px"></i></a>  

       <!--  <button class="btn btn-primary" onclick="editcourse(<?php echo $course->CourseID;?>)"><i class="glyphicon glyphicon-remove"></i></button> -->

        &nbsp; |    &nbsp;
       <a href="#" onClick="deletecourse(<?php echo $course->CourseID;?>)"><font style="color: #D2553D;">Delete</font><i class="fa fa-times" style="margin-left: 5px;color: #D2553D;"></i></a>

      </td>
    </tr>
   <?php endforeach; ?> 
    </table>
  </div>
  </div>
</div>



<script>
  $(document).ready(function() {
    // matchHeight the contents of each .card-pf and then the .card-pf itself
    $(".row-cards-pf > [class*='col'] > .card-pf .card-pf-title").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf > .card-pf-body").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf > .card-pf-footer").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf").matchHeight();

    // Initialize the vertical navigation
    $().setupVerticalNavigation(true);
  });
</script>

<script type="text/javascript">
   $(document).ready(function() {

    $('#example').DataTable();
} );
 </script>

Basically you want to render the page with all options and then, on select change, redirect the user to the same page with an additional url attribute /somepage/{someid} 基本上,您希望使用所有选项来呈现页面,然后在选择更改时,使用附加的url属性/somepage/{someid}将用户重定向到同一页面

// [CONTROLLER]
// specific department: /getcoursesbydepartment/{dept_id}
// all departments:     /getcoursesbydepartment/
public function getcoursesbydepartment($DepartmentID = null) {
    $this->load->model('Admin_model');
    $data['departments'] = $this->Admin_model->get_departments();
    $data['courses'] = $this->Admin_model->getCoursesByDepartment($DepartmentID);
    $data['selected_department'] = $DepartmentID; // added
    $this->load->view('Admin/managecourses_view', $data);
}

This necessitates a change in your model code: 这需要更改模型代码:

Note: I've added so the function returns null if there are no results, this is generally a good practice (to either return null, false or an empty array() ). 注意:我已经添加了该函数,如果没有结果,该函数将返回null,这通常是一个好习惯(返回null,false或空array() )。 Otherwise, result() and foreach in view will fail if there are no rows. 否则,如果没有行,则view中的result()foreach将失败。

public function getCoursesByDepartment($DepartmentID = null) {
    $this->dbi->select('course.CourseCode, course.CourseTitle');
    $this->dbi->from('course');
    if (!is_null($DepartmentID)) {
        $this->dbi->where('course.DepartmentID', $DepartmentID);
    }
    $query = $this->dbi->get();
    if ($query->num_rows() < 1) {
        return null;
    }
    return $query->result();
}

Your dropdown code also looked a bit off, I fixed it and added the selected department as the value to be selected. 您的下拉代码看起来也有些偏离,我对其进行了修复并将所选部门添加为要选择的值。 As you will note, if nothing is selected the value will be null according to the controller code. 您将注意到,如果未选择任何内容,则根据控制器代码,该值将为null

//https://www.codeigniter.com/userguide3/helpers/form_helper.html#form_dropdown
//form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])
echo form_dropdown('DepartmentID', $departments, $selected_department, 'class="form-control" id="DepartmentID"');

The $DepartmentID variable should be an array that looks like: $DepartmentID变量应为如下所示的数组:

array('93'=>'HR', '21'=>'Marketing') where the numbers are the department id's (UPDATE: it looks like you are doing that right). array('93'=>'HR', '21'=>'Marketing') ,其中数字是部门ID(更新:看起来您做对了)。

Now all you have to do is, using js or jquery, on select change redirect to: /getcoursesbydepartment/{dept_id} where {dept_id} is the value of the select box. 现在,您只需使用js或jquery,将选择更改重定向到: /getcoursesbydepartment/{dept_id} ,其中{dept_id}是选择框的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM