简体   繁体   English

从列表中创建一个选择并将其放在表中

[英]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

AJAX - JS - 从 AJAX 响应创建表并放入<div></div><div id="text_translate"><p>在这个方法中,我以文本形式传递了这个动作的响应,但是我想使用表格格式,我该怎么做?</p><pre> function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('&lt;div id="loaderMaiorDemandante"&gt;&lt;/div&gt;'); $("#loader").show(); var jsonHide = $('#activeJsonHide').html(); if(jsonHide) { $.ajax({ url: 'search.action', // action to be perform type: 'POST', //type of posting the data data: { dataJson: jsonHide }, // data to set to Action Class dataType: 'text', success: function (html) { $("#loader").hide(); $('#showAdditiveasText').html(html); //How to pass a tablw to That DIV? }, }); } }</pre><p> 如何使用表格格式将此数据传递给 div: "$('#showAdditiveasText').html(html)"?</p><p> 例子:</p><pre> [ { "UserID": 1, "UserName": "rooter", "Password": "12345", "Country": "UK", "Email": "sac@gmail.com" }, { "UserID": 2, "UserName": "binu", "Password": "123", "Country": "uk", "Email": "Binu@gmail.com" }, { "UserID": 3, "UserName": "cal", "Password": "123", "Country": "uk", "Email": "cal@gmail.com" }, { "UserID": 4, "UserName": "nera", "Password": "1234", "Country": "uk", "Email": "nera@gmail.com" } ]</pre></div> - AJAX - JS - Create a table from a AJAX response and put inside a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数据库获取数据并将其放在选择列表中 - Get the data from database and put it in an select list 从数组创建选择列表 - Create select list from array AJAX - JS - 从 AJAX 响应创建表并放入<div></div><div id="text_translate"><p>在这个方法中,我以文本形式传递了这个动作的响应,但是我想使用表格格式,我该怎么做?</p><pre> function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('&lt;div id="loaderMaiorDemandante"&gt;&lt;/div&gt;'); $("#loader").show(); var jsonHide = $('#activeJsonHide').html(); if(jsonHide) { $.ajax({ url: 'search.action', // action to be perform type: 'POST', //type of posting the data data: { dataJson: jsonHide }, // data to set to Action Class dataType: 'text', success: function (html) { $("#loader").hide(); $('#showAdditiveasText').html(html); //How to pass a tablw to That DIV? }, }); } }</pre><p> 如何使用表格格式将此数据传递给 div: "$('#showAdditiveasText').html(html)"?</p><p> 例子:</p><pre> [ { "UserID": 1, "UserName": "rooter", "Password": "12345", "Country": "UK", "Email": "sac@gmail.com" }, { "UserID": 2, "UserName": "binu", "Password": "123", "Country": "uk", "Email": "Binu@gmail.com" }, { "UserID": 3, "UserName": "cal", "Password": "123", "Country": "uk", "Email": "cal@gmail.com" }, { "UserID": 4, "UserName": "nera", "Password": "1234", "Country": "uk", "Email": "nera@gmail.com" } ]</pre></div> - AJAX - JS - Create a table from a AJAX response and put inside a <div> 从选择列表创建自动链接 - Create automatic Links from Select List 使用列表 json 数据创建表 html 并添加过滤器选择下拉列表 - create table html with list json data and add filter select dropdown JavaScript问题:根据select列表创建包含数据的表 - JavaScript issue: To create a table with data based on select list 从废弃的Javascript表列表创建DataFrame - Create DataFrame from Scraped Javascript Table List 放 <select>在表单元格中使用D3 - Put <select> in table cell using D3 如何选择元素表单列表并将其放在文本框中 - how to select element form list and put it in textbox 如何从html表中选择行并获取第一个单元格的值并将其放在PHP Session上 - How select row from html table and get the value of the first cell and put it on PHP Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM