代码之家  ›  专栏  ›  技术社区  ›  joeyanthon

如何通过onclick将对象参数传递给控制器Spring-mvc

  •  0
  • joeyanthon  · 技术社区  · 8 年前

    我有一个用spring mvc填充的表,我想获取整个对象并通过参数传递给javascript函数: 示例:

      <tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td onclick="${prod}" >CLICK HERE</td>
      </tr>
    

    但这不起作用。

    1 回复  |  直到 8 年前
        1
  •  0
  •   amdg    8 年前

    如果您正在使用 jQuery ,然后有一个插件称为 tabletojson 它将返回包含所有表数据的等效json字符串。然后可以将该json传递给任何其他函数并轻松操作。我认为,这是最干净的方式。

    var table = $('#mytable').tableToJSON();
    

    否则,需要遍历所有列并将其推送到某个数组中

    var mytable = document.getElementById("mytable");
    for (var i = 0, myrow; myrow = table.rows[i]; i++) {
       //iterate through rows using "myrow"
       for (var j = 0, mycol; mycol = myrow.cells[j]; j++) {
         //iterate through all the columns using mycol
    
       }  
    }