Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

DataTable is not showing

This is my JavaScript code.

$(document).ready(function(){
            $(document).on('change', '#time', function () {
                time = $('#time').val();
                var url = '{{ route("owner.safety.timeOverview", ":time") }}';
                url = url.replace(':time', time);
                $('#overview').DataTable({
                    bDestroy: true,
                    processing: true,
                    serverSide: true,
                    pageLength: 100,
                    responsive: true,
                    ajax: {
                        url: url,
                        type: 'GET',
                        headers: {
                            'Accept': 'application/json'
                        },
                        success: function(response) {
                        console.log(response); // I am getting output here
                        },
                    },
                    order: [1, 'desc'],
                    ordering: false,
                    autoWidth: false,
                    drawCallback: function () {
                        $(".dataTables_length select").addClass("form-select form-select-sm");
                    },
                    language: {
                        'paginate': {
                            'previous': '<span class="iconify" data-icon="icons8:angle-left"></span>',
                            'next': '<span class="iconify" data-icon="icons8:angle-right"></span>'
                        }
                    },
                    columns: [
                        {
                            "data": "property_id",
                        },
                        {
                            "data": "unit_id",
                        },
                        {
                            "data": "issue_date",
                        },
                        {
                            "data": "expiry_date",
                        },
                        {
                            "data": "certified_by",
                        },
                        {
                            "data": "safety",
                        },
                        {
                            "data": "reminder",
                        },
                    ]
                });
            });
        });

My HTML is like this.

<div class="billing-center-area bg-off-white theme-border radius-4 p-25">
    <table id="overview" class="table responsive theme-border p-20 ">
        <thead>
            <tr>
                <th class="all">{{ __('Property') }}</th>
                <th class="all">{{ __('Unit') }}</th>
                <th class="all">{{ __('Issue Date') }}</th>
                <th class="all">{{ __('Expiry Date') }}</th>
                <th class="all">{{ __('Certified by') }}</th>
                <th class="all">{{ __('Safety Type') }}</th>
                <th class="all">{{ __('Status') }}</th>
            </tr>
        </thead>
    </table>
</div>  

Drop down HTML is like below.

<select id="time" class="form-select flex-shrink-0" name="time">
    <option value="all">Select</option>
    <option value="oneweek">1 Week</option>
    <option value="onemonth">1 Month</option>
    <option value="threemonth">3 Months</option>
    <option value="sixmonth">6 Months</option>
</select>

I am not getting any error in console.

0 likes
2 replies
s4muel's avatar

does your ajax endpoint return data in correct structure? (data enclosed in data root node, like this?

{
  "data": [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011-04-25",
      "0,800"
    ],
...

what happens if you simplify the ajax configuration in datatables initialization, just keep the url

ajax: url,
1 like
afoysal's avatar

@s4muel Thanks. Yes, I am getting response from AJAX request. I am getting below output.

Object { draw: 1, recordsTotal: 2, recordsFiltered: 2, data: (2) […], input: {…} }
​
data: Array [ {…}, {…} ]
​​
0: Object { safety_id: 30, id: 30, property_id: "AzizProperty", … }
​​​
DT_RowIndex: 1
​​​
boiler_contact: null
​​​
certificate_no: null
​​​
certified_by: "dsfds"
​​​
claim_contact: null
​​​
created_at: "2024-01-30T00:00:00.000000Z"
​​​
epc_certificate_no: "sdfsf"
​​​
epc_url: "dsfsf"
​​​
expired: 0
​​​
expiry_date: "30-06-2024"
​​​
governing_body: null
​​​
id: 30
​​​
image: ""
​​​
insurance_type: null
​​​
insurer_name: null
​​​
issue_date: "01-01-2024"
​​​
policy_number: null
​​​
properties_name: "AzizProperty"
​​​
property: Object { id: 40, owner_user_id: 2, property_type: 1, … }
​​​
property_id: "AzizProperty"
​​​
property_unit: Object { id: 38, property_id: 40, unit_name: "Aziz", … }
​​​
ratings: "D"
​​​
remarks: null
​​​
reminder: '<div class = "not_due">Not Due</div>'
​​​
renew_contact: null
​​​
safety: "Epc"
​​​
safety_id: 30
​​​
score: "20"
​​​
serviced_by: null
​​​
status: ""
​​​
type: "epc"
​​​
unit_id: "Aziz"
​​​
updated_at: "2024-05-04T00:00:00.000000Z"
​​​
<prototype>: Object { … }
​​
1: Object { safety_id: 5, id: 5, property_id: "My Property", … }
​​
length: 2
​​
<prototype>: Array []
​
draw: 1
​
input: Object { draw: "1", start: "0", length: "100", … }
​
recordsFiltered: 2
​
recordsTotal: 2
​
<prototype>: Object { … }

Please or to participate in this conversation.