JQuery Table


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JQuery Table</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        *{
            margin: 0;
            padding: 0;
            font-family: sans-serif;
        }
        .header{
            width: 100%;
            height: 75px;
            background: #0087FF;
            border-bottom: 1px solid #333;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }
        button{
            border: none;
            border-radius: 4px;
            width: 150px;
            height: 30px;
            background: #fff;
            outline: none;
            font-size: 13px;
        }
        input{
            border: none;
            border-radius: 4px;
            width: 150px;
            height: 30px;
            background: #fff;
            outline: none;
            font-size: 13px;
            text-align: center;
        }
        td{
            text-align: center;
        }
        .check{
            width: 15px;
            height: 15px;
        }
    </style>
</head>
<body>
    <div class="header">
        <input id="myInput" type="text" placeholder="Search.. ">
        <input id="InputRows" type="text" placeholder="Enter Rows">
        <button  id="addrows" type="button">Add Rows</button>
        <button  id="addbtn" type="button">Add Only One</button>
    </div>
    <div class="container pt-4">
        <!-- style="text-align:center" -->
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th class="text-center">SN.</th>
                        <th class="text-center">Name</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
                <tbody id="tbody">

                </tbody>
            </table>
        </div>
    </div>
    <script >
        $(document).ready(function (){
            var rowIndex = 0
            $('#addbtn').on('click', function () {
                $('#tbody').append(`<tr id="R${++rowIndex}">
                    <td class="row-index text-center"><p>
                        ${rowIndex}
                        </p>
                        </td>
                        <td class="text-center" contenteditable="true">
                            Employee ${rowIndex}
                            </td>
                        <td class="text-center">
                            <input type="checkbox" class="form-check-input check"/>
                            </td>
                    </tr>`);
            });

            $("#tbody").on('click', '.check',function () {
                    $(this).closest('tr').css("color", "red");
                });
            // $("#tbody").on('dblclick', '.check',function () {
            //         $(this).closest('tr').css("color", "black");
            //     });
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#tbody tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
        
        });
    </script>
</body>
</html>
        $("#addrows").on("click", function() {
            var RowInput = document.getElementById('InputRows').value;
            let num = 0
            for (let i = 0; i < RowInput; i++) {
                $('#tbody').append(`<tr id="R${++rowIndex}">
                    <td class="row-index text-center"><p>
                        ${rowIndex}
                        </p>
                        </td>
                        <td class="text-center" contenteditable="true">
                            Employee ${rowIndex}
                            </td>
                        <td class="text-center">
                            <input type="checkbox" class="form-check-input check"/>
                            </td>
                    </tr>`);
            }

Full code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JQuery Table</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        *{
            margin: 0;
            padding: 0;
            font-family: sans-serif;
        }
        .header{
            width: 100%;
            height: 75px;
            background: #0087FF;
            border-bottom: 1px solid #333;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }
        button{
            border: none;
            border-radius: 4px;
            width: 150px;
            height: 30px;
            background: #fff;
            outline: none;
            font-size: 13px;
        }
        input{
            border: none;
            border-radius: 4px;
            width: 150px;
            height: 30px;
            background: #fff;
            outline: none;
            font-size: 13px;
            text-align: center;
        }
        td{
            text-align: center;
        }
        .check{
            width: 15px;
            height: 15px;
        }
    </style>
</head>
<body>
    <div class="header">
        <input id="myInput" type="text" placeholder="Search.. ">
        <input id="InputRows" type="text" placeholder="Enter Rows">
        <button  id="addrows" type="button">Add Rows</button>
        <button  id="addbtn" type="button">Add Only One</button>
    </div>
    <div class="container pt-4">
        <!-- style="text-align:center" -->
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th class="text-center">SN.</th>
                        <th class="text-center">Name</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
                <tbody id="tbody">

                </tbody>
            </table>
        </div>
    </div>
    <script >
        $(document).ready(function (){
            var rowIndex = 0
            $('#addbtn').on('click', function () {
                $('#tbody').append(`<tr id="R${++rowIndex}">
                    <td class="row-index text-center"><p>
                        ${rowIndex}
                        </p>
                        </td>
                        <td class="text-center" contenteditable="true">
                            Employee ${rowIndex}
                            </td>
                        <td class="text-center">
                            <input type="checkbox" class="form-check-input check"/>
                            </td>
                    </tr>`);
            });

            $("#tbody").on('click', '.check',function () {
                    $(this).closest('tr').css("color", "red");
                });
            // $("#tbody").on('dblclick', '.check',function () {
            //         $(this).closest('tr').css("color", "black");
            //     });
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#tbody tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });

        $("#addrows").on("click", function() {
            var RowInput = document.getElementById('InputRows').value;
            let num = 0
            for (let i = 0; i < RowInput; i++) {
                $('#tbody').append(`<tr id="R${++rowIndex}">
                    <td class="row-index text-center"><p>
                        ${rowIndex}
                        </p>
                        </td>
                        <td class="text-center" contenteditable="true">
                            Employee ${rowIndex}
                            </td>
                        <td class="text-center">
                            <input type="checkbox" class="form-check-input check"/>
                            </td>
                    </tr>`);
            }
        });
        });
    </script>
</body>
</html>

Post a Comment

Previous Post Next Post