screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.14.0/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.14.0/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, countryName: "India", capital: "Delhi" }, { id: 2, countryName: "USA", capital: "Washington, D.C." }, { id: 3, countryName: "Japan", capital: "Tokyo" }, { id: 4, countryName: "Germany", capital: "Berlin" }, { id: 5, countryName: "Brazil", capital: "Brasília" }, { id: 6, countryName: "France", capital: "Paris" }, { id: 7, countryName: "Russia", capital: "Moscow" }, { id: 8, countryName: "Australia", capital: "Canberra" }, { id: 9, countryName: "Canada", capital: "Ottawa" }, { id: 10, countryName: "China", capital: "Beijing" }, { id: 11, countryName: "Mexico", capital: "Mexico City" }, { id: 12, countryName: "South Africa", capital: "Pretoria" }, { id: 13, countryName: "Italy", capital: "Rome" }, { id: 14, countryName: "Spain", capital: "Madrid" }, { id: 15, countryName: "Argentina", capital: "Buenos Aires" }, { id: 16, countryName: "South Korea", capital: "Seoul" }, { id: 17, countryName: "Egypt", capital: "Cairo" }, { id: 18, countryName: "Nigeria", capital: "Abuja" }, ]; const handleClick = (countryName, capital) => { // Perform the desired action when the row is clicked alert(`Clicked on row for ${countryName}, Capital: ${capital}`); }; function App() { return ( <div className="container"> <h3>React Js Make Table Row Clickable</h3> <table className="custom-table"> <thead> <tr> <th>S.no</th> <th>Country</th> <th>Capital</th> </tr> </thead> <tbody> {data.map((item) => ( <tr key={item.id} onClick={() => handleClick(item.countryName, item.capital)} > <td>{item.id}</td> <td>{item.countryName}</td> <td>{item.capital}</td> </tr> ))} </tbody> </table> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { margin: 0 auto; width: 500px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; display: flex; flex-direction: column; align-items: center; } h3 { color: #333; font-size: 24px; margin-bottom: 20px; } .custom-table { width: 100%; border-collapse: collapse; } .custom-table th, .custom-table td { padding: 10px; border: 1px solid #ccc; } .custom-table th { background-color: #f0f0f0; font-weight: bold; } .custom-table tbody tr { cursor: pointer; transition: background-color 0.3s ease; } .custom-table tbody tr:hover { background-color: #f5f5f5; } .custom-table td:first-child { font-weight: bold; } </style> </body> </html>