Devnote

Web Development Blog Company, Services India

MySQL Error 121 Duplicate key on write - Practical Repair Strategy
MySQL Error 121 Duplicate key on write – Practical Repair Strategy
February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
How to Add Custom Dropdown in DataTable

How to Add Custom Dropdown in DataTable

Image
Devnote team
February 2, 2023
3
7K Views
0 Comments

In this tutorial, we will learn How to Add Custom Dropdown in DataTable. I will show how to use a custom drop-down menu to filter a DataTable by column value. I will give you an example to create a drop-down that displays the unique list of strings from a column and when the user selects the dropdown value, the data table will be rendered with only records with the selected value.

How to Add Custom Dropdown in DataTable

Table of concept

  • Gender column Dropdown Filter
  • Individual column searching

Gender column Dropdown Filter

Gender column Dropdown Filter
Gender column Dropdown Filter
Example
<!doctype html>

<html lang="en">

<head>
    <meta charset="utf-8">

    <title>How to Add Custom Dropdown in DataTable</title>
    <meta name="description" content="">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
    <style>
        .custom-cls {
            display: inline;
            width: 180px;
            margin-left: 25px;
        }
    </style>
</head>

<body>
    <h1 class="text-center m-3">Gender column Dropdown Filter</h1>
    <div class="container mt-4">
        <select id="customFilter" class="form-control custom-cls">
            <option value="">Show All</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
            <option value="Bigender">Bigender</option>
            <option value="Genderfluid">Genderfluid</option>
        </select>

        <!-- Set up the datatable -->
        <table class="table" id="userTable">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Gender</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>Meredithe</td>
                    <td>mhanburybrown0@youku.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>Jere</td>
                    <td>jbartolomeoni1@vk.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>Fara</td>
                    <td>fpreator2@cisco.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>4</td>
                    <td>Neron</td>
                    <td>nmacgarrity3@is.gd</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>5</td>
                    <td>Estel</td>
                    <td>eousbie4@cnn.com</td>
                    <td>Bigender</td>
                </tr>
                <tr>
                    <td>6</td>
                    <td>Brittni</td>
                    <td>bblannin5@barnesandnoble.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>7</td>
                    <td>Huntington</td>
                    <td>hverchambre6@mac.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>8</td>
                    <td>Cristin</td>
                    <td>ckirley7@dmoz.org</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>9</td>
                    <td>Grethel</td>
                    <td>gstroud8@cdbaby.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>10</td>
                    <td>Letitia</td>
                    <td>lbreston9@sphinn.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>11</td>
                    <td>Alonso</td>
                    <td>astoddarta@usda.gov</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>12</td>
                    <td>Vick</td>
                    <td>vtitheringtonb@elegantthemes.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>13</td>
                    <td>Sharity</td>
                    <td>sshilstonec@seattletimes.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>14</td>
                    <td>Korie</td>
                    <td>kboaked@arizona.edu</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>15</td>
                    <td>Terry</td>
                    <td>tbuttwelle@aboutads.info</td>
                    <td>Genderfluid</td>
                </tr>
                <tr>
                    <td>16</td>
                    <td>Estel</td>
                    <td>eousbie4@cnn.com</td>
                    <td>Bigender</td>
                </tr>
                <tr>
                    <td>17</td>
                    <td>Brittni</td>
                    <td>bblannin5@barnesandnoble.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>18</td>
                    <td>Huntington</td>
                    <td>hverchambre6@mac.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>19</td>
                    <td>Cristin</td>
                    <td>ckirley7@dmoz.org</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>20</td>
                    <td>Grethel</td>
                    <td>gstroud8@cdbaby.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>21</td>
                    <td>Letitia</td>
                    <td>lbreston9@sphinn.com</td>
                    <td>Female</td>
                </tr>
            </tbody>
        </table>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>

    <script>
        $("document").ready(function () {

            $("#userTable").dataTable({
                "searching": true
            });

            var table = $('#userTable').DataTable();

            $("#userTable_filter.dataTables_filter").append($("#customFilter"));

            var index = 0;
            $("#userTable th").each(function (i) {
                if ($($(this)).html() == "Gender") {
                    index = i;
                    return false;
                }
            });

            $.fn.dataTable.ext.search.push(
                function (settings, data, dataIndex) {
                    var selectedItem = $('#customFilter').val()
                    var gender = data[index];
                    if (selectedItem === "" || gender.includes(selectedItem)) {
                        return true;
                    }
                    return false;
                }
            );

            //When you change event for the Gender Filter dropdown to redraw the datatable
            $("#customFilter").change(function (e) {
                table.draw();
            });

            table.draw();
        });
    </script>
</body>

</html>
Live Example

