Could we help you? Please click the banners. We are young and desperately need the money
DataTables is a jQuery plugin that transforms plain HTML tables into fully interactive grids. It supports searching, sorting, pagination, responsive design, and can load data dynamically via JSON or AJAX. It’s widely used for dashboards, admin panels, and any application that needs to display structured data efficiently.
Instead of fetching JSON via AJAX, you can keep your data directly inside a JavaScript variable and initialize the DataTable from it:
const employeesJson = [
{ "name": "Tiger Nixon", "position": "System Architect", "office": "Edinburgh", "age": 61, "start_date": "2011/04/25", "salary": "$320,800" },
{ "name": "Garrett Winters", "position": "Accountant", "office": "Tokyo", "age": 63, "start_date": "2011/07/25", "salary": "$170,750" }
];
jQuery(document).ready(function($) {
$('#example').DataTable({
data: employeesJson,
columns: [
{ data: 'name', title: 'Name' },
{ data: 'position', title: 'Position' },
{ data: 'office', title: 'Office' },
{ data: 'age', title: 'Age' },
{ data: 'start_date', title: 'Start Date' },
{ data: 'salary', title: 'Salary' }
],
responsive: true,
pageLength: 10
});
});
Filter table rows dynamically by a specific column, for example office location:
jQuery(document).ready(function($) {
var table = $('#example').DataTable({
data: employeesJson,
columns: [
{ data: 'name', title: 'Name' },
{ data: 'position', title: 'Position' },
{ data: 'office', title: 'Office' },
{ data: 'age', title: 'Age' },
{ data: 'start_date', title: 'Start Date' },
{ data: 'salary', title: 'Salary' }
]
});
$('#officeFilter').on('change', function() {
table.column(2).search(this.value).draw();
});
});
HTML filter:
<select id="officeFilter">
<option value="">All</option>
<option value="Tokyo">Tokyo</option>
<option value="Edinburgh">Edinburgh</option>
</select>
$('#example').DataTable({
data: employeesJson,
columns: [...],
paging: true,
pageLength: 10,
lengthMenu: [5, 10, 25, 50]
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
searching: true
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
ordering: true,
order: [[1, 'asc']]
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
responsive: true
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
scrollY: '50vh',
scrollX: true
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
columnDefs: [
{ targets: [0], visible: false }
]
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
dom: 'Bfrtip',
buttons: ['csv', 'excel', 'pdf', 'print']
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
select: true
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
initComplete: function() {
console.log('Table initialized');
},
rowCallback: function(row, data) {
if (data.age > 60) {
$(row).css('background-color', '#f9f9f9');
}
}
});
$('#example').DataTable({
data: employeesJson,
columns: [...],
language: {
search: "Filter records:",
lengthMenu: "Display _MENU_ rows per page",
info: "Showing _START_ to _END_ of _TOTAL_ entries"
}
});
If you were fetching data from an API, you would normally use serverSide: true
Since in this example we are working with local JSON objects, here’s a simulated version:
$('#example').DataTable({
processing: true,
serverSide: true,
ajax: function(data, callback) {
// Simulate server-side by returning employeesJson
callback({ data: employeesJson });
},
columns: [
{ data: 'name' },
{ data: 'position' },
{ data: 'office' },
{ data: 'age' }
]
});
jQuery(document).ready(function($) {
$('#example').DataTable({
data: employeesJson,
columns: [
{ data: 'name', title: 'Name' },
{ data: 'position', title: 'Position' },
{ data: 'office', title: 'Office' },
{ data: 'age', title: 'Age' },
{ data: 'start_date', title: 'Start Date' },
{ data: 'salary', title: 'Salary' }
],
responsive: true,
pageLength: 10,
dom: 'Bfrtip',
buttons: ['csv', 'excel', 'pdf', 'print']
});
});
DataTables is a versatile and powerful plugin that turns basic HTML tables into interactive, responsive, and feature-rich grids. With options for dynamic JSON loading, filtering, export, responsive design, and server-side processing, it’s a complete solution for any data-heavy web application. Mastering these options allows you to create professional tables for dashboards, admin panels, and public-facing websites efficiently.
Ovde je sve super, medjutim primeri su napisani u jquery, ali zelim da budu u vanila js. Da li mozes to da uradis ali da nista drugo ne menjas, ni nacin pisanja.