all.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <br>
  2. <div class="ui container">
  3. <h2 class="ui header">All Blog Posts</h2>
  4. <table class="ui celled table">
  5. <thead>
  6. <tr>
  7. <th>Post Name</th>
  8. <th>Edit</th>
  9. <th>Delete</th>
  10. </tr>
  11. </thead>
  12. <tbody id="postTableBody">
  13. <!-- Rows will be dynamically loaded here -->
  14. </tbody>
  15. </table>
  16. </div>
  17. <script>
  18. $(document).ready(function() {
  19. // Dummy function to fetch posts from an API
  20. function fetchPosts() {
  21. // Simulated API response
  22. const posts = [
  23. { id: 1, name: "First Blog Post" },
  24. { id: 2, name: "Second Blog Post" },
  25. { id: 3, name: "Third Blog Post" }
  26. ];
  27. // Clear the table body
  28. $('#postTableBody').empty();
  29. // Populate the table with posts
  30. posts.forEach(post => {
  31. $('#postTableBody').append(`
  32. <tr>
  33. <td>${post.name}</td>
  34. <td><button class="ui blue button edit-button" data-id="${post.id}">Edit</button></td>
  35. <td><button class="ui red button delete-button" data-id="${post.id}">Delete</button></td>
  36. </tr>
  37. `);
  38. });
  39. }
  40. // Call the fetchPosts function to load data
  41. fetchPosts();
  42. // Event listeners for Edit and Delete buttons
  43. $(document).on('click', '.edit-button', function() {
  44. const postId = $(this).data('id');
  45. alert(`Edit post with ID: ${postId}`);
  46. });
  47. $(document).on('click', '.delete-button', function() {
  48. const postId = $(this).data('id');
  49. alert(`Delete post with ID: ${postId}`);
  50. });
  51. });
  52. </script>