简体   繁体   English

如何组合这些功能而不重复?

[英]How to combine these functions without repeating?

As all the 3 product functions has the same product list how can i combine these multiple functions into one so that i can avoid repeating myself here.由于所有 3 个产品功能都有相同的产品列表,我如何将这些多个功能组合成一个,这样我就可以避免在这里重复自己。

How to combine these three functions into one as all the functions has the product list only the currency are different?如何将这三个功能合二为一,因为所有功能都有产品列表,只是货币不同? Could someone please suggest me.有人可以建议我。 Thanks谢谢

 function ProductDataRenderer() { } ProductDataRenderer.prototype.render = function () { var nzd = '<table class="table table-striped">' +' <thead>' +' <tr><td colspan="3">Products (NZD)</td></tr>' +' <tr>' +' <td>Name</td>' +' <td>Price</td>' +' <td>Type</td>' +' </tr>' +' </thead>' + ' <tbody>'; var n = ProductDataConsolidator.get(); for (var i = 0; i < n.length; i++) { nzd += '<tr>' + '<td>' + n[i].name +'</td>' + '<td>' + n[i].price + '</td>' + '<td>' + n[i].type + '</td>' + '</tr>'; } nzd += '</tbody></table>'; document.getElementById("nzdProducts").innerHTML = nzd; var usd = '<table class="table table-striped">' + ' <thead>' + ' <tr><td colspan="3">Products (USD)</td></tr>' + ' <tr>' + ' <td>Name</td>' + ' <td>Price</td>' + ' <td>Type</td>' + ' </tr>' + ' </thead>' + ' <tbody>'; var u = ProductDataConsolidator.getInUSDollars(); for (var i = 0; i < u.length; i++) { usd += '<tr>' + '<td>' + u[i].name + '</td>' + '<td>' + u[i].price + '</td>' + '<td>' + u[i].type + '</td>' + '</tr>'; } usd += '</tbody></table>'; document.getElementById("usdProducts").innerHTML = usd; var euro = '<table class="table table-striped">' + ' <thead>' + ' <tr><td colspan="3">Products (Euro)</td></tr>' + ' <tr>' + ' <td>Name</td>' + ' <td>Price</td>' + ' <td>Type</td>' + ' </tr>' + ' </thead>' + ' <tbody>'; var e = ProductDataConsolidator.getInEuros(); for (var i = 0; i < e.length; i++) { euro += '<tr>' + '<td>' + e[i].name + '</td>' + '<td>' + e[i].price + '</td>' + '<td>' + e[i].type + '</td>' + '</tr>'; } euro += '</tbody></table>'; document.getElementById("euProducts").innerHTML = euro; }

Hey this one should do the trick嘿,这个应该可以解决问题

const renderTable = ({ items, title, containerId }) => {
  let tableTemplate =
    '<table class="table table-striped">' +
    '  <thead>' +
    `      <tr><td colspan="3">${title}</td></tr>` +
    '      <tr>' +
    '          <td>Name</td>' +
    '          <td>Price</td>' +
    '          <td>Type</td>' +
    '      </tr>' +
    '  </thead>' +
    ' <tbody>'

  for (let i = 0; i < items.length; i++) {
    tableTemplate +=
      '<tr>' +
      `<td>${items[i].name}</td>` +
      `<td>${items[i].price}</td>` +
      `<td>${items[i].type}</td>` +
      `</tr>`
  }

  tableTemplate += '</tbody></table>'
  document.getElementById(containerId).innerHTML = tableTemplate
}

renderTable({
  items: ProductDataConsolidator.get(),
  title: 'Products (NZD)',
  containerId: 'nzdProducts'
})
renderTable({
  items: ProductDataConsolidator.getInUSDollars(),
  title: 'Products (USD)',
  containerId: 'usdProducts'
})
renderTable({
  items: ProductDataConsolidator.getInEuros(),
  title: 'Products (Euro)',
  containerId: 'euProducts'
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM