[英]Create a select from list and put it in table
I'm writing a web application using Java and Spring. 我正在使用Java和Spring编写Web应用程序。
From the controller, I send to the view a list called materials
从控制器,我向视图发送一个称为materials
的列表
@RequestMapping(value = "/advanced")
public String advancedCalculation(Model model) {
model.addAttribute("materials", materialService.getMaterials());
model.addAttribute("calcForm", new CalculationForm());
return "advanced";
}
materials
is a list of objects called MaterialDTO
: materials
是称为MaterialDTO
的对象的列表:
public class MaterialDTO extends DTO {
/* Constructors */
public MaterialDTO() { super(); }
public MaterialDTO(Integer id, String name) { super(id, name); }
}
From the client side I have to make tables with buttons to add rows to such tables. 从客户端,我必须制作带有按钮的表以向这些表添加行。 Something like: 就像是:
<table id="1">
<thead>
... 2 columns ...
</thead>
<tbody>
<tr>
<td>HERE I NEED THE SELECTOR</td>
<td>column 2</td>
</tr>
</tbody>
</table>
.
.
.
<table id=N>
<thead>
... M columns ...
</thead>
<tbody>
<tr>
<td>HERE I NEED THE SELECTOR</td>
<td>column 2</td>
...
<td>column M</td>
</tr>
</tbody>
</table>
That selector is the same for all tables, so I wanted to create it from the materials
list once and use it where I need it. 所有表的选择器都相同,因此我想从materials
列表中一次创建它,并在需要的地方使用它。 With this last I'm having problems ... 最后,我遇到了问题...
/* Making select */
var materialsList = new Array();
var sel = $('<select>');
<c:forEach items="${materials}" var="material">
var material = new Object();
material.id = '${material.id}';
material.name = '${material.name}';
materialsList.push(material);
</c:forEach>
$(materialsList).each(function() {
sel.append($('<option>').attr('value', this.id).text(this.name));
});
That's when I try to add it as HTML and it fails 那是我尝试将其添加为HTML时失败的原因
function add_to_table_1() {
$('#1')
.append('<tr><td>' + sel.html() + '</td><td><input type="text" id="e"></input></td><td><input type="text" id="S"></input></td><td><input type="itext" id="r"></input></td><td><button onclick="delete_row(this)">Borrar</button></td></tr>');
}
And instead of the selector appears "[object Object]". 而不是选择器出现“ [object Object]”。 How can I fix this? 我怎样才能解决这个问题?
Try this: 尝试这个:
$(sel).wrap('<div></div>').parent().html();
This will fix your select not displaying with your options. 这将解决您的选择不与选项一起显示的问题。 Remember .html() only returns innerHTML. 请记住,.html()仅返回innerHTML。 The outer html, <select></select>
, will not display with it. 外部html <select></select>
不会与其一起显示。 You have to wrap first. 您必须先包装。
Alternative: 替代方案:
$(sel)[0].outerHTML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.