Finally I get this to work! :) I just hope someone find this solution also useful for them... :lol:
HERE IS THE SOLUTION:
1. Get dependent items using ajax/jquery:
Create a CONTROLLER that creates/receives the needed output based on the selected value on the drop down. I placed it on the same file that contains the controller that generates my datagrid. The file name is: "ctrl_gcrud.php"
public function get_funct_by_prog($progID)
{
$mysqli = new mysqli("localhost", "USERNAME", "PASSWORD ", "DBNAME");
$result = $mysqli->query("SELECT * FROM tblfunction WHERE funct_prog_id ='".$progID."'");
while($row_funct = mysqli_fetch_array($result))
{
echo "<option value='".$row_funct['funct_id']."'>".$row_funct['funct_Name']."</option>";
}
}
2. Create a callback inside the same controller: "ctrl_gcrud.php". This call back is responsible for the following:
a. Delete all elements from the multi-select: $("#field-Functions").find("option").remove();
b. Add new elements in the multi-select: $("#field-Functions").html($response.responseText);
c. Refresh "choosen select" plugin: $("#field-Functions").trigger("liszt:updated"); and $(".remove-all").trigger("click");
Here is the code for the working callback:
function tos_CBae_selFunc()
{
return '
<script type="text/javascript">
$(document).ready(function(){
$("#secret_field_box").hide();
$("#field-tos_prog_id").change(function() {
var progID = $("#field-tos_prog_id").val();
$(".remove-all").trigger("click");
$.ajax({
"url" : "http://localhost/OCGA/ctrl_gcrud/get_funct_by_prog/"+ progID,
"cache" : true,
"beforeSend" : function (){
//Show a message
},
"complete" : function($response, $status){
if ($status != "error" && $status != "timeout") {
$("#field-tos_qry").find("option").remove();
$("#field-tos_qry").html($response.responseText);
$("#field-tos_qry").trigger("liszt:updated");
$("#field-Functions").find("option").remove();
$("#field-Functions").html($response.responseText);
$("#field-Functions").trigger("liszt:updated");
$(".remove-all").trigger("click");
}
},
"error" : function ($responseObj){
alert("Something went wrong while processing your request.\n\nError => "
+ $responseObj.responseText);
}
});
})
})
</script>
' ;
}
3. This is the controller that generates my datagrid: Notice the lines of code that is used to make our callback work.And here is the successful desired output:
public function tos()
{
/*Create ajax_grocery_CRUD instead of grocery_CRUD. This extends the functionality with the
field_set_defaults keeping all functionality as well (I extended this to grocery_CRUD)*/
$crud = new ajax_grocery_CRUD();
// Set theme
$crud->set_theme('datatables');
// Set table name
$crud->set_table('tbltos');
// Set this name instead of 'Record' in prompt
$crud->set_subject('Table of Specification');
//* These displays custom name instead of field name in columns
$crud->display_as('tos_prog_id','Program');
$crud->display_as('tos_course_DescTitle_id','Descriptive Title');
$crud->display_as('tos_course_RefNo_id','Reference No.');
$crud->display_as('tos_course_Title_id','Title');
$crud->display_as('tos_lo_name_id','Learning Objective');
$crud->display_as('tos_topic_name_id','Topic');
$crud->display_as('tos_sem_id','Semester');
$crud->display_as('tos_qtype_id','Question Type');
$crud->display_as('tos_coglvl_id','Cognitive Level');
$crud->display_as('tos_diffindex_id','Difficulty Index');
$crud->display_as('tos_encoder_id','Encoder');
$crud->display_as('tos_reviewed_id','Reviewed');
$crud->display_as('tos_remarks_id','Remarks');
$crud->display_as('tos_status_id','Status');
$crud->display_as('tos_published_id','Published');
$crud->display_as('tos_PilotTest_id','Pilot Tested');
//*
// Sets relation with dependent dropdown list (Program->Function)
$crud->set_relation('tos_prog_id','tblprogram','prog_Name');
// Sets function (this must be dependent on the program)
$crud->set_relation_n_n('Functions', 'tbltos_funct', 'tblfunction', 'tos_id', 'funct_id', "{funct_Name}", 'priority');
// This is the set of code for using the callback -*begin ppp
$f=array'secret','tos_id','tos_assmntype_id','tos_prog_id','Functions','tos_sem_id','tos_qtype_id','tos_remarks_id','tos_reviewed_id','tos_status_id','tos_published_id','tos_PilotTest_id','tos_qry','tos_ctr','tos_need'); $crud->add_fields($f); $crud->edit_fields($f); $crud->callback_add_field('secret',array($this,'tos_CBae_selFunc')); $crud->callback_edit_field('secret',array($this,'tos_CBae_selFunc')); // -*end
// Sets dropdown list (Semester)
$crud->set_relation('tos_sem_id','tblsem','Sem');
// Sets dropdown list (QType)
$crud->set_relation('tos_qtype_id','tblqtype','QType');
// Sets dropdown list (Remarks)
$crud->set_relation('tos_remarks_id','tblqrem','qrem_Name');
// Sets dropdown list (Status)
$crud->set_relation('tos_status_id','tblstatus','status_Name');
// Sets dropdown list (Reviewed bool)
$crud->set_relation('tos_reviewed_id','tblbool','bool_value');
// Sets dropdown list (Published bool) then turn it as default value as text
$crud->set_relation('tos_published_id','tblbool','bool_value');
// Sets dropdown list (PilotTest bool) then turn it as default value as text
$crud->set_relation('tos_PilotTest_id','tblbool','bool_value');
//Removes the print feature $crud->unset_print();
//**** These displays the output in the page
$output = $crud->render();
$this->_view_output($output);
}
Special thanks to the hints provided by none other than the grocery CRUD Hero himself: Sir Victor... It took me almost a month to get it though... I just hope this helps someone too.... This method is also tested working in multiselect created by using this code:
$this->db->select('funct_id, funct_Name');
$results = $this->db->get('tblfunction')->result();
$funct_multiselect = array();
foreach ($results as $result) {
$funct_multiselect[$result->funct_id] = $result->funct_Name;
}
$crud->field_type('tos_qry', 'multiselect', $funct_multiselect);
Here is what it looks like:
Happy GCng! :D