読者です 読者をやめる 読者になる 読者になる

submit button used in ForEach

Teeda Seasar2

ForEach内でボタンを使用して押されたインデックスを取得する方法を
teeda-html-exampleのtrunkにコミットしました(Rev:2765)

http://localhost:8080/teeda-html-example/view/foreach/forEachList.html


簡単に説明すると、indexをhiddenに埋め込んでボタンを押された時に
押されたindexをjavascriptで取得して、その値をsubmitします。

forEachList.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ForEach</title>
<script type="text/javascript">
function setIndex(name) {
  var hidden = name.substring(0, name.lastIndexOf(':'));
  var f = document.forEachListForm;
  f.clickIndex.value = f[hidden + ':aaaIndex-x'].value;
}
</script>
</head>
<body>
<form id="forEachListForm">
<input type="hidden" name="clickIndex"/>
<table border="1">
 <tr bgcolor="#7777FF">
  <th>Key</th>
  <th colspan="2">Test</th>
 </tr>
 <div id="aaaItems">
 <tr>
  <td><span id="key">dummy1</span></td>
  <td>
    <input type="button" id="doForEachResult" value="test" onclick="setIndex(this.name);"/>
    <input type="hidden" id="aaaIndex-x"/>
  </td>
 </tr>
 </div>
</table>
</form>
</body>
</html>

ForEachListPage.java

public class ForEachListPage {

  private ForEachDto[] aaaItems;

  private int aaaIndex;

  private int clickIndex;

  private String key;

  public ForEachDto[] getAaaItems() {
    aaaItems = new ForEachDto[] { new ForEachDto(), new ForEachDto(),
        new ForEachDto(), new ForEachDto(), new ForEachDto() };
    aaaItems[0].setKey("111");
    aaaItems[1].setKey("222");
    aaaItems[2].setKey("333");
    aaaItems[3].setKey("444");
    aaaItems[4].setKey("555");
    return aaaItems;
  }

  public void setAaaItems(ForEachDto[] bbbItems) {
    this.aaaItems = bbbItems;
  }

  public int getAaaIndex() {
    return aaaIndex;
  }

  public void setAaaIndex(int bbbIndex) {
    this.aaaIndex = bbbIndex;
  }

  public String getKey() {
    return this.key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public int getClickIndex() {
    return this.clickIndex;
  }

  public void setClickIndex(int clickIndex) {
    this.clickIndex = clickIndex;
  }

  public String doForEachResult() {
    System.out.println("##### CLICKED INDEX[" + clickIndex + "] #####");
    return "forEachResult";
  }

  public static class ForEachDto implements Serializable {

    private static final long serialVersionUID = 1L;

    private String key;

    public ForEachDto() {
    }

    public String getKey() {
      return key;
    }

    public void setKey(String key) {
      this.key = key;
    }
  }
}