Codeigniter:根据选择ID值更改数据表

时间:2017-04-23 15:50:17

标签: javascript ajax codeigniter datatable

您好我的数据表需要帮助。我的问题是我希望我的数据表只显示基于schoolyear的数据,该数据在选择下拉列表中具有schoolyear_id的值,如您所见。

Schoolyear Dropdown Image

到目前为止,这是我在下拉列表中的变量。

 <select class="form-control" style="width:115px" name="schoolyearData" id="schoolyearData">
   <?php foreach ($schoolyearData as $schoolyear)
   {
   ?>
      <option value="<?php echo $schoolyear['schoolyear_id']; ?>"> <?php echo $schoolyear['schoolyear_date']; ?> </option>

   <?php 
   } 
   ?>
 </select>

现在这是我对数据表和ajax的自定义js。

var manageRegistrationTable;

$(document).ready(function() {

    manageRegistrationTable = $("#manageRegistrationTable").DataTable({
        'ajax' : 'registration/fetchRegistrationData',
        'order' : []

    });
});

这是我的控制器功能,将由ajax访问。

public function fetchRegistrationData($registrationId = null)
{
if($registrationId) {
  $registrationData = $this->model_registration->fetchRegistrationData($registrationId);
  echo json_encode($registrationData); // USED FOR EDITING
}
else {
  $registrationData = $this->model_registration->fetchRegistrationData();
  $result = array('data' => array());

  $x = 1;
  foreach ($registrationData as $key => $value) {

    $button = '<!-- Single button -->
    <div class="btn-group">
      <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Action <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        <li><a type="button" class="btn btn-default" style="text-align:left;" data-toggle="modal" data-target="#viewRegistraionModal" onclick="viewRegistration('.$value['registration_id'].')"> <i class="glyphicon glyphicon-user"></i> View</a></li>
        <li><a type="button" class="btn btn-default" style="text-align:left;" data-toggle="modal" data-target="#updateRegistrationModal" onclick="editRegistration('.$value['registration_id'].')"> <i class="glyphicon glyphicon-edit"></i> Edit</a></li>
        <li><a type="button" class="btn btn-default" style="text-align:left;" data-toggle="modal" data-target="#removeRegistrationModal" onclick="removeRegistration('.$value['registration_id'].')"> <i class="glyphicon glyphicon-trash"></i> Remove</a></li>       
      </ul>
    </div>';

    $photo = '  <img src="../'.$value['student_image'].'" alt="Photo" class="img-circle candidate-photo" width="95"  height="95"/>';

    $result['data'][$key] = array(
      $x,
      $photo,
      $value['student_lastname'],
      $value['student_firstname'],
      $value['student_middlename'],
      $value['schoolyear_date'],
      $value['gradelevel_name'],
      $value['section_name'],
      $value['registration_dateadded'],
      $button
    );
    $x++;
  } // /froeach

  echo json_encode($result);
} // /else    
}

现在是模特。

public function fetchRegistrationData($registrationId = null)
{
if($registrationId) {
  $sql = "SELECT * FROM tbl_registration 
  INNER JOIN tbl_student ON tbl_student.student_id = tbl_registration.student_id 
  INNER JOIN tbl_schoolyear ON tbl_schoolyear.schoolyear_id = tbl_registration.schoolyear_id 
  INNER JOIN tbl_gradelevel ON tbl_gradelevel.gradelevel_id = tbl_registration.gradelevel_id 
  INNER JOIN tbl_section ON tbl_section.section_id = tbl_registration.section_id WHERE registration_id = ?";
  $query = $this->db->query($sql, array($registrationId));
  return $query->row_array();

  // USED FOR EDIT
} 
else {
  $sql = "SELECT * FROM tbl_registration 
  INNER JOIN tbl_student ON tbl_student.student_id = tbl_registration.student_id 
  INNER JOIN tbl_schoolyear ON tbl_schoolyear.schoolyear_id = tbl_registration.schoolyear_id 
  INNER JOIN tbl_gradelevel ON tbl_gradelevel.gradelevel_id = tbl_registration.gradelevel_id 
  INNER JOIN tbl_section ON tbl_section.section_id = tbl_registration.section_id ORDER BY schoolyear_date DESC";
  $query = $this->db->query($sql);
  return $query->result_array();
}
}

我认为这需要一个onchange功能或什么? 我将如何将schoolyear_id的值传递给数据表ajax。

1 个答案:

答案 0 :(得分:1)

抱歉,我必须回答,因为我没有足够的声誉来添加评论。通常,数据表js将具有名为ajaxOptions的选项,以将参数传递给它。

对于类似的情况,您也可以针对此问题here