2

Hi I need help at my datatables. My problem is I want my datatables to show datas based only the schoolyear that has the value of schoolyear_id at the select dropdown as you will see here.

Schoolyear Dropdown Image

So far this is my variables in the dropdown.

 <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>

Now this is my custom js for the datatables and ajax.

var manageRegistrationTable;

$(document).ready(function() {

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

    });
});

This is my controller function that will be access by the 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    
}

Now for the model.

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();
}
}

I'm thinking that this needs an onchange function or what? And how will I pass the value of the schoolyear_id to the datatables ajax.

1 Answer 1

1

Sorry I have to answer because I dont have enough reputation to add comment. Generally, the datatable js will have option called ajaxOptions to pass the paramter to it.

You can also to this issue here for similar scenario.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.