Sure! Below is a script that creates a website with a stylish interface as per your requirements.
The script includes:
- A function to generate a specified number of cells, each containing two forms: one for the product name and one for the product price.
- An option to set either a green check mark in a green circle or a red letter X in a red circle to the right of each cell.
- A button that saves all the table data in a
.txt file.
Here's the complete code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product Entry</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
#container {
width: 80%;
margin: 50px auto;
background-color: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#productTable {
width: 100%;
border-collapse: collapse;
}
#productTable th, #productTable td {
padding: 10px;
border: 1px solid #ddd;
}
#productTable th {
background-color: #f9f9f9;
text-align: left;
}
.status-cell {
text-align: center;
}
.status-icon {
cursor: pointer;
font-size: 20px;
user-select: none;
}
.checkmark {
color: green;
}
.cross {
color: red;
}
#generateBtn, #saveBtn {
padding: 10px 15px;
font-size: 16px;
margin: 10px 5px;
cursor: pointer;
}
#generateSection {
margin-bottom: 20px;
}
.input-field {
width: 90%;
padding: 5px;
}
</style>
</head>
<body>
<div id="container">
<div id="generateSection">
<label for="numProducts">Number of Products: </label>
<input type="number" id="numProducts" value="1" min="1" />
<button id="generateBtn">Generate</button>
</div>
<table id="productTable">
<thead>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<!-- Product rows will be generated here -->
</tbody>
</table>
<button id="saveBtn">Save Data</button>
</div>
<script>
const generateBtn = document.getElementById('generateBtn');
const saveBtn = document.getElementById('saveBtn');
const productTableBody = document.querySelector('#productTable tbody');
function generateProductRows() {
const numProducts = parseInt(document.getElementById('numProducts').value);
productTableBody.innerHTML = ''; // Clear existing rows
for(let i = 0; i < numProducts; i++) {
const row = document.createElement('tr');
// Product Name Cell
const nameCell = document.createElement('td');
const nameInput = document.createElement('input');
nameInput.type = 'text';
nameInput.className = 'input-field';
nameCell.appendChild(nameInput);
row.appendChild(nameCell);
// Product Price Cell
const priceCell = document.createElement('td');
const priceInput = document.createElement('input');
priceInput.type = 'number';
priceInput.className = 'input-field';
priceInput.step = '0.01';
priceCell.appendChild(priceInput);
row.appendChild(priceCell);
// Status Cell
const statusCell = document.createElement('td');
statusCell.className = 'status-cell';
const statusIcon = document.createElement('span');
statusIcon.className = 'status-icon checkmark';
statusIcon.textContent = '✔';
statusIcon.addEventListener('click', function() {
if(statusIcon.classList.contains('checkmark')) {
statusIcon.classList.remove('checkmark');
statusIcon.classList.add('cross');
statusIcon.style.color = 'red';
statusIcon.textContent = '✖';
} else {
statusIcon.classList.remove('cross');
statusIcon.classList.add('checkmark');
statusIcon.style.color = 'green';
statusIcon.textContent = '✔';
}
});
statusCell.appendChild(statusIcon);
row.appendChild(statusCell);
productTableBody.appendChild(row);
}
}
function saveData() {
let data = '';
const rows = productTableBody.querySelectorAll('tr');
rows.forEach((row, index) => {
const name = row.cells[0].querySelector('input').value;
const price = row.cells[1].querySelector('input').value;
const statusIcon = row.cells[2].querySelector('.status-icon');
const status = statusIcon.classList.contains('checkmark') ? 'Approved' : 'Rejected';
data += `Product ${index + 1}:\n`;
data += `Name: ${name}\n`;
data += `Price: ${price}\n`;
data += `Status: ${status}\n\n`;
});
const blob = new Blob([data], { type: 'text/plain' });
const anchor = document.createElement('a');
anchor.download = 'product_data.txt';
anchor.href = window.URL.createObjectURL(blob);
anchor.target ='_blank';
anchor.style.display = 'none'; // make it invisible
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
generateBtn.addEventListener('click', generateProductRows);
saveBtn.addEventListener('click', saveData);
</script>
</body>
</html>
Explanation:
Instructions to Use the Script:
-
Copy the Code:
- Copy the entire code above and paste it into a text editor.
- Save the file with an
.html extension (e.g., product_entry.html).
-
Open in a Browser:
- Open the saved
.html file in a modern web browser (e.g., Chrome, Firefox).
-
Generate Product Entries:
- Enter the number of products you want to add in the "Number of Products" input field.
- Click the "Generate" button to create the product entry rows.
-
Fill in Product Information:
- In each row, enter the product name and price.
- Click on the status icon to toggle between approved (green check mark) and rejected (red cross).
-
Save Data:
- Once all data is entered, click the "Save Data" button.
- A file named
product_data.txt will be downloaded containing all the entered information.
Notes:
- The script uses basic HTML, CSS, and JavaScript, and does not require any external libraries.
- Ensure that JavaScript is enabled in your browser for full functionality.
- The styling can be further customized by modifying the CSS section.
- The data is saved locally as a text file; for more advanced data handling or database storage, server-side scripting would be necessary.
Feel free to modify and enhance the script to better suit your specific needs!