如何在Struts2中取消选中specifc复选框 [英] How to get the specifc unchecked checkboxes in Struts2

查看:131
本文介绍了如何在Struts2中取消选中specifc复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我打印一些文档以及一些复选框(一些复选框在页面加载时默认选中)对一些特定的操作。



现在用户将选中一些复选框,或者可以取消选中某些复选框。并点击更新按钮。



现在我的要求是我要取消选中未选中的复选框值。例如:


当页面第一次加载时,通过
选中的复选框有5个复选框,默认值为700个复选框;



现在,用户将取消选中两个复选框并点击提交。



我的要求是在这里,我想要在我的操作类中取消选中2
复选框的值。


我使用Struts2打印这些文档。我使用Struts2-jQgrid,我试过Struts2 CheckboxInterceptor获取未选中的复选框,但它给了所有未选中的复选框,不仅是所需的复选框(改变它们的状态)。 p>

解决方案

假设您从迭代自定义对象列表开始,

  private List< MyCustomObject> myList; 

,并且您的自定义对象有eg。 boolean isChecked() String getValue()方法,



您可以在目标操作中使用两个不同的列表:一个用于未选中且现在已选中的项目,另一个用于已选中但现在未选中的项目

  private List< String>检查
private List< String>项目未检查

那么在页面中你应该使用经典的复选框来获取选中的项目,存储未检查项的值,使用与检查值(必须没有名称)相反的JavaScript激活其值:

 < s:iterator value =myListstatus =ctr> 
< s:if test =!isChecked()>
<! - 我没有选中,需要捕获只有当我将被检查 - >
< s:checkbox id =checkbox _%{#ctr.index}
fieldValue =%{getValue()}
value =false
name = itemsThatHaveBeenChecked/>
< / s:if>
< s:else>
<! - 我检查了,需要捕获,如果我将被取消选中 - >
< input type =checkbox
id =< s:property value =%{'checkbox _'+#ctr.index}/>
checked =checked
class =startedChecked/>
<! - hidden trick - >
< input type =hidden
id =hidden_​​< s:property value =%{'checkbox _'+#ctr.index}/>
value =< s:property value =%{getValue()}/>
name =itemsThatHaveBeenUnchecked
disabled =disabled/>
< / s:else>
< / s:iterator>

< script>
/ *当复选框未选中时启用隐藏字段
当选中复选框时禁用隐藏字段* /
$(function(){
$(input [type = 'checkbox']。startedChecked)。change(function(){
$(#hidden _+ this.id).prop({disabled:this.checked});
});
});
< / script>

这样,您将只获得两个列表中所需的值。


In my application I am printing some document along with some checkboxes (Some checkboxes get checked by default when page loads) on some specific action.

Now user will Check some checkboxes Or may uncheck some. And clicks on update button.

Now my requirement is I want the checkboxes values that are unchecked.

For example :

When the page loads first time there are 5 checkboxes checked by default out of some 700 check boxes;

now user will uncheck 2 checkboxes and clicks on submit.

My requirement is here I want those 2 unchecked checkboxes values in my action class.

I am using Struts2 to print those documents. I'm using Struts2-jQgrid, I have tried Struts2 CheckboxInterceptor to get the unchecked ones but it's giving me all the unchecked checkboxes, not only the desired ones (the ones that changed their state).

解决方案

Assuming you start by iterating a List of custom objects,

private List<MyCustomObject> myList;

and that your custom object has eg. boolean isChecked() and String getValue() methods,

you could use two different Lists in the destination Action: one for the items that were unchecked and now are checked, and another one for items that were checked and now are unchecked:

private List<String> itemsThatHaveBeenChecked;
private List<String> itemsThatHaveBeenUnchecked;

then in the page you should use a classic checkbox to get the checked items, and an hidden input to store the value of the unchecked items, activating its value with javascript as opposite to the checkbox value (that must have no name):

<s:iterator value="myList" status="ctr">
    <s:if test="!isChecked()">
        <!-- I'm not checked, need to catch only if I will be checked -->
        <s:checkbox id = "checkbox_%{#ctr.index}" 
            fieldValue = "%{getValue()}"
                 value = "false" 
                  name = "itemsThatHaveBeenChecked" />
    </s:if>
    <s:else>
        <!-- I'm checked, need to catch only if I will be unchecked -->
        <input type = "checkbox" 
                 id = "<s:property value="%{'checkbox_'+#ctr.index}"/>"
            checked = "checked"
              class = "startedChecked" />
        <!-- hidden trick -->
        <input type = "hidden" 
                 id = "hidden_<s:property value="%{'checkbox_'+#ctr.index}"/>" 
              value = "<s:property value="%{getValue()}"/>"         
               name = "itemsThatHaveBeenUnchecked" 
           disabled = "disabled" />            
    </s:else> 
</s:iterator>

<script>
    /* Enable the hidden field when checkbox is unchecked, 
      Disable the hidden field when checkbox is checked    */
    $(function() {
        $("input[type='checkbox'].startedChecked").change(function () {
            $("#hidden_"+this.id).prop({disabled : this.checked});
        });
    });
</script>

This way you will get only the values needed in both Lists.

这篇关于如何在Struts2中取消选中specifc复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