Create dynamic HTML tables with Bootstrap & jQuery for full CRUD operations. Enhance user experience with responsive designs and seamless interactions.
Transform Your Web Apps with Dynamic Tables and Smooth User Interactions
Ever looked at a slick website and thought "Man, I wish I could make something like that without spending hours coding"? Well, you're not alone! That's where HTML Tables with Bootstrap & jQuery come in handy. They let you create interactive data tables that look great and work smoothly across all devices.
In today's fast-paced online world, users expect websites to be quick and easy to use. An HTML table styled with Bootstrap makes your data look good on any screen size, while jQuery adds that interactive magic to make CRUD operations (Create, Read, Update, Delete) a breeze. Whether you're a seasoned pro or just starting out, mastering this combo can really level up your web dev skills.
So let's dive in and see how you can use Bootstrap and jQuery to create dynamic, responsive tables that handle full CRUD operations like a champ.
Before we get our hands dirty with code, let's quickly go over what CRUD operations are and why they're so important for web apps.
CRUD stands for:
Real-world examples:
These operations are the building blocks of interactive websites, letting users mess with content without having to reload the whole page. By using HTML tables to show data, Bootstrap to make it look good on any device, and jQuery to add interactivity, you can create a solid interface that handles CRUD operations smoothly.
Before we start coding, let's make sure you've got everything you need:
To include Bootstrap and jQuery, add these links to your HTML file's
head
section:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD Operations with Bootstrap & jQuery</title>
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
>
</head>
<body>
<!-- Your content goes here -->
<!-- jQuery -->
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
></script>
<!-- Bootstrap JS -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
></script>
</body>
</html>
This setup gives you access to Bootstrap's styling and jQuery's functionality throughout your project.
Tables are a straightforward way to show structured data. But making them look good on all devices can be tricky. Bootstrap makes this easier with its built-in classes.
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Dynamic Content Goes Here -->
</tbody>
</table>
table
: Base class for Bootstrap tables
table-striped
: Adds zebra-striping to rows
table-bordered
: Adds borders to all sides
table-dark
: Makes the header dark
To make sure the table looks good on all devices, wrap it in a
.table-responsive
container:
<div class="table-responsive">
<table class="table table-striped table-bordered">
<!-- Table Content -->
</table>
</div>
This adds horizontal scroll on smaller screens so the table doesn't break the layout.
<div class="container mt-5">
<h2>User Management</h2>
<div class="table-responsive">
<table class="table table-striped table-bordered" id="userTable">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Sample Data -->
<tr>
<td>1</td>
<td>Jane Doe</td>
<td>[email protected]</td>
<td>
<button class="btn btn-primary btn-sm edit-btn">Edit</button>
<button class="btn btn-danger btn-sm delete-btn">Delete</button>
</td>
</tr>
<!-- More Rows -->
</tbody>
</table>
</div>
</div>
You can add more styles to your table with Bootstrap classes. For example, adding
table-hover
will highlight rows when you mouse over them:
<table class="table table-striped table-bordered table-hover">
<!-- Table Content -->
</table>
Now that your table is set up, let's make it interactive. We'll use jQuery to handle CRUD operations so users can add, view, update, and delete data without reloading the page.
Goal: Let users add new entries to the table.
<button class="btn btn-success mb-3" id="addUserBtn">Add User</button>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addUserModalLabel">Add New User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="addUserForm">
<div class="mb-3">
<label for="userName" class="form-label">Name</label>
<input type="text" class="form-control" id="userName" required>
</div>
<div class="mb-3">
<label for="userEmail" class="form-label">Email</label>
<input type="email" class="form-control" id="userEmail" required>
</div>
<button type="submit" class="btn btn-primary">Add User</button>
</form>
</div>
</div>
</div>
</div>
$(document).ready(function(){
// Show Add User Modal
$('#addUserBtn').click(function(){
$('#addUserModal').modal('show');
});
// Handle Add User Form Submission
$('#addUserForm').submit(function(event){
event.preventDefault();
var name = $('#userName').val();
var email = $('#userEmail').val();
// Add new row to the table
var newRow = `
<tr>
<td>${$('#userTable tbody tr').length + 1}</td>
<td>${name}</td>
<td>${email}</td>
<td>
<button class="btn btn-primary btn-sm edit-btn">Edit</button>
<button class="btn btn-danger btn-sm delete-btn">Delete</button>
</td>
</tr>
`;
$('#userTable tbody').append(newRow);
// Hide the modal
$('#addUserModal').modal('hide');
// Reset the form
$('#addUserForm')[0].reset();
});
});
How it works:
Goal: Show data in an organized way so users can see existing entries.
<!-- View User Modal -->
<div class="modal fade" id="viewUserModal" tabindex="-1" aria-labelledby="viewUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="viewUserModalLabel">User Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p><strong>Name:</strong> <span id="viewName"></span></p>
<p><strong>Email:</strong> <span id="viewEmail"></span></p>
</div>
</div>
</div>
</div>
<button class="btn btn-info btn-sm view-btn">View</button>
// Handle View Button Click
$(document).on('click', '.view-btn', function(){
var currentRow = $(this).closest('tr');
var name = currentRow.find('td:eq(1)').text();
var email = currentRow.find('td:eq(2)').text();
$('#viewName').text(name);
$('#viewEmail').text(email);
$('#viewUserModal').modal('show');
});
How it works:
Goal: Let users change existing data entries.
<button class="btn btn-primary btn-sm edit-btn">Edit</button>
<!-- Edit User Modal -->
<div class="modal fade" id="editUserModal" tabindex="-1" aria-labelledby="editUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editUserModalLabel">Edit User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="editUserForm">
<input type="hidden" id="editUserId">
<div class="mb-3">
<label for="editUserName" class="form-label">Name</label>
<input type="text" class="form-control" id="editUserName" required>
</div>
<div class="mb-3">
<label for="editUserEmail" class="form-label">Email</label>
<input type="email" class="form-control" id="editUserEmail" required>
</div>
<button type="submit" class="btn btn-primary">Update User</button>
</form>
</div>
</div>
</div>
</div>
// Handle Edit Button Click
$(document).on('click', '.edit-btn', function(){
var currentRow = $(this).closest('tr');
var userId = currentRow.find('td:eq(0)').text();
var userName = currentRow.find('td:eq(1)').text();
var userEmail = currentRow.find('td:eq(2)').text();
$('#editUserId').val(userId);
$('#editUserName').val(userName);
$('#editUserEmail').val(userEmail);
$('#editUserModal').modal('show');
});
// Handle Edit User Form Submission
$('#editUserForm').submit(function(event){
event.preventDefault();
var userId = $('#editUserId').val();
var userName = $('#editUserName').val();
var userEmail = $('#editUserEmail').val();
// Find the row to update
$('#userTable tbody tr').each(function(){
var currentRow = $(this);
var currentId = currentRow.find('td:eq(0)').text();
if(currentId == userId){
currentRow.find('td:eq(1)').text(userName);
currentRow.find('td:eq(2)').text(userEmail);
}
});
// Hide the modal
$('#editUserModal').modal('hide');
});
How it works:
Goal: Let users remove data entries from the table.
<button class="btn btn-danger btn-sm delete-btn">Delete</button>
// Handle Delete Button Click
$(document).on('click', '.delete-btn', function(){
if(confirm("Are you sure you want to delete this user?")){
$(this).closest('tr').remove();
// Optionally, re-index the ID column
$('#userTable tbody tr').each(function(index){
$(this).find('td:eq(0)').text(index + 1);
});
}
});
How it works:
While the above setup handles CRUD operations on the front-end, real-world apps need to store data somewhere. Connecting to backend services makes sure data sticks around between sessions and users.
To do CRUD operations with a backend, you'll usually work with RESTful APIs. Here's a quick rundown:
JSON Server is an easy way to set up a fake REST API for testing.
npm install -g json-server
db.json
file:
{
"users": [
{ "id": 1, "name": "Jane Doe", "email": "[email protected]" }
]
}
json-server --watch db.json
Now you've got an API at http://localhost:3000/users .
With the backend ready, let's tweak the jQuery code to talk to the API:
// Handle Add User Form Submission with Backend
$('#addUserForm').submit(function(event){
event.preventDefault();
var name = $('#userName').val();
var email = $('#userEmail').val();
$.ajax({
url: 'http://localhost:3000/users',
method: 'POST',
data: { name: name, email: email },
success: function(data){
var newRow = `
<tr>
<td>${data.id}</td>
<td>${data.name}</td>
<td>${data.email}</td>
<td>
<button class="btn btn-info btn-sm view-btn">View</button>
<button class="btn btn-primary btn-sm edit-btn">Edit</button>
<button class="btn btn-danger btn-sm delete-btn">Delete</button>
</td>
</tr>
`;
$('#userTable tbody').append(newRow);
$('#addUserModal').modal('hide');
$('#addUserForm')[0].reset();
},
error: function(){
alert('Error adding user.');
}
});
});
How it works:
Tips:
A smooth user experience can make or break your web app. Here's how to make your CRUD tables even better for users.
Make sure data is correct and handle errors nicely.
HTML5 has some built-in validation:
<input type="email" class="form-control" id="userEmail" required>
For more complex checks, use jQuery:
if(!validateEmail(email)){
alert('Please enter a valid email address.');
return;
}
Always give feedback when things go wrong with AJAX:
<!-- Handle Add User Form Submission with Backend -->
$.ajax({
// ...
error: function(jqXHR, textStatus, errorThrown){
alert(`Error: ${textStatus}`);
}
});
Make your design easy to use.
Let users know when things happen or if there are errors.
alert('User added successfully!');
Or for a nicer look, use Bootstrap alerts:
$('#successAlert').show();
Show spinners while stuff is loading:
$('#loadingSpinner').show(); // To show
$('#loadingSpinner').hide(); // To hide
As your app grows, making sure it stays fast becomes super important. Here's how to keep your tables quick and responsive.
Changing the DOM a lot can slow things down. Optimize by:
var fragment = $(document.createDocumentFragment());
data.forEach(function(user){
var row = `<tr>...</tr>`;
fragment.append(row);
});
$('#userTable tbody').append(fragment);
var $tableBody = $('#userTable tbody');
$tableBody.append(newRow);
Take your CRUD tables to the next level with these advanced features.
Make it easier to navigate and find data.
Split data into manageable pages
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<!-- Page numbers -->
<li class="page-item"><a class="page-link" href="#">1</a></li>
<!-- More pages -->
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</nav>
// Example logic to show a specific number of rows per page
var rowsPerPage = 5;
var rows = $('#userTable tbody tr');
var rowCount = rows.length;
var pageCount = Math.ceil(rowCount / rowsPerPage);
// Show the first set of rows
rows.hide();
rows.slice(0, rowsPerPage).show();
// Handle pagination clicks
$('.page-link').click(function(e){
e.preventDefault();
var page = $(this).text();
var start = (page - 1) * rowsPerPage;
var end = start + rowsPerPage;
rows.hide().slice(start, end).show();
});
Let users sort data by clicking table headers
$('th').click(function(){
var table = $(this).parents('table').eq(0);
var rows = table.find('tbody tr').toArray().sort(comparer($(this).index()));
this.asc = !this.asc;
if (!this.asc){rows = rows.reverse();}
for (var i = 0; i < rows.length; i++){table.append(rows[i]);}
});
function comparer(index) {
return function(a, b) {
var valA = getCellValue(a, index), valB = getCellValue(b, index);
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB);
}
}
function getCellValue(row, index){ return $(row).children('td').eq(index).text(); }
Add a search bar to filter table data
<input class="form-control mb-3" id="searchInput" type="text" placeholder="Search...">
$('#searchInput').on('keyup', function(){
var value = $(this).val().toLowerCase();
$('#userTable tbody tr').filter(function(){
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
Let users edit table data right in the cells.
<td class="editable">Jane Doe</td>
// Make table cells editable on double-click
$(document).on('dblclick', '.editable', function(){
var currentCell = $(this);
var originalContent = currentCell.text();
var input = $('', {
type: 'text',
value: originalContent,
class: 'form-control'
});
currentCell.html(input);
input.focus();
// Handle input blur (loss of focus)
input.blur(function(){
var newContent = $(this).val();
currentCell.text(newContent);
// Optionally, send update to backend here
});
});
How it works:
Control who can do what based on user roles.
This should be handled by your backend and login system
Use jQuery to show or hide buttons based on the user's role
var userRole = 'admin'; // This should come from your authentication system
if(userRole === 'viewer'){
$('.edit-btn, .delete-btn').hide();
} else if(userRole === 'editor'){
$('.delete-btn').hide();
}
Make sure your backend also checks permissions to prevent unauthorized actions
Every developer runs into issues. Here are some common ones and how to fix them:
Problem: Clicking "Add User" doesn't show the modal.
data-bs-target
and
id
attributes match
Problem: "Edit" or "Delete" buttons on new rows don't work when clicked.
Use event delegation instead of binding directly to elements:
$(document).on('click', '.edit-btn', function(){
// Handler code
});
Problem: Added users vanish when you reload the page.
Problem: Table layout breaks on small screens.
.table-responsive
wrapper
Staying up-to-date helps keep your apps relevant and efficient.
While jQuery is still popular and easy to use, modern frameworks like React, Vue, and Angular offer more structured ways to build apps.
Think about what your project needs. If you think it'll grow and get complex, it might be worth learning a modern framework.
As projects grow, keeping code organized becomes super important.
Learning how to create dynamic, responsive HTML tables with Bootstrap and jQuery opens up a world of possibilities for your web apps. By mastering CRUD operations, you're giving your projects the tools they need to manage data and interact with users - which is super important for interactive websites.
From setting up your development environment to connecting with backend services and making things user-friendly, each step plays a big role in creating smooth-running applications. While newer frameworks keep popping up, the basic skills of HTML, Bootstrap, and jQuery are still really valuable, offering a simple and efficient way to build all kinds of projects.
Don't let your tables just sit there showing static data. Make them interactive, responsive, and functional in ways users will love. Dive into your next project knowing that with Bootstrap and jQuery, you've got all the CRUD operations at your fingertips.
Get Started Today! Try out the examples we've covered, tweak them to fit what you need, and watch your web apps transform into dynamic, user-friendly experiences.
Happy Coding!