将datatable属性添加到动态创建的表中

时间:2016-12-19 05:36:05

标签: javascript jquery ajax twitter-bootstrap

我有一个在按钮点击时动态创建的表。我可以得到结果,但事情是结果没有搜索框和分页。以下是我的代码:

查看:

<div id="log-list">
</div>

<script type="text/javascript" src="<?=base_url()?>assets/jquery/jquery.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/js/chart.min.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/datatables/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/datatables/js/dataTables.bootstrap.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/jquery/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[name=date_from]').val('<?echo date('Y-m-d')?>');
$('input[name=date_to]').val('<?echo date('Y-m-d')?>');

$('#btn_view_records').click(function(){
    $.ajax({
        type:"POST",
        data:$('#form_filter').serialize(),
        datatype:"JSON",
        url:"<?php echo site_url('pakyaw/get_filtered_data');?>",
        success:function(data){
            $("#log-list").html(data);

            $('#tbl_results').Datatable({
                "footerCallback": function ( row, data, start, end, display ) {
                    var api = this.api(), data;
                }
            });
        }
    });
});
});/* end of ready */

</script>

控制器:

public function get_filtered_data(){
        $this->load->library('table');
        $this->table->set_heading('Bio Id', 'Time In', 'Time Out', 'Time Rendendered');
        $style = array('table_open'  => '<table class="table table-striped table-hover" id="tbl_results">');
        $this->table->set_template($style);
        $from=$this->input->post('date_from');
        $to= $this->input->post('date_to');
        $branch_name=$this->session->userdata('branch_name');
        echo $this->table->generate($this->model_pakyaw->get_filtered_data($branch_name, $from, $to));
    }

我的错误说明了这一点:

(index):119 Uncaught TypeError: $(...).Datatable is not a function
    at Object.success ((index):119)
    at fire (jquery.js:3305)
    at Object.fireWith [as resolveWith] (jquery.js:3435)
    at done (jquery.js:9242)
    at XMLHttpRequest.<anonymous> (jquery.js:9484)

请告诉我我做错了什么。谢谢。

1 个答案:

答案 0 :(得分:1)

当脚本依赖于库或其他脚本时,脚本顺序很重要。

<script type="text/javascript" src="<?=base_url()?>assets/jquery/jquery.js"></script>

<script type="text/javascript" src="<?=base_url()?>assets/jquery/jquery-ui.min.js"></script>

<script type="text/javascript" src="<?=base_url()?>assets/datatables/js/jquery.dataTables.min.js"></script>