screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; const data = [ { id: 1, name: 'Kasab Ali', date: '2023-09-14' }, { id: 2, name: 'Johnson', date: '2023-09-10' }, { id: 3, name: 'Emily', date: '2023-09-12' }, { id: 4, name: 'Michael', date: '2023-09-11' }, { id: 5, name: 'Sophia', date: '2023-09-09' }, { id: 6, name: 'Liam', date: '2022-09-08' }, { id: 7, name: 'Olivia', date: '2021-09-13' }, { id: 8, name: 'William', date: '2023-09-07' }, { id: 9, name: 'Emma', date: '2020-09-06' }, { id: 10, name: 'James', date: '2023-09-05' }, // Add more data items here ]; function App() { const [sortedData, setSortedData] = useState(data); const [ascending, setAscending] = useState(true); const sortByDate = () => { const sorted = [...sortedData].sort((a, b) => { const dateA = new Date(a.date); const dateB = new Date(b.date); return ascending ? dateA - dateB : dateB - dateA; }); setSortedData(sorted); setAscending(!ascending); // Toggle sorting order }; return ( <div className='container'> <h1>React js Table Sort By Date</h1> <button className="sort-button" onClick={sortByDate}> {ascending ? 'Sort by Date (Ascending)' : 'Sort by Date (Descending)'} </button> <table className="data-table"> <thead> <tr className="table-header"> <th className="table-cell">ID</th> <th className="table-cell">Name</th> <th className="table-cell">Date</th> </tr> </thead> <tbody> {sortedData.map((item) => ( <tr className="table-row" key={item.id}> <td className="table-cell">{item.id}</td> <td className="table-cell">{item.name}</td> <td className="table-cell">{item.date}</td> </tr> ))} </tbody> </table> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> /* Container Styles */ .container { margin: 20px; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-color: #fff; } /* Sort Button Styles */ .sort-button { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; } .sort-button:hover { background-color: #0056b3; } /* Data Table Styles */ .data-table { width: 100%; border-collapse: collapse; margin-top: 20px; } /* Table Header Styles */ .table-header { background-color: #f0f0f0; font-weight: bold; } /* Table Cell Styles */ .table-cell { padding: 10px; border: 1px solid #ccc; text-align: center; } /* Table Row Styles */ .table-row:nth-child(even) { background-color: #f2f2f2; } /* Hover effect on table rows */ .table-row:hover { background-color: #e0e0e0; } </style> </body> </html>