我需要创建一个通过ajax填充数据的表,其代码如下:
查看:
<form class="form-inline" id="form_filter" method="POST">
<label for="date_from">From </label>
<input type="date" name="date_from">
<label for="date_to"> To </label>
<input type="date" name="date_to">
<input type="button" name="btn_view_records" value="Filter Date" class="btn btn-success btn-sm" id="btn_view_records">
</form>
$('#btn_view_records').click(function(){
$.ajax({
type:"POST",
data:$('#form_filter').serialize(),
datataype:"JSON",
url:"<?php echo site_url('pakyaw/get_filtered_data');?>",
success:function(data){
$("#log-list").html(data);
}
});
});
控制器:
public function get_filtered_data(){
$this->load->library('table');
$this->table->set_heading('Bio Id', 'Log Date', 'Time In', 'Time Out', 'Time Rendendered');
$style = array('table_open' => '<table class="table table-striped table-hover">');
$this->table->set_template($style);
echo $this->table->generate($this->model_pakyaw->get_filtered_data($this->session->userdata('branch_name'), $this->input->post('date_from'), $this->input->post('date_to')));
}
MODEL:
public function get_filtered_data($branch_name, $date_from, $date_to){
$this->output->enable_profiler(TRUE);
$str2="select bio_id as 'bio_id', cast(attendance as date) as 'Log Date', min(attendance) as 'time_in', max(attendance) as 'time_out', timediff(max(attendance), min(attendance)) as 'difference'
FROM delwater_downydb.pakyaw_attendance
WHERE attendance > '".$date_from."'
GROUP BY bio_id, cast(attendance as date)
ORDER BY bio_id, cast(attendance as date) desc;";
$query=$this->db->query($str2);
return $query->result_array();
}
当我点击btn_view_records时,它不会过滤结果。我检查过,我已经看到date_from没有被传递。 sql语法正在获得&#39;&#39;这就是为什么它没有正确过滤的价值。你能告诉我我做错了吗?感谢。
答案 0 :(得分:0)
你的ajax电话中的错字?
datataype:"JSON",
应为dataType:"JSON",
答案 1 :(得分:0)
传递分配给帖子值的变量或尝试在模型中打印POST值。
print_r($_POST); //in model
$from_date = $this->input->post('date_from');//in model
$date_to= $this->input->post('date_to');//in model
$result = $this->table->generate($this->model_pakyaw->get_filtered_data( $from_date, $date_to, $any_other_variable );// in controller
print_r($result);// in controller