DEV Community

Irman Firmansyah
Irman Firmansyah

Posted on • Edited on • Originally published at irmanf.com

Menggunakan Library Datatable di Bootstrap 5

Include CSS nya di header

<link rel="stylesheet" href="//cdn.datatables.net/1.13.7/css/dataTables.bootstrap5.min.css" />
Enter fullscreen mode Exit fullscreen mode

Include JS nya sebelum tag </body>

Datatable membutuhkan jQuery, jika sudah di include bisa di skip

<script src="//code.jquery.com/jquery-3.7.0.js"></script>
<script src="//cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.13.7/js/dataTables.bootstrap5.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Deklarasi datatable untuk table dengan id datatable

<script>
$(document).ready(function () {
    $("#datatable").DataTable({
        searching: true,
        search: {
            smart: false,
        },
        paging: true,
        lengthChange: true,
        ordering: true,
        info: true,
        language: {
            // menggunakan bahasa Indonesia
            url: "//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json",
        },
    });
});
</script>
Enter fullscreen mode Exit fullscreen mode

searching, paging, lengthChange, ordering dan info semua nilai default nya adalah true, jadi config tersebut bisa dihilangkan, kecuali mau diubah nilai nya ke false

<script>
$(document).ready(function () {
    $("#datatable").DataTable({
        search: {
            smart: false,
        },
        language: {
            // menggunakan bahasa Indonesia
            url: "//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json",
        },
    });
});
</script>
Enter fullscreen mode Exit fullscreen mode

Opsi smart di set false karena secara default, Datatable menerapkan smart filtering

Jadi jika keyword pencarian dua kata atau lebih, dia akan split kata-kata tersebut menjadi masing-masing dan melakukan pencarian dari dua kata tersebut

Contoh table

<table class="table" id="datatable">
    <thead>
        <tr>
            <th>No</th>
            <th>Nama</th>
            <th>Username</th>
            <th>Role</th>
            <th>Aksi</th>
        </tr>
    </thead>
    <tbody>
        <!-- looping data -->
    </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay