Contoh Nested Custom Element Di Javascript
DAFTAR ISI
Saat memakai custom element, mungkin terdapat kondisi di mana kita memerlukan custom element berada di dalam custom element lain. Misalnya, banyak website sekarang ini yang memperlihatkan data berupa daftar, entah itu daftar artikel maupun item belanja.
Pada umumnya setiap daftar yang ditampilkan ditampung pada sebuah container <div>. Kemudian item yang sama ditampilkan secara berulang dengan data yang berbeda pada container itu.
Contoh Nested Custom Element Di Javascript
Contoh Nested Custom Element Di Javascript
Web component bisa mempermudah dalam mengatur daftar item yang ditampilkan dalam wujud daftar memakai container. Caranya kita membuat dua custom element yatu container dan itemnya. Container dipakai untuk menampung elemen item di dalamnya. Disamping itu pada container juga data (array) diberikan. Nantinya container-lah yang akan membuat elemen item di dalamnya berdasarkan data yang diberikan.
Belum terbayang seperti apa? Berikut contohnya:
    • index.html
    1. <!DOCTYPE html>
    1. <html>
    1.  <head>
    1.    <meta charset=“utf-8”>
    1.    <meta name=“viewport” content=“width=device-width”>
    1.    <title>repl.it</title>
    1.    <link href=“style.css” rel=“stylesheet” type=“text/css” />
    1.  </head>
    1.  <body>
    1.    <script src=“script.js” type=“module”></script>
    1.  </body>
    2. </html>
    • script.js
    1. import “./article-list.js”;
    1. import articles from “./articles.js”;
    1.  const articleListElement = document.createElement(“article-list”);
    1. articleListElement.articles = articles;
  1.  document.body.appendChild(articleListElement);
    • article-list.js
    1. import “./article-item.js”
    1.  class ArticleList extends HTMLElement {
    1.  set articles(articles) {
    1.    this._articles = articles;
    1.    this.render();
    1.  }
    1.  render() {
    1.    this._articles.forEach(article => {
    1.      const articleItemElement = document.createElement(“article-item”);
    1.      // memanggil fungsi setter article() pada article-item.
    1.      articleItemElement.article = article;
    1.      this.appendChild(articleItemElement);
    1.    })
    1.  }
    1. } 
    2. customElements.define(“article-list”, ArticleList);
Baca Juga:  Cara Menyimpan Foto dari Google Images
    • article-item.js
    1. class ArticleItem extends HTMLElement {
    1.  set article(article) {
    1.    this._article = article;
    1.    this.render();
    1.  }
    1.  
    1.   render() {
    1.    this.innerHTML = `
    1.     <img class=”featured-image” src=”${this._article.featuredImage}”>
    1.     <div class=”article-info”>
    1.         <h2><a href=”${this._article.id}”>${this._article.title}</a></h2>
    1.         <p>${this._article.description}</p>
    1.     </div>
    1.   `;
    1.  }
    1. }
    1.  
  1.  customElements.define(“article-item”, ArticleItem);
      • articles.js

const articles = [

    1. {
    1.    id: 1,
    1.    title: “Lorem Ipsum Dolor”,
    1.    featuredImage: “https://i.picsum.photos/id/204/536/354.jpg”,
    1.    description: “Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.”  
    1.  },
    1.  {
    1.    id: 2,
    1.    title: “Lorem Ipsum Dolor”,
    1.    featuredImage: “https://i.picsum.photos/id/209/536/354.jpg”,
    1.    description: “Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.”  
    1.  },
    1.  {
    1.    id: 3,
    1.    title: “Lorem Ipsum Dolor”,
    1.    featuredImage: “https://i.picsum.photos/id/206/536/354.jpg”,
    1.    description: “Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.”
    1.  },
    1.  {
    1.    id: 4,
    1.    title: “Lorem Ipsum Dolor”,
    1.    featuredImage: “https://i.picsum.photos/id/212/536/354.jpg”,
    1.    description: “Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.”
    1.   }
    1. ] 
    1.  
  1. export default articles;
Baca Juga:  Inilah yang Harus Kamu Lakukan Bila Registrasi Kartu Prabayar Tidak sukses
  • style.css
    1. * {
    1.     padding: ;
    1.     margin: ;
    1.     boxsizing: borderbox;
    1.   }
    1.   body {
    1.     padding: 16px;
    1.   }
    1.   articledaftar {
    1.     display: block;
    1.     maxwidth: 800px;
    1.     margin: auto;
    1.   }
    2. articleitem {
    1.     display: block;
    1.     marginbottom: 18px;
    1.     boxshadow: 4px 8px rgba(, , , 0.2);
    1.     borderradius: 10px;
    1.     overflow: hidden;
    1.   }
    1.   articleitem > .featuredimage {
    1.     width: 100%;
    1.     maxheight: 300px;
    1.     objectfit: cover;
    1.     objectposition: center;
    1.   }
    1.   articleitem > .articleinfo {
    1.     padding: 24px;
    1.   }
    1.   articleitem > .articleinfo > p {
    1.     margintop: 10px;
    2.  }
Pada kode di atas kita dapat memandang bahwa terdapat dua buah custom component yaitu <article-list> dan <article-item>. Pada article-list.js terdapat fungsi setter articles yang berguna untuk menyimpan nilai articles pada properti this._articles.
    1. set articles(articles) {
    1.    this._articles = articles;
    1.    this.render();
    2. }
Kemudian properti itu dipakai pada fungsi render() untuk ditampilkan satu persatu melalui <article-item>.
    1. render() {
    1.    this._articles.forEach(article => {
    1.      const articleItemElement = document.createElement(“article-item”);
    1.      // memanggil fungsi setter article() pada article-item.
    1.      articleItemElement.article = article;
    1.      this.appendChild(articleItemElement);
    1.    })
    2. }
Dengan seperti itu, untuk menampilkan data pada script.js bakal lebih mudah. Kita tidak harus melakukan proses perulangan lagi di sana sebab proses itu langsung dilakukan saat memakai element <article-list>. Kita cukup memberikan nilai array yang akan ditampilkan.
    1. import “./article-list.js”;
    1. import articles from “./articles.js”;
    1. const articleListElement = document.createElement(“article-list”);
    1. articleListElement.articles = articles;
  1. document.body.appendChild(articleListElement);
Kian mudah kita memakai sebuah element maka akan kian baik bukan? Meskipun tampak agak sedikit menyusahkan dalam membuatnya, perlu Kamu ingat bahwa  web component ini bersifat reusable. Artinya, bila kita mau membuat komponen serupa, kita tak perlu membuatnya dari awal.
Dengan menjalankan kode di atas, maka hasilnya akan terlihat semacam ini:
Contoh Nested Custom Element Di Javascript
Contoh Nested Custom Element Di Javascript

Ebook Gratis!!

Subscribe untuk dapatkan e-book GRATIS dan informasi teknologi terbaru dan diskon menarik langsung di Email-mu

Programmer Indonesia
Programmer Indonesia
Admin yang mengelola konten khusus berita. Kalau ada yang ingin diinfokan langsung chat aja ya :D
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
WhatsApp chat