Codeigniter:在jquery数据表

时间:2016-01-29 01:49:25

标签: jquery mysql json ajax codeigniter

带有提交按钮的日期范围未使用json和ajax在jquery数据表中的多个列上的两个日期之间进行过滤或显示。我希望它在codeigniter上使用MVC过滤或显示日期格式为yyyy-mm-dd的两个日期之间的日期范围。

例如: 这是jquery数据表[image of jquery datatable]

的图像

我想过滤2016年1月22日至2016年2月25日期间的日期范围。结果/显示将是这样的。 [image of result]

这是jquery,json和ajax(Employee.js):

$(document).ready( function () {
    var table = $('#attendance').dataTable( { 
        "aoColumnDefs": [ {"bSortable": false, "aTargets": [ 0, 1, 4 ] } ],  
        "aaSorting": [], "aLengthMenu": [[5, 15, 25, 50], [5, 15, 25, 50]],
        buttons:['excel'],
        dom:'lrtip'
    } );


    $(function() {
        $( "#fromDate" ).datepicker();
        $( "#toDate" ).datepicker();
    });

//ajax to query attendance
$("#btnFind").click(function(){

    var from = $("#fromDate").val();
    var to = $("#toDate").val();

    $.ajax({
        url:"FindAttendance",
        type:"POST",
        data:{fromDate:from, toDate:to},
        success:function(msg){
//console.log(msg);
$("#divAttendance").html(msg);
}
})
})                                      

} );

这是控制器(home.php):

public function goEmployee() {  // view of employees if they login
        $username = $this->session->userdata('username');
        $this->load->model('Model_attendance');
        $query = $this->Model_attendance->getOne($username);
        $data['EMPLOYEES'] = null;
        $data['isAdmin'] = false; //that will check if the user is admin or not
        if ($query) {
            $data['EMPLOYEES'] = $query;
        }

        $this->load->view('imports/header');
        $this->load->view('imports/menu');

        $this->load->view('employee', $data);
    }

public function FindAttendance(){

        $from=$this->input->post('fromDate');
        $to = $this->input->post('toDate');

        $this->load->model('model_attendance');
        $data = $this->model_attendance->GetAttendance($from,$to);

        echo json_encode($data);
    }

这是视图(employee.php):

<div>
    <input type="text" placeholder="From" id="fromDate">&nbsp;<input type="text" placeholder="To" id="toDate">&nbsp;
    <a href="#" class="btn btn-info" id="btnFind" title="Search Attendance"><span class="glyphicon glyphicon-search"></span></a>
</div><br/>
<div id="divAttendance">

<table class="table table-striped responsive-utilities jambo_table" id="attendance">
    <thead>
        <tr class="headings">
            <th>Employee ID</th>      
            <th>Name</th>
            <th>Time-in</th>
            <th>Time-out</i></th> 
            <th>Duration</th>
        </tr>
    </thead>
    <tbody>

        <?php 
        foreach($EMPLOYEES as $employee){

            $timein = $employee->TIMEIN;
            $timeout = $employee->TIMEOUT;

            ?>

            <tr>
                <td data-order="<?php echo $timein; ?>"><?php echo $employee->empnum; ?></td>
                <td><?php echo $employee->NAME; ?></td>
                <td data-order="<?php echo $timein;?>"><?php echo $timein; ?></td>
                <td data-order="<?php echo $timeout;?>"><?php echo $timeout; ?></td>
                <td><?php 
                $employee->DUR;

                ?>

            </td>
        </tr>
        <?php } ?>
    </tbody>
</table>

这是模型(model_attendance.php):

public function GetAttendance($from, $to){

        $sql = "SELECT a.empnum,CONCAT(a.name,' ',a.midname,' ',a.lastname) AS
                NAME,CONCAT(b.indate,' ',b.intime) AS 'TIMEIN',CONCAT(b.outdate,'
                ',b.outtime)AS 'TIMEOUT', DATEDIFF('timeout','timein') AS 'DUR' 
                FROM employees AS a
                JOIN times AS b ON (a.empnum=b.userid)
                WHERE b.indate BETWEEN
                STR_TO_DATE('".$from."','%m/%d/%Y') AND STR_TO_DATE('".$to."','%m/%d/%Y')";

        $query = $this->db->query($sql);

        return $query->result();
    }

0 个答案:

没有答案