首页 > 解决方案 > 如何解决这个错误(Uncaught TypeError: Cannot read property 'aDataSort' of undefined)?

问题描述

我该如何解决这个问题,这是我的代码:

<html>
    <head>
    <script>       
    $(function() {
        $('#pemohon-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: 'http://localhost:8000/pemohon/get_datatable'
        });
    });
    </script>
    </head>

    <body>
    <table id="pemohon-table" class="table">
        <thead>
            <tr>No</tr>
            <tr>Nomor Permohonan</tr>
            <tr>Tanggal Permohonan</tr>
            <tr>Nama</tr>
            <tr>Tanggal Lahir</tr>
            <tr>Status</tr>
        </thead>
    </table>
    </body>

我得到了这个错误:

未捕获的类型错误:无法读取未定义的属性“aDataSort”

我不知道具体是什么问题,请帮助我。谢谢。

标签: ajaxlaraveldatatabledatatables

解决方案


如果你使用数据表,也许你可以这样设置:

在控制器中,您制作 getData 功能

public function getData(Request $request){
    $pemohon = YourModel::select('your_coloum_in_database.*');
    return Datatables::of($pemohon)
                    ->make(true);
}

在您看来,您可以这样调用:

<table class="table table-bordered table-hover table-striped w100" cellspacing="0" id="datatable"></table>

    <script>


$('#datatable').dataTable({
                        processing: true,
                        serverSide: true,
                        ajax: {
                            method: 'POST',
                            url : '{{ route('your_route') }}',
                            headers: {
                                'X-CSRF-TOKEN': '{{ csrf_token() }}'
                            }
                        },
                        columns : [
                            {title: 'No', data: 'no, name: 'no', defaultContent: '-', class: 'text-center'},
                            {title: 'Nomor Permohonan', data: 'nomor_permohonan', name: 'nomor_permohonan', defaultContent: '-', class: 'text-center'},
                            {title: 'Tanggal Permohonan', data: 'tanggal_permohonan', name: 'tanggal_permohonan', defaultContent: '-', class: 'text-center'},
                            {title: 'Nama', data: 'nama', name: 'nama', defaultContent: '-', class: 'text-center'},
                            {title: 'Tanggal Lahir', data: 'tanggal_lahir', name: 'tanggal_lahir', defaultContent: '-', class: 'text-center'},{title: 'Status', data: 'status', name: 'status', defaultContent: '-', class: 'text-center'},
    </script>

推荐阅读