jquerysails.jssails-postgresql

SailsJS populate html table with each


I cant retrieve model data to a html select tag. Heres my code so far.

% _.each(users, function(user){ %>
<select data-id="<%= user.id %>" data-model="user">
<option value="<%= user.id%>"><%= user.name %></option>
<% }) %>

I get an error when trying to lift the server, maybe it happen because im making a syntax mistake


Solution

  • There seem to be a couple of issues here, depending what you are trying to do. You are creating a different <select> element for each user (each having only a single <option>. What you probably want is a single <select> with multiple <option> tags:

    <select id="userid" name="userid" data-model="user">
        <% _.each(users, function(user) { %>
            <option value="<%= user.id %>"><%= user.name %></option>
        <% }); %>
    </select>
    

    A couple of other sources of error are corrected: the initial templating tag is not missing its angle bracket, and the <select> has been given a name property (in case you are submitting this as part of a form), and an id property (in case you are getting the value using javascript).

    If you are submitting this with a form, then a userid property will be included with the value being the value of the selected <option>.