如何在动态表格中显示下拉列表?

时间:2014-04-01 11:28:19

标签: javascript html ruby-on-rails innerhtml

我有表格的代码

<fieldset class="tabular"><legend><%= l(:redmine_taskjuggler) %></legend>
<%= labelled_fields_for(@issue) do |f| %>

<div id="taskjuggler" class="attributes">
<div class="splitcontent">
<div class="splitcontentright">

<table id="ResTable" cellspacing="0" border="1" width="850">
<thead>
<tr>
<th width="450" align = center> TJ allocation <a href="#" onClick="javascript:confirm('<%= l(:tooltip_tj_allocates) %>')">(?)</a> </th>
<th width="200" align = center> TJ efficiency <a href="#" onClick="javascript:confirm('<%= l(:tooltip_tj_efficiency) %>')">(?)</a> </th>
<th width="200" align = center> TJ limits <a href="#" onClick="javascript:confirm('<%= l(:tooltip_tj_limits) %>')">(?)</a> </th>
</tr>
</thead>
<tbody>
<tr>
<% users_array = User.all.map { |user| [user.firstname + ' ' + user.lastname, user.login.sub('-','_') ] } %>
<td> <%= select(:issue,:tj_allocates,users_array) %> </td>
<td> <%= f.text_field :tj_efficiency %> </td>
<td> <%= f.text_field :tj_limits %> </td>
</tr>
    </tbody>
</table>

<a href="#" onclick="javascript:addRow('ResTable')"> Add string </a>

</div>
</div>
</div>

在同一页面脚本中动态添加行

 <script type="text/javascript">
 function addRow(id){
 var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
 var row = document.createElement("TR")
 var td1 = document.createElement("TD")
 var td2 = document.createElement("TD")
 var td3 = document.createElement("TD")

 td1.innerHTML = '<%= select(:issue,:tj_allocates,users_array) %>'
 td2.innerHTML = '<%= f.text_field :tj_efficiency %>'
 td3.innerHTML = '<%= f.text_field :tj_limits %>'

 row.appendChild(td1);
 row.appendChild(td2);
 row.appendChild(td3);

 tbody.appendChild(row);
 }
 </script>

<% end %>
</fieldset>

但它没有用,因为没有工作

 td1.innerHTML = '<%= select(:issue,:tj_allocates,users_array) %>'

我有错误

SyntaxError: unterminated string literal  
td1.innerHTML = '<select id="issue_tj_allocates" name="issue[tj_allocates]">
----------------^

我查看了html页面的代码,我看到我已经创建

td1.innerHTML = '<select id="issue_tj_allocates" name="issue[tj_allocates]"><option value="admin">Redmine Admin</option>
<option value=""> Anonymous</option>
<option value="namiko">Namiko Namiko</option>
<option value="kitsune">Kitsune Kitsune</option>
<option value="tanuki">Tanuki Tanuki</option>
<option value="ookami">Ookami Ookami</option>
<option value="kirin">Kirin Kirin</option></select>'

如何在此表中创建下拉?

1 个答案:

答案 0 :(得分:0)

您可以创建模板,然后像这样使用它: -

<script type="text/template" id="some_select">
  <%= select(:issue,:tj_allocates,users_array) %>
</script>

然后像这样引用这个模板

td1.innerHTML = document.getElementById('some_select').innerHTML;

希望这会有所帮助。