PHP Classes

File: assets/js/card/CardComponent.js

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Simple SPA   assets/js/card/CardComponent.js   Download  
File: assets/js/card/CardComponent.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Simple SPA
Demonstrates single page applications
Author: By
Last change:
Date: 17 days ago
Size: 1,265 bytes
 

Contents

Class file image Download
import Component from '../component/Component.js'; class CardComponent extends Component { constructor(data, clickCallback) { super(); this.data = data; this.clickCallback = clickCallback; } render() { return this.data.map(post => ` <div class="w3-cell-row"> <div class="w3-cell" data-id="${post.id}" style="cursor: pointer;"> <h5 class="card-title">${post.title}</h5> <img src="${post.image}" alt="${post.title}" class="card-img-top"> <div class="card-body"> <p class="card-text">${this.truncateContent(post.content)}</p> </div> </div> </div><hr> `).join(''); } truncateContent(content) { return content.length > 100 ? content.substring(0, 100) + '...' : content; } afterRender() { this.data.forEach(post => { const card = document.querySelector(`.w3-cell[data-id="${post.id}"]`); if (card) { card.addEventListener('click', () => this.clickCallback(post)); } else { console.error(`Card with id ${post.id} not found`); } }); } } export default CardComponent;