如何从动态表中向弹簧启动控制器发送对象列表,该表通过按钮单击将项目添加到表中

发布于 2025-01-25 07:10:31 字数 2712 浏览 2 评论 0原文

在我的Spring Boot项目中,用户必须输入其银行详细信息(选择银行名称,分支名称,输入帐号)。还有一个表,在用户单击“添加银行”后,将使用jQuery添加一行。这些表行还隐藏了一些输入类型。实际上,我必须将输入隐藏值从表发送到控制器,并将值保存在数据库中。我尝试使用列表来做到这一点,但无济于事。我有其他方法可以实现这一目标吗?以及如何将表行绑定到jQuery中的控制器。 是我的jQuery和控制器代码

$('#Addbank').click(function() {
    alert($('#bank').children().length);
    if ($('#banktable').children().length == 0)
        $('#banktable').append("");
    $('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +"<input type='hidden' value="+$('#Bank').val() +">"+ "</td><td>" + $('#Branch option:selected').text() +
        "</td><td>" + $('#accno').val() +"<input type='hidden' value="+ $('#accno').val()+ "</td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
})

的一部分

appl_banks = new BankDto();
        for (int i = 0; i <= 50; i++) {
            appl_banks.addBank(new Appl_bank());
            System.out.println(appl_banks);
        }

控制器

<table class="table table-hover" id="banktable">
            <tr>
                <th>Bank Name</th>
                <th>Branch Name</th>
                <th>Account Number</th>
            </tr>
            <tbody>
                <tr th:each='b,item:${applbank}'>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].bankcd}'></td>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].branchcd}'></td>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].acno}'></td>
                </tr>
            </tbody>
        </table>

以下 私人列表&lt; appl_bank&gt; appl_banks;

public BankDto() {
    this.appl_banks=new ArrayList<>();
    // TODO Auto-generated constructor stub
}

public BankDto(List<Appl_bank> appl_banks) {
    super();
    this.appl_banks = appl_banks;
}

public List<Appl_bank> getAppl_banks() {
    return appl_banks;
}

public void setAppl_banks(List<Appl_bank> appl_banks) {
    this.appl_banks = appl_banks;
}
public void addBank(Appl_bank appl_bank)
{
    this.appl_banks.add(appl_bank);
}

}

银行详细信息表银行详细信息表应将其提交给控制器

桌子和来源

In my spring boot project there is a part where the user has to enter his bank details(select bank name, branch name , enter account number). There is also a table where after the user clicks on "Add Bank" a row will be added to a table using jquery. These table rows also have some input type hidden on them. I actually have to send the input hidden values from the table to the controller and save the values in the database. I have tried using lists to do it but to no avail. Is there some other way I can achieve this. And also how do I bind the table rows to the Controller in Jquery. Below is my jquery and controller codes

$('#Addbank').click(function() {
    alert($('#bank').children().length);
    if ($('#banktable').children().length == 0)
        $('#banktable').append("");
    $('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +"<input type='hidden' value="+$('#Bank').val() +">"+ "</td><td>" + $('#Branch option:selected').text() +
        "</td><td>" + $('#accno').val() +"<input type='hidden' value="+ $('#accno').val()+ "</td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
})

The part of controller where I initialize a list before adding to a model

appl_banks = new BankDto();
        for (int i = 0; i <= 50; i++) {
            appl_banks.addBank(new Appl_bank());
            System.out.println(appl_banks);
        }

The table code in HTML

<table class="table table-hover" id="banktable">
            <tr>
                <th>Bank Name</th>
                <th>Branch Name</th>
                <th>Account Number</th>
            </tr>
            <tbody>
                <tr th:each='b,item:${applbank}'>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].bankcd}'></td>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].branchcd}'></td>
                    <td><input type='hidden'
                        th:value='*{applbank[__${item.index}__].acno}'></td>
                </tr>
            </tbody>
        </table>

My wrapper class

public class BankDto {
private List<Appl_bank> appl_banks;

public BankDto() {
    this.appl_banks=new ArrayList<>();
    // TODO Auto-generated constructor stub
}

public BankDto(List<Appl_bank> appl_banks) {
    super();
    this.appl_banks = appl_banks;
}

public List<Appl_bank> getAppl_banks() {
    return appl_banks;
}

public void setAppl_banks(List<Appl_bank> appl_banks) {
    this.appl_banks = appl_banks;
}
public void addBank(Appl_bank appl_bank)
{
    this.appl_banks.add(appl_bank);
}

}

Bank details table The bank details table which should be submitted to the controller

The table along with the sources

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-02-01 07:10:31

我已经通过将隐藏的输入字段提供相同的名称并通过控制器中的数组访问它们来解决此问题。

jQuery part(插入具有相同名称的隐藏输入字段)

$('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +
        "<input type='hidden' name='bankcd' value="
        + $('#Bank').val() + "></td><td>"
        + $('#Branch option:selected').text() +
        "<input type='hidden' name='branchcd' value="
        + $('#Branch').val() + "></td><td>"
        + $('#accno').val() +
        "<input type='hidden' value="
        + $('#accno').val() +
        " name='accno'></td><td>"
        + $('#actype option:selected').text() +
        "<input type='hidden' name='actype' value="
        + $('#actype ').val() + "></td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
    str = str + $('#Bank').val();
    $('#bankid').val(str);
})

如何访问控制器中的输入字段名称

@RequestParam(value = "bankcd") String bankcd[]

,希望帖子可以帮助遇到此错误的人:D

I had already managed to solve this by giving the hidden input fields the same name and accessing them as an array through in the Controller.

Jquery part(inserted hidden input fields with same name)

$('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +
        "<input type='hidden' name='bankcd' value="
        + $('#Bank').val() + "></td><td>"
        + $('#Branch option:selected').text() +
        "<input type='hidden' name='branchcd' value="
        + $('#Branch').val() + "></td><td>"
        + $('#accno').val() +
        "<input type='hidden' value="
        + $('#accno').val() +
        " name='accno'></td><td>"
        + $('#actype option:selected').text() +
        "<input type='hidden' name='actype' value="
        + $('#actype ').val() + "></td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
    str = str + $('#Bank').val();
    $('#bankid').val(str);
})

How to access input fields name in Controller

@RequestParam(value = "bankcd") String bankcd[]

Hoping post helps someone who's stuck in this error :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文