Individual column searching

In this example, you can add an event handler when the select element is changed and when you can change the content of the next selection using the selector-modifier.

Individual column searching
Individual column searching
Example
<!doctype html>

<html lang="en">

<head>
    <meta charset="utf-8">

    <title>How to Add Custom Dropdown in DataTable</title>
    <meta name="description" content="">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.min.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
</head>

<body>
    <h1 class="text-center m-3">Individual column searching</h1>
    <div class="container mt-4">

        <!-- Set up the datatable -->
        <table class="table" id="userTable" style="width:100%">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Gender</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>Meredithe</td>
                    <td>mhanburybrown0@youku.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>Jere</td>
                    <td>jbartolomeoni1@vk.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>Fara</td>
                    <td>fpreator2@cisco.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>4</td>
                    <td>Neron</td>
                    <td>nmacgarrity3@is.gd</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>5</td>
                    <td>Estel</td>
                    <td>eousbie4@cnn.com</td>
                    <td>Bigender</td>
                </tr>
                <tr>
                    <td>6</td>
                    <td>Brittni</td>
                    <td>bblannin5@barnesandnoble.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>7</td>
                    <td>Huntington</td>
                    <td>hverchambre6@mac.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>8</td>
                    <td>Cristin</td>
                    <td>ckirley7@dmoz.org</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>9</td>
                    <td>Grethel</td>
                    <td>gstroud8@cdbaby.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>10</td>
                    <td>Letitia</td>
                    <td>lbreston9@sphinn.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>11</td>
                    <td>Alonso</td>
                    <td>astoddarta@usda.gov</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>12</td>
                    <td>Vick</td>
                    <td>vtitheringtonb@elegantthemes.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>13</td>
                    <td>Sharity</td>
                    <td>sshilstonec@seattletimes.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>14</td>
                    <td>Korie</td>
                    <td>kboaked@arizona.edu</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>15</td>
                    <td>Terry</td>
                    <td>tbuttwelle@aboutads.info</td>
                    <td>Genderfluid</td>
                </tr>
                <tr>
                    <td>16</td>
                    <td>Estel</td>
                    <td>eousbie4@cnn.com</td>
                    <td>Bigender</td>
                </tr>
                <tr>
                    <td>17</td>
                    <td>Brittni</td>
                    <td>bblannin5@barnesandnoble.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>18</td>
                    <td>Huntington</td>
                    <td>hverchambre6@mac.com</td>
                    <td>Male</td>
                </tr>
                <tr>
                    <td>19</td>
                    <td>Cristin</td>
                    <td>ckirley7@dmoz.org</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>20</td>
                    <td>Grethel</td>
                    <td>gstroud8@cdbaby.com</td>
                    <td>Female</td>
                </tr>
                <tr>
                    <td>21</td>
                    <td>Letitia</td>
                    <td>lbreston9@sphinn.com</td>
                    <td>Female</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th>Id</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Gender</th>
                </tr>
            </tfoot>
        </table>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>

    <script>
        $(document).ready(function () {
            $('#userTable').DataTable({
                initComplete: function () {
                    this.api().columns().every(function (ind) {
                        var column = this;
                        if(ind != 0) {
                            var select = $('<select class="form-control custom-cls"><option value="">All</option></select>').appendTo($(column.footer()).empty()).on('change', function () {
                                var val = $.fn.dataTable.util.escapeRegex($(this).val());
                                column.search(val ? '^' + val + '$' : '', true, false).draw();
                            });
        
                            column.data().unique().sort().each(function (d, j) {
                                select.append('<option value="' + d + '">' + d + '</option>');
                            });
                        }
                    });
                },
            });
        });
    </script>
</body>

</html>
Live Example

Categories:

CSSDATATABLEHTMLjavascriptjQuery

Other Articles

How to Add an Extra Field in the Registration Form using Laravel?
Previous

How do Add an Extra Field in the Registration Form using Laravel?

How to Detect AdBlock using HTML CSS & JavaScript
Next

How to Detect AdBlock using HTML CSS & JavaScript

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Fix iOS Safari input type="file" Not Opening Camera - Real Solution

Fix iOS Safari input type=”file” Not Opening Camera – Real Solution

February 16, 2026
DataTables Requested unknown parameter Error – How to Debug Columns Properly

DataTables Requested unknown parameter Error – How to Debug Columns Properly

January 2, 2026
jQuery Validate Not Working After AJAX Loaded Form – Common Mistakes and Fix

jQuery Validate Not Working After AJAX Loaded Form – Common Mistakes and Fix

December 31, 2025
Jquery Validate Rules With if Condition

Jquery Validate Rules With if Condition

July 29, 2025
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement
Advertisement