天天看点

easyui实现多行删除

此文为博主原创内容,转载请说明出处,欢迎各位批评指点。

最近楼主在自己研究easyui数据网格多行删除操作,以下为本人经验总结,供大家参阅:

多行删除的思想就是:取到所选中多行的ID进行字符串拼接,传到后台对字符串分割,循环进行删除每一行。

所以说此问题的重点就是解决字符串的分割、拼接。

表格

<div id="dlg" class="easyui-dialog"
		style="width: 500px; height: 300px;" title="编辑用户" iconCls="icon-edit"
		closed="true" closable="true">
		<s:form id="userForm" action="save" namespace="/testuser">
			<s:token />
			<s:hidden name="newUser.fuserid" id="newUser_fuserid"/>
			<table id="userTable" cellpadding=0 cellspacing=0 border=0
				class="ListTable" align="center" style="width: 40%">
				<tr>
					<td class="EditHead" nowrap><font color=red>*</font>&nbsp;用户账号</td>
					<td class="editTd"><input class ="noborder
							easyui-validatebox" data-options="required:true,validType:'length[1,50]'"
							type="text" name="newUser.floginname" id='newUser_floginname'
							maxlength="255" title="用户账号" /></td>
				</tr>
				<tr>
					<td class="EditHead" nowrap><font color=red>*</font>&nbsp;用户名称</td>
					<td class="editTd"><input class="noborder easyui-validatebox"
						data-options="required:true,validType:'length[1,50]'" type="text"
						name="newUser.fname" id='newUser_fname' maxlength="255"
						title="用户名称" /></td>
				</tr>
			</table>
		</s:form>
	</div>
           

js方法:

<script type="text/javascript">

$(document).ready(function(){
	var editIndex = undefined;
	$('#userTable').datagrid({
		url:'${contextPath}/testuser/queryUser.action?querySource=123',
		rownumbers:true,
		pagination:true,
		fitColumns:true,
		fit:true,
		idField:'fuserid',
		singleSelect:false,   //此处不能忘记改为false,否则不能多选
		striped:true,
		pageSize:10,
		toolbar:[{
			id:'delUser',
			text:'删除',
			iconCls:'icon-remove',
			handler:function(){
				var selectRows = $('#userTable').datagrid('getChecked');
				if(selectRows.length == 0){
					showMessage1('请选择要删除的用户!');
					return;
				}
				var fuserids = [];
				for (var i = 0; i < selectRows.length; i++) {
					fuserids += selectRows[i].fuserid + ",";    //把fuserid(字符串类型)以“,”拼接
	            }
				fuserids = fuserids.substr(0, fuserids.length - 1);   //去掉最后一个“,”
				//alert(fuserids);
				if (confirm('确定删除吗?')) {
				window.location.href="${contextPath}/testuser/delUser.action?fuserids=" target="_blank" rel="external nofollow" +fuserids;
					return true;
				} else {
					return false;
				}
			}
		}
	
	]
		columns:[[
				  {field:'fuserid',checkbox:true},
		          {field:'floginname',title:'用户账号',sortable:true, width:100},
		          {field:'fname',title:'用户姓名', width:100,
		        	editor:{type:'validatebox',options:{required:true}}  
		          },
		          {field:'fdepname',title:'所在单位', width:100},
		          {field:'fsex',title:'性别', width:100},
		]]
	});
});
</script>
           

后台代码:

public String delete() {
		String fuserids = this.getRequestParameter("fuserids");  //接受拼接后的字符串(id)
		//System.out.println(fuserids);
		String[] delFuserids = fuserids.split(",");   //分割字符串
		for (int i = 0; i < delFuserids.length; i++) {   //每次循环删除一行
			newUser = (UUser) this.testUserService.get(UUser.class,delFuserids[i]);
			if (newUser != null) {
				testUserService.delete(newUser);
			}
		}
		return SUCCESS;
	}
	```


           

继续阅读