全选
//全选
function chooseAll() {
$("input[type='checkbox']").attr("checked", true);
}
反选
//反选
function reverseAll() {
//获取表格里的所有checkbox选择框
$("#list-table").find("input[type='checkbox']").each(function () {
$(this).attr("checked",!$(this).attr("checked"))
})
}
批量删除(异步)
//批量删除
function batchDelete() {
//获取所有选中的id
var ids = [];
$("#list-table").find("input:checked").each(function () {
var customerId = $(this).val();
ids.push(customerId);
});
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/cust/batchDelete",
data:{"_method":"DELETE","ids":ids},
success:function (msg) {
if (msg.status == 200){
alert("删除成功!");
location.href="${pageContext.request.contextPath}/cust/list";
}
}
})
}
@RequestMapping(value = "batchDelete",method = RequestMethod.DELETE)
@ResponseBody
public Map<String,Object> batchDelete(@RequestParam("ids[]") Integer[] ids){
customerService.batchDelete(ids);
Map<String,Object> map = new HashMap<String, Object>();
map.put("status",200);
return map;
}
public void batchDelete(Integer[] ids) {
CustomerExample example = new CustomerExample();
CustomerExample.Criteria criteria = example.createCriteria();
criteria.andIdIn(Arrays.asList(ids));
customerMapper.deleteByExample(example);
}
多条件查询
前端表单
<!-- 搜索表单 -->
<form name='form3' action='${pageContext.request.contextPath}/cust/search' method='get'>
<input type='hidden' name='dopost' value=''/>
<table width='98%' border='0' cellpadding='1' cellspacing='1' bgcolor='#CBD8AC' align="center"
style="margin-top:8px">
<tr bgcolor='#EEF4EA'>
<td background='skin/images/wbg.gif' align='center'>
<table border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='90' align='center'>搜索条件:</td>
<td width='160'>
<select name='cid' style='width:150'>
<option value='0'>选择类型...</option>
<option value='1'>公司名称</option>
<option value='2'>联系人姓名</option>
</select>
</td>
<td width='70'>
关键字:
</td>
<td width='160'>
<input type='text' name='keyword' value='' style='width:120px'/>
</td>
<td width='110'>
<select name='orderby' style='width:120px'>
<option value='0'>排序...</option>
<option value='0'>id升序</option>
<option value='1'>id降序</option>
</select>
</td>
<td>
<input name="imageField" type="image" src="${pageContext.request.contextPath}/skin/images/frame/search.gif"
width="45" height="20" border="0" class="np"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
后端Controller层
后端接收的字段名和前端页面name保持一致,不一致用@Requestparam接收
@RequestMapping(value = "/search",method = RequestMethod.GET)
public ModelAndView search(Integer cid,String keyword,Integer orderby){
List<Customer> customers = customerService.search(cid,keyword,orderby);
ModelAndView mv = new ModelAndView("customer");
mv.addObject("customers",customers);
return mv;
}
Service层
public List<Customer> search(Integer cid, String keyword, Integer orderby) {
CustomerExample example = new CustomerExample();
CustomerExample.Criteria criteria = example.createCriteria();
//查询条件为默认(有可能是公司名称也有可能是联系人姓名)
if (cid==0){
//条件一:公司名称
criteria.andComnameLike("%"+keyword+"%");
//条件二:联系人姓名
CustomerExample.Criteria criteria2 = example.createCriteria();
criteria2.andCompanypersonLike("%"+keyword+"%");
example.or(criteria2);
}
else if (cid==1){//根据公司名称查询
criteria.andComnameLike("%"+keyword+"%");
}
else {//根据联系人名称查询
criteria.andCompanypersonLike("%"+keyword+"%");
}
//默认是升序
if (orderby==1){//降序
example.setOrderByClause("id desc");
}
List<Customer> customers = customerMapper.selectByExample(example);
return customers;
}
注意:如果tomcat使用的是tomcat7,要解决get请求乱码,不然查询不到数据