我需要查找特定日期范围内的数据。请帮助我
型号
public function Date($start_date, $end_date){
$query = $this->db->select($this->tables['table_search'])
->where($this->tables['table_search'].'.created_at >=', $start_date)
->where($this->tables['table_search'].'.created_at <=', $end_date)
->group_by($this->tables['table_search'].".created_at")
->get($this->tables['table_search']);
return $query->result_array();
}
控制器
public function Date()
{
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$return = $this->m_model->Date($start_date,$end_date);
echo json_encode($return);
}
Ajax J'S
$('.input-daterange').datepicker({
todayBtn: 'linked',
format: "yyyy-mm-dd",
autoclose: true
});
函数fetch_data(start_date ='',end_date =''){ var dataTable = $('#table')。DataTable({ “处理中”:是的, “ serverSide”:是的, “订购”:[], “ ajax”:{ 网址:“搜索/日期”, 输入:“ POST”, 数据:{ 开始日期:开始日期,结束日期:结束日期 }, 成功:功能(数据){ $('#table')。html(data); } } }); }
$('#search').click(function(){
var start_date = $('#start_date').val();
var end_date = $('#end_date').val();
if(start_date != '' && end_date != ''){
$('#table').DataTable().destroy();
fetch_data(start_date,end_date);
}else{
}
答案 0 :(得分:0)
如果使用服务器端处理,则必须从控制器返回正确的JSON格式,请参见Datatables serverside docs。
在此为客户端:
$('#table').DataTable( { "processing": true, "serverSide": true, "ajax": { "url": "search/Date", "type": "POST" "data": function ( d ) { var start_date = $('#start_date').val(); var end_date = $('#end_date').val(); if(start_date != '' && end_date != ''){ d.start_date = start_date; d.end_date = end_date; } } } } );
一些参数会根据ajax请求自动发送,例如分页的开始和长度,请参见here以限制查询结果。