tkamath99
tkamath99

Reputation: 649

Load DataTables dynamically in JQuery tabs

I am planning to use JQuery Datatables in one of my project, so i decided to do a POC to ensure that everything is planned.

I build a table where i will be printing the values from my Object which will be received as a JSON during my further development. But i am getting an AJAX error for the id i was going to print the data.

I have uploaded the code on JSFiddle!

HTML

 <div id="tab-customers">
            <table id="customers-table" class="display general-table" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Gender</th>
                        <th>City</th>
                        <th>Country</th>
                    </tr>
                </thead>
            </table>
        </div>

JQuery

 $(".tabs").click(function() {
                var source = $(this).data("source");
                var tableId = $(this).data("table");
                initiateTable(tableId, source);
            });
function initiateTable(tableId, source) {
                var table = $("#" + tableId).DataTable({
                    "ajax": source,
                    order: [],
                    columnDefs: [{
                        orderable: false,
                        targets: [0]
                    }],
                    "destroy": true,
                    "bFilter": true,
                    "bLengthChange": false,
                    "bPaginate": false
                });
            }
            initiateTable("customers-table", "customers");
            $("#dynamic-tabs").tabs();
        });

Upvotes: 0

Views: 1305

Answers (1)

User863
User863

Reputation: 20039

Try the columns option

columns: [
    { "data": "Id" }, 
    { "data": "firstName" }, 
    { "data": "lastName" }, 
    { "data": "email" }, 
    { "data": "phone" }, 
    { "data": "gender" }, 
    { "data": "city" }, 
    { "data": "country" }
]

Demo

Upvotes: 1

Related Questions