Affichage des articles dont le libellé est html table. Afficher tous les articles
Affichage des articles dont le libellé est html table. Afficher tous les articles

JavaScript - Drag and Drop Table Cells Content

How to Create a Drag and Drop Table with CSS and JavaScript


How to Create a Drag and Drop Table with CSS and JavaScript


In this Javascript Tutorial, we will see how to create an interactive table where users can rearrange content (both text and images) with a simple drag and drop.




Project Source Code:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Mixed Content Table</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&
display=swap" rel="stylesheet">
<style>

:root{
--primary-color:#4a90ea;
--secondary-color:#f5f7fa;
--text-color:#333;
--border-color:#eaeaea;
--hover-color:#ecf0f1;
--shadow-color:rgba(0,0,0,0.1);
--gradient-bg:linear-gradient(135deg, #6dd5ed 0%, #2193b0 100%);
}

*{ box-sizing: border-box; margin: 0; padding: 0; }

body{
font-family: 'Poppins', sans-serif;
background-color: var(--secondary-color);
color: var(--text-color); display: flex;
justify-content: center; align-items: center;
min-height: 100vh; padding: 20px;
}

.table-container{
background-color: #fff; border-radius: 20px;
box-shadow: 12px 12px 24px var(--shadow-color),
-12px -12px 24px rgba(255,255,255,0.6);
overflow: hidden; transition: all 0.3s ease;
max-width: 900px; width: 100%; padding: 20px;
}

.table-container:hover{
box-shadow: 15px 15px 30px rgba(0,0,0,0.15),
-15px -15px 30px rgba(255,255,255,0.75);
}

table{ border-collapse: separate; border-spacing: 0;
width: 100%; border-radius: 12px; overflow: hidden;
}

th, td{ padding: 16px 24px; text-align: center; transition: all 0.3s ease; }

th{ background: var(--gradient-bg); color: white;
font-weight: 600; text-transform: uppercase;
letter-spacing: 1px; font-size: 14px;
}

tbody tr:last-child td{ border-bottom: none; }

td:hover{ background-color: var(--hover-color); }

.dragging{ opacity: 0.7; transform: scale(0.98); }

.drag-over{ background-color: var(--hover-color) !important;
transform: scale(1.05);
}

.drag-ghost{background-color: var(--primary-color);
color: white; border-radius: 4px;
padding: 8px 12px; font-size: 14px; position: fixed;
pointer-events: none; z-index: 1000; opacity: 0.9;
box-shadow: 0 4px 10px var(--shadow-color);
}



img{ max-width: 100px; max-height: 100px; object-fit: cover;
border-radius: 8px; transition: all 0.3s ease;
}

img:hover{ transform: scale(1.1);
box-shadow: 0 4px 8px var(--shadow-color);
}


/* Highlight animation for dropped items */
.highlight{
animation: highlight 1s ease-in-out;
}

@keyframes highlight{
0%{ background: var(--primary-color); color:white; }
100%{ background: transparent; color:var(--text-color); }
}


</style>
</head>
<body>

<div class="table-container">
<table id="myTable">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td draggable="true"><img src="images/avatar7.png"
alt="image1" draggable="false"></td>
<td draggable="true">Text Cell 1</td>
<td draggable="true"><img src="images/avatar2.png"
alt="image2" draggable="false"></td>
</tr>
<tr>
<td draggable="true">Text Cell 2</td>
<td draggable="true"><img src="images/avatar3.png"
alt="image3" draggable="false"></td>
<td draggable="true">Text Cell 3</td>
</tr>
<tr>
<td draggable="true"><img src="images/avatar4.png"
alt="image4" draggable="false"></td>
<td draggable="true">Text Cell 4</td>
<td draggable="true"><img src="images/avatar5.png"
alt="image5" draggable="false"></td>
</tr>
</tbody>
</table>
</div>
<script>
const table = document.getElementById('myTable');
let draggedCell = null;
let dragGhost = null;

// Event listener for drag start
table.addEventListener('dragstart', (e) => {
if(e.target.tagName !== 'TD') return;

draggedCell = e.target;
e.target.classList.add('dragging');

// Create a ghost element for dragging
dragGhost = document.createElement('div');
dragGhost.className = 'drag-ghost';

// Check if the dragged cell contains an image or text
const img = draggedCell.querySelector('img');
if(img){
dragGhost.innerHTML = `<img src="${img.src}"
alt="${img.alt}" style="width:50px; object-fit:cover;">`;
}
else{ dragGhost.textContent = draggedCell.textContent; }

document.body.appendChild(dragGhost);
e.dataTransfer.setDragImage(dragGhost, 25, 25);

setTimeout(() => { e.target.style.opacity = '0.5';}
, 0 );

});

// Event listener for drag over
table.addEventListener('dragover', (e) => {
e.preventDefault();
if(dragGhost){
dragGhost.style.left = e.clientX + 15 + 'px';
dragGhost.style.top = e.clientY + 15 + 'px';
}
});


// Event listener for drag enter
table.addEventListener('dragenter', (e) => {
e.preventDefault();
if(e.target.tagName === 'TD'){
e.target.classList.add('drag-over');
}
});


// Event listener for drag leave
table.addEventListener('dragleave', (e) => {
e.preventDefault();
if(e.target.tagName === 'TD'){
e.target.classList.remove('drag-over');
}
});


// Event listener for drop
table.addEventListener('drop', (e) => {
e.preventDefault();
if(e.target.tagName === 'TD'){
// Swap the contents of the cells
const temp = e.target.innerHTML;
e.target.innerHTML = draggedCell.innerHTML;
draggedCell.innerHTML = temp;

// Remove drag-over class and add highlight effect
e.target.classList.remove('drag-over');
e.target.classList.add('highlight');
draggedCell.classList.add('highlight');

// Remove highlight class after animation
setTimeout(() => { e.target.classList.remove('highlight');
draggedCell.classList.remove('highlight');
}, 1000);
}
});


// Event listener for drag end
table.addEventListener('dragend', (e) => {

e.target.classList.remove('dragging');
e.target.style.opacity = '1';

table.querySelectorAll('td').forEach(cell => {
cell.classList.remove('drag-over');
});

if(dragGhost){
document.body.removeChild(dragGhost);
dragGhost = null;
}

});

</script>

</body>
</html>




OUTPUT:

Drag and Drop Table Cells Content In JavaScript

Drag and Drop Tables In JavaScript

JavaScript Drag and Drop Tables







JavaScript HTML Table Inline Edit and Remove Row

How to Create HTML Table with Column Edit and Remove Rows Using JavaScript


JavaScript HTML Table Inline Edit and Remove Row


In this Javascript tutorial, we will see how to use JavaScript to edit and remove html table row.
This code allows users to do an inline edit for table row, and delete row.
It uses Font Awesome icons for the edit, delete, and save actions and JavaScript to handle the interactivity.



Project Source Code:



<!DOCTYPE html>
<html>
<head>
<title> Table - Edit / Remove Row </title>
<!-- FontAwesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.14.0/css/all.css"
integrity="sha384-HzLeBuhoNPvSl5KYnjx0BT+WB0QEEqLprO+NBkkk5gbc67FTaL7XIGa2w1L0Xbgc"
crossorigin="anonymous">

<style>

*{box-sizing: border-box}

body{
margin: 0;
padding: 0;
font-family: sans-serif, Arial, Helvetica;
background-color: #f8f8f8;
}

table{
width: 100%;
border-collapse: collapse;
background-color: #fff;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}


table th, table td{
padding:12px 15px;
text-align: left;
border-bottom: 1px solid #e0e0e0;
}

table th{background-color: #1abc9c; color: #fff; text-transform: uppercase; }
table td{font-size: 14px;}

table tr:last-child td{border-bottom: none}

table tr:hover{background-color: #f4f4f4;}

.fa{font-size: 18px; margin-right: 10px;}

.fa-edit:hover,
.fa-trash:hover,
.fa-save:hover{
color: #2c3e50;
}

</style>


</head>
<body>
<table id="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>City</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>John 11</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
<tr>
<td>John 22</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
<tr>
<td>John 33</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
<tr>
<td>John 44</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
<tr>
<td>John 55</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
<tr>
<td>John 66</td>
<td>100</td>
<td>Male</td>
<td>Mars</td>
<td>
<i class="fas fa-edit"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-save" style="display: none"></i>
</td>
</tr>
</tbody>
</table>

<script>

const trashIcons = document.querySelectorAll(".fa-trash");
trashIcons.forEach(icon => {
icon.addEventListener("click", function(){
const row = this.parentNode.parentNode;
row.remove();
});
});


const editIcons = document.querySelectorAll(".fa-edit");
editIcons.forEach(icon => {
icon.addEventListener("click", function(){
// get the current row
const row = this.parentNode.parentNode;
// get the cells in the current row
const cells = row.querySelectorAll("td");
// loop through the cells and replace the text with input fields
cells.forEach((cell, index) => {
if(index !== cells.length-1)
{
const input = document.createElement("input");
input.value = cell.textContent;
cell.textContent = "";
cell.appendChild(input);
}
});
// hide the edit icon and show the save icon
this.style.display = "none";
const saveIcon = this.parentNode.querySelector(".fa-save");
saveIcon.style.display = "inline-block";

});
});


const saveIcons = document.querySelectorAll(".fa-save");
saveIcons.forEach(icon => {
icon.addEventListener("click", function(){
// get the current row
const row = this.parentNode.parentNode;
// get the cells in the current row
const cells = row.querySelectorAll("td");
// loop through the cells and replace the input fields with their values
cells.forEach((cell, index) => {
if(index !== cells.length - 1){
const input = cell.querySelector("input");
cell.textContent = input.value;
}
});
// hide the save icon and show the edit icon
this.style.display = "none";
const editIcon = this.parentNode.querySelector(".fa-edit");
editIcon.style.display = "inline-block";

});
});

</script>

</body>
</html>





Code Explanation:

this JavaScript code, allows users to edit and remove html table row using javascript with font awesome icons.

This JavaScript code performs the following actions:

1 - It first selects all elements with the class "fa-trash" (the delete icons) and adds a click event listener to each of them, and when a delete icon is clicked, the associated row is removed from the table..

2 - It adds click event listeners to all "fa-edit" icons, transforming rows into editable forms when clicked. Text in cells becomes input fields, edit icons are hidden, and save icons allow data to be saved.

3 - It attaches a click event listener to all elements with the "fa-save" class. When a save icon is clicked, it restores the row to its non-editable state, replacing input fields with their values, hides the save icon, and shows the edit icon.



OUTPUT:

How to Build a Table with Edit and Remove Functionality using JavaScript

Edit html table row using JavaScript

html table row edited using JavaScript

remove html table selected row using JavaScript







JavaScript - How to Filter Table Data Between Two Dates

How to Display Table Data Between Two Selected Dates Using JavaScript 


How to Filter HTML Table Rows by Date Range Using JavaScript



In this Javascript tutorial, we will see how to use JavaScript to filter html table rows based on the selected date range.
This code allows users to input a start and end date and then filters the table rows to display only the rows with birthdays falling within that date range. It provides a simple date-based search functionality for the table data.



Project Source Code:



<!DOCTYPE html>
<html>
<head>
<title>Search Between 2 Dates</title>

<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}

body{background-color: #f2f2f2;}

table{
border-collapse: collapse;
width: 100%;
max-width: 800px;
margin: 20px auto;
background-color: #fff;
border: 1px solid #ddd;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

table th{
background-color: #f2f2f2;
font-weight: bold;
text-align: left;
padding: 10px;
border: 1px solid #ddd;
}

table tr{ border: 1px solid #ddd; }

table td{ padding: 10px; border: 1px solid #ddd; }

#search-box{
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}

#start-date, #end-date{
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
margin-left: 5px;
margin-right: 10px;
}
#search-button{
padding: 10px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: #fff;
cursor: pointer;
}

</style>


</head>
<body>

<table>

<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>City</th>
<th>Birthday</th>
</tr>

<tr>
<td>John Doe</td>
<td>30</td>
<td>Male</td>
<td>New York</td>
<td>01/01/2021</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>25</td>
<td>Female</td>
<td>Los Angeles</td>
<td>02/01/2022</td>
</tr>
<tr>
<td>Bob Johnson</td>
<td>35</td>
<td>Male</td>
<td>Chicago</td>
<td>03/01/2020</td>
</tr>

</table>
<br>
<div id="search-box">
Start Date: <input type="date" id="start-date">
End Date: <input type="date" id="end-date">
<button id="search-button">Search</button>
</div>

<script>
const startDateInput = document.getElementById("start-date");
const endDateInput = document.getElementById("end-date");
const searchButton = document.getElementById("search-button");

searchButton.addEventListener("click", filterByDateRange);

function filterByDateRange(){
// get the start and end date values
const startDate = new Date(startDateInput.value);
startDate.setHours(0,0,0,0); // Set time to midnight
const endDate = new Date(endDateInput.value);
endDate.setHours(0,0,0,0); // Set time to midnight
// get all the rows in the table
const rows = document.querySelectorAll("table tr:not(:first-child)");

rows.forEach(row=>{
// get the birthday cell in the current row
const birthDayCell = row.querySelector("td:last-child");
// get the birthday value
const birthday = new Date(birthDayCell.textContent);
//Also, you should check that the selected element is not null
//before trying to access its textContent property
if(birthDayCell !== null){
const birthday = new Date(birthDayCell.textContent);
birthday.setHours(0,0,0,0); // Set time to midnight
}
// check if the birthday is within the date range
if(birthday >= startDate && birthday <= endDate)
{
row.style.display = "";
}
else{
row.style.display = "none";
}

});
}



</script>

</body>
</html>




Code Explanation:

this JavaScript code, allows users to dynamically filter a table based on their dates selections.

This JavaScript code performs the following actions:

1 - Captures references to HTML elements: startDateInput, endDateInput, and searchButton..

2 - Adds a "click" event listener to the "Search" button.

3 - Iterates through each row, obtaining the birthday date from the last cell.

4 - Compares the birthday date to the specified date range.

5 - Displays the row if the birthday date falls within the range.

6 - Hides the row if the birthday date is outside the range.


OUTPUT:

How to Search HTML Table Rows by Date Range In JavaScript