在Node.js模块内部循环

时间:2019-05-08 19:24:22

标签: node.js

这是我的模块。

module.exports = function (requestUser) {  

  let content = `

    <div id="cpl">
      <table id="currentpatientslists">
      </table>
    </div>
    <div id="rp">
      <h1>Requested Patients</h1>
    </div>
    <hr>    
    <div id="note" >Currently no patients</div>
    <div id="rpl">
    <table id="requestedpatientslists">    
    <tr>
    <td width="30%"></td>
    <td width="30%" class="right"><button>Accept</button></td>
    <td width="30%" class="left"><button>Reject</button></td>
    </tr>
    </table>
    </div>`;

  return render(content);
}

在requested Patientslists表中,我想循环来自表中的requestUser的表行中的数据。我想循环直到requestUser.length。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您只需要遍历用户并首先为他们创建行

module.exports = function(requestUser) {
  // I'm guessing that the user has normal properties like name, etc?
  const tableRows = requestUser.map(
    user => `
    <tr>
      <td width="30%">${user.name}</td>
      <td width="30%" class="right"><button>Accept</button></td>
      <td width="30%" class="left"><button>Reject</button></td>
    </tr>
  `,
  );
  const content = `

    <div id="cpl">
      <table id="currentpatientslists">
      </table>
    </div>
    <div id="rp">
      <h1>Requested Patients</h1>
    </div>
    <hr>    
    <div id="note" >Currently no patients</div>
    <div id="rpl">
    <table id="requestedpatientslists">    
    ${tableRows.join('\n')}
    </table>
    </div>`;

  return render(content);
};