dimanche 7 janvier 2018

How do I handle an array of checkboxes in ejs

I'm using ejs for my webpages and have one page where I have an array of objects. I don't know how many objects I have when the page loads. It's a page where I can edit a set of announcements. I'd like to use a checkbox to enable or disable each announcement, but need to set them initially from the incoming values. The page looks like this: enter image description here I'm using a foreach in the code for the page:

<% var i=0; %>
<% announcements.forEach(function(announcement) { %>
<tr>
    <td><input type="checkbox" id="enabled<%= i %>" name="enabled[<%= i %>]"></td>
    <td contenteditable="true"><%= announcement.text %></td>
    <td><%= type %></td>
    <td>
        <span class="table-remove glyphicon glyphicon-remove"></span>
    </td>
    <td>
        <span class="table-up glyphicon glyphicon-arrow-up"></span>
        <span class="table-down glyphicon glyphicon-arrow-down"></span>
    </td>
</tr>
<%      i++; %>
<% }) %>

How do I set the initial values of the checkboxes for each row? I have used the variable i to uniquely identify the checkboxes, but I don't know how to find the number of rows in the script portion.

Thanks!




Aucun commentaire:

Enregistrer un commentaire