简体   繁体   English

jQuery Datatable的所有记录中的Struts2复选框

[英]Struts2 checkbox in all records of jQuery Datatable

I am trying to implement the checkbox as a column in a jquery datatable. 我试图将复选框实现为jquery数据表中的列。 When I select the records then those alone should be actioned in the respective Action class. 当我选择记录时,应单独在相应的Action类中对这些记录进行操作。 I have created a hidden variable and am assigning the value in jQuery likewise: 我创建了一个隐藏变量,并同样在jQuery中分配值:

HTML: HTML:

<s:iterator value="warehouseBean.pickReqList" status="matStat">
    <s:hidden name="warehouseBean.pickReqList[%{#matStat.index}].grade"></s:hidden>
    <s:hidden name="warehouseBean.pickReqList[%{#matStat.index}].quantity"></s:hidden>
    <s:hidden name="warehouseBean.pickReqList[%{#matStat.index}].action"></s:hidden>
</s:iterator>

<tbody><s:iterator value="warehouseBean.pickReqList" status="matStat">
    <tr>
         <td><s:property value="grade" /></td>
         <td><s:property value="quantity" /></td>
         <td><s:select name="warehouseBean.pickReqList[%{#matStat.index}].pickQuantity" list="pickQuantity"></s:select></td>
         <td><s:checkbox name="chkboxname" cssClass="checkbox pickIndivCheck"></s:checkbox></td>
    </tr>
    </s:iterator>
</tbody>

Whenever the checkbox is clicked, then the hidden variable is assigned with the value. 只要单击该复选框,就会为隐藏变量分配值。

jQuery: jQuery的:

$('.pickIndivCheck').click(function(e){
   var index=$(this).closest('tr').index();
   $('#warehousePick_warehouseBean_pickReqList_'+index+'__action').val(e.target.checked); });

The above code works perfectly if I do not sort or navigate to different page of the table. 如果我不对表格进行排序或导航到该表的其他页面,则以上代码可以完美地工作。 If I select the check box on the first page of the datatable, the checked property is assigned to the action variable and the same is reflected in Action class. 如果选中数据表第一页上的复选框,则选中的属性将分配给操作变量,并且在Action类中也会反映出来。

If I sort or paginate then its not working. 如果我排序或分页,则它不起作用。 Upon troubleshooting, I found out that the jQuery code is getting the index of the clicked checkbox and assigns the value. 故障排除后,我发现jQuery代码正在获取单击复选框的索引并分配值。 For ex, if there are 20 elements in the object, loading in two different pages of the datatable. 例如,如果对象中有20个元素,则在数据表的两个不同页面中加载。 The hidden variables are written likewise: 隐藏的变量也这样写:

<input type="hidden" name="warehouseBean.pickReqList[0].grade" value="BR000-R" id="warehousePick_warehouseBean_pickReqList_0__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[0].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_0__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[0].action" value="false" id="warehousePick_warehouseBean_pickReqList_0__action"/>

<input type="hidden" name="warehouseBean.pickReqList[1].grade" value="BR001-R" id="warehousePick_warehouseBean_pickReqList_1__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[1].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_1__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[1].action" value="false" id="warehousePick_warehouseBean_pickReqList_1__action"/>

<input type="hidden" name="warehouseBean.pickReqList[2].grade" value="BR002-R" id="warehousePick_warehouseBean_pickReqList_2__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[2].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_2__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[2].action" value="false" id="warehousePick_warehouseBean_pickReqList_2__action"/> .........

<input type="hidden" name="warehouseBean.pickReqList[18].grade" value="BR0018-R" id="warehousePick_warehouseBean_pickReqList_18__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[18].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_18__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[18].action" value="false" id="warehousePick_warehouseBean_pickReqList_18__action"/>

<input type="hidden" name="warehouseBean.pickReqList[19].grade" value="BR0019-R" id="warehousePick_warehouseBean_pickReqList_19__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[19].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_19__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[19].action" value="false" id="warehousePick_warehouseBean_pickReqList_19__action"/>

This is how it appears on the datatable as well. 这也是它在数据表上的显示方式。 If I sort the table, then the last records appear in the first page of table. 如果我对表进行排序,那么最后的记录将出现在表的第一页中。 If I select the second record, the jQuery method gets the current index (which is 1) is assigned to the hidden variable 如果选择第二条记录,则jQuery方法将当前索引(即1)分配给隐藏变量

warehousePick_warehouseBean_pickReqList_1__action warehousePick_warehouseBean_pickReqList_1__action

whereas the actual variable should be 而实际变量应为

warehousePick_warehouseBean_pickReqList_18__action warehousePick_warehouseBean_pickReqList_18__action

The index of the datatable record is not same as the index of the hidden object. 数据表记录的索引与隐藏对象的索引不同。 Can you please let me know how to assign the values to the checkbox of rows in different table? 您能否让我知道如何将值分配给不同表中的行的复选框?

I created a dummy variable temp and then changed the HTML likewise 我创建了一个虚拟变量temp ,然后同样更改了HTML

<tr>
     <td><s:property value="grade" /></td>
     <td><s:property value="quantity" /></td>
     <td><s:select name="warehouseBean.pickReqList[%{#matStat.index}].pickQuantity" list="pickQuantity"></s:select></td>
     <td><s:checkbox name="warehouseBean.pickReqList[%{#matStat.index}].temp" cssClass="checkbox pickIndivCheck"></s:checkbox></td>
</tr>

Changed jQuery to get the id of the check box (which is clicked), extracted the index from the ID and then assigned to the respective hidden variable after creating the variable using the index, likewise: 更改jQuery以获取复选框的ID(单击该ID),从ID中提取索引,然后在使用索引创建变量后将其分配给相应的隐藏变量,同样:

$('.pickIndivCheck').click(function(e){
    var clId=this.id;
    var temp=clId.substring(40);
    var n=temp.lastIndexOf('_');
    var index=temp.substring(0,n-1);
    $('#warehousePick_warehouseBean_pickReqList_'+index+'__action').val(e.target.checked);   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM