Write a script to create a web application with a stylish interface design and adaptation to any mobile device:
Create a function that will allow you to generate an Excel table with a specified number of rows and columns in which you can write text. To the right of each row and column there should be a button inside which you can set either a green check mark or a red letter X.
Create a button that will allow you to save all Excel table data in formats such as *.txt and *.png
Create a function that will allow you to select a gradient background of two colors and fill the entire area of the site with them.
Assistant
Sure! Below is a complete script for a web application that fulfills your requirements. This application includes:
A function to generate an Excel-like table with a specified number of rows and columns, with editable text cells. Each row and column has a toggle button to set a green check mark or a red letter X.
Buttons to save the table data as a .txt file and as a .png image.
A function to select a gradient background of two colors and fill the entire area of the site.
The interface is designed to be stylish and responsive, adapting to any mobile device.
HTML: Two <input type="color"> elements allow the user to choose two colors.
JavaScript: When the "Apply Gradient Background" button is clicked, an event listener captures the selected colors and applies a CSS linear gradient to the <body> element.
Table Generator:
HTML: Input fields for the user to specify the number of rows and columns, and a "Generate Table" button.
JavaScript:
The event listener on the "Generate Table" button reads the number of rows and columns.
It then constructs a table dynamically using DOM manipulation:
Creates table headers (<th>) for columns with editable content and a toggle button.
Creates rows with editable cells and a toggle button for each row header.
Editable Cells and Headers:
Editable Content: Cells and headers are made editable by setting contentEditable = true.
Toggle Buttons: Each header has a toggle button created by the createToggleButton() function.
Toggle Buttons Functionality:
CreateToggleButton Function:
Initializes the button with a green check mark (Unicode character ✔) and green color.
Adds a click event listener to toggle between a green check mark and a red letter X (Unicode character ✘), changing the color accordingly.
Saving as TXT File:
JavaScript:
When the "Save as TXT" button is clicked, the script gathers the text from all editable cells and headers.
The text is formatted as tab-separated values (TSV), suitable for opening in Excel or any text editor.
The data is saved using a Blob and a temporary anchor element that triggers the download.
Saving as PNG Image:
JavaScript:
Uses the html2canvas library to capture the .table-container element as a canvas.
How to Use the Application
Setup:
Copy and paste the HTML and CSS code into an .html file (e.g., index.html).
Ensure that the JavaScript code is placed within the <script> tags in your HTML file or linked externally.
Running the Application:
Open the .html file in any modern web browser (e.g., Chrome, Firefox, Edge).
Selecting Gradient Background:
Use the color pickers to choose your desired colors.
Click "Apply Gradient Background" to change the site's background.
Generating the Table:
Input the number of rows and columns you want.
Click "Generate Table" to create the Excel-like table.
The table headers and cells are editable; click on them to enter text.
Using Toggle Buttons:
Each row and column header has a button:
Click to toggle between a green check mark and a red letter X.
The button's state can represent status indicators as per your needs.
Saving the Table Data:
As TXT:
Click "Save as TXT" to download the table data as a text file.
The text file contains tab-separated values of your table, suitable for importing into spreadsheet applications.
As PNG:
Click "Save as PNG" to download an image of the table.
The image captures the current state of the table, including text and toggle button symbols.
Responsive Design:
The application adjusts to different screen sizes.
On mobile devices, input fields and buttons stack vertically for better accessibility.
Additional Notes
Customization:
You can customize the styling by modifying the CSS within the <style> tags.
Add your own classes or adjust the existing ones to change fonts, colors, and layout.
Data Persistence:
Currently, the data entered is not saved when the page is refreshed.
For persistent storage, consider integrating with a backend or using browser local storage.
Security Considerations:
Since the application uses contentEditable, be cautious if integrating into a larger application to avoid potential security risks like XSS attacks.
Enhancements:
You may add features such as data import/export in other formats (e.g., CSV, Excel).
Implement sorting or filtering functionalities within the table.
This script provides a complete solution for your requirements, creating a stylish, responsive web application that works across all devices. Feel free to modify and expand upon this foundation to suit any additional needs you may have.