grails - 如何将 HTML 表格数据从 View 传递到 grails 中的 Controller ?

标签 grails groovy

我对grails很陌生。我正在为我的项目创建一个 SMS 发送模块。在这里,我从用户那里获得了一些联系号码,并将其显示在动态创建的“TD”值中。现在我想将此“TD”值作为列表传递给我的 Controller 。

def controller(){ 

def var = params.name

}

在这里不起作用,因为它不是字段值。

最佳答案

将所有表 td 放入数组逗号分隔的联系人号码中。

在下面的代码中添加了两种方式。首先,联系电话是最后一列。第二个,所有 td 值都有类名。

// First way

var contactNumbers= []
$("#example tbody tr").each(function() {
  contactNumbers.push($(this).find("td:last-child").html());
});
console.log("If Contact number column is on last"+contactNumbers.join())


// Second way

var contactNumbers1= []
$(".cnTd").each(function(i){
    contactNumbers1.push($(this).text())
})
console.log("Contact numbers by class"+contactNumbers1.join())
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
          
  <table class="table" id="example">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
        <th>Contact Number</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
        <td class="cnTd">5675677567</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
        <td>90987786756</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
        <td>3455657457</td>
      </tr>
    </tbody>
  </table>
</div>


现在,您可以使用隐藏的表单值或发送 ajax 请求或查询参数将收集到的值发送到 Controller 。在 Controller 端只需使用 split(',')tokenize(',')获取数字列表的方法。

希望这会帮助你。

关于grails - 如何将 HTML 表格数据从 View 传递到 grails 中的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58946950/

相关文章:

grails - 如何在Grails 3的编辑 View 中修改ID列的值?

spring - Grails + Spring 安全核心 : How to assign a role after user has logged in?

java - 如何将 JUnit 测试类中的一个方法的回滚设置为 false?

grails - 每一格

grails - 从域对象调用 Grails 服务是不是很糟糕?

grails - 获取HQL中的两个集合的交集

spring - 在grails.test.GrailsMock上设置属性

grails - groovy/grails - 域对象和工具提示

sql - Groovy Sql 从列中获取最大值

groovy - 在 Groovy 的 file.eachFileMatch() 中访问模式变量的 ant 属性值