从具有struts2的表中删除动态添加的行

时间:2013-09-26 06:25:21

标签: java jsp struts2

我对此很新,我知道如何从使用struts2创建的动态表中获取索引行。这是我到目前为止所尝试的:

测试文件

<table class="table table-bordered table-striped table-condensed table-hover"
        id="exp">
    <thead>
        <tr>
            <th class="center"></th>
            <th>Name</th>
        </tr>           
    </thead>

    <tbody>
        <s:if test="listanume.size>0">
            <s:iterator value="listanume" status="statusVar">
                <tr>
                    <td class="center">
                            <button class="btn" onclick="javascript:setval(0)">
                                delete
                            </button>
                    </td>
                    <td><s:property value="nume"/></td>
                </tr>
            </s:iterator>
        </s:if>
    </tbody>
</table>
<h4>Numarul randului este: <s:property value="mysample"/> </h4>

    <script>
        var mysample= '';
        function= setval(varval){
            mysample=varval;
            alert(mysample);
        }
    </script>

pop java

public class Pop {
private int nrrow;
 private List<UserModel> listanume = new ArrayList<UserModel>();

 UserModel user = new UserModel(),
        user1 = new UserModel(),
        user2 = new UserModel();

 private String[] sample;

 {
     sample = new String[]{"list1", "list2", "list3"};
 }


 public String execute() {

    user.setEmail("eret@esre");
    user.setPass1("1234");
    user.setNume("gigel");

    user1.setEmail("yuii@ihj");
    user1.setPass1("6789");
    user1.setNume("marius");

    user2.setEmail("nmnn@cvx");
    user2.setPass1("7676");
    user2.setNume("sorin");

    sample1.add(user);
    sample1.add(user1);
    sample1.add(user2);

    return "succes";
 }

 public String afisnrow(){

    execute();
    listanume=sample1;

    return "succes";
 }

 public String[] getSample() {
    return sample;
 }

 public void setSample(String[] sample) {
    this.sample = sample;
 }

 public List<UserModel> getSample1() {
    return sample1;
 }

 public void setSample1(List<UserModel> sample1) {
    this.sample1 = sample1;
 }

 public void setNrrow(int nrrow){
    this.nrrow=nrrow;
 }

 public int getNrrow(){
    return nrrow;
 }

 public List<UserModel> getListanume(){
    return listanume;
 }

 public void setListanume(List<UserModel> listanume){
    this.listanume=listanume;
 }
}

每一行都有一个按钮,当我点击那个按钮时我需要得到行索引,我有点卡住并且没有想法,有什么建议吗?

2 个答案:

答案 0 :(得分:1)

替换         onclick="javascript:setval(0)"

onclick="javascript:setval(<s:property value="#statusVar.index" />)"

有关struts2 iterator标记here的更多信息。

答案 1 :(得分:0)

我找到的解决方案是:

测试文件

<table class="table table-bordered table-striped table-condensed table-hover"
    id="exp">
<thead>
    <tr>
        <th class="center"></th>
        <th>Name</th>
    </tr>           
</thead>

<tbody>
    <s:if test="listanume.size>0">
        <s:iterator value="listanume" status="statusVar">
            <tr>
                <td class="center">
                        <button class="btn" id="<s:property value='indexrow'/>">
                            delete
                        </button>
                </td>
                <td><s:property value="nume"/></td>
            </tr>
        </s:iterator>
    </s:if>
</tbody>
</table>

<i><p id="test"> No button pressed.</p></i>

<script>
$("button").click(function(e){
    var idClicked = e.target.id;
$("#test").html(idClicked);
});
</script>

pop java

public class Pop {
private int indexrow;
private List<UserModel> listanume = new ArrayList<UserModel>();

 UserModel user = new UserModel(),
          user1 = new UserModel(),
          user2 = new UserModel();

 public String execute() {

    user.setEmail("eret@esre");
    user.setPass1("1234");
    user.setNume("gigel");

user1.setEmail("yuii@ihj");
user1.setPass1("6789");
user1.setNume("marius");

user2.setEmail("nmnn@cvx");
user2.setPass1("7676");
user2.setNume("sorin");

listanume.add(user);
listanume.add(user1);
listanume.add(user2);

return "succes";
 }

 public String afisnrow(){

   execute();
   for (UserModel userModel : listanume) {
        UserModel userModel1=new UserModel();
        indexrow++;

        userModel1.setNume(userModel.getNume());
        userModel1.setIndexrow(indexrow);

        listanume.add(userModel1);

    }
   return "succes";
 }

   public List<UserModel> getListanume(){
     return listanume;
  }

   public void setListanume(List<UserModel> listanume){
     this.listanume=listanume;
  }
 }