亲爱的小伙伴们好久不见,最近比较忙,所以有将近半年的时间没写博客了,从今天起,我决定要重新实行起我的这个习惯。今天要总结的内容呢,就是最近接触比较多的freeMark标签,一开始我都不知道这是个干啥的,其实吧,我个人感觉就和jstl差不多,只是写法儿可能略有不同,freeMark的具体概念我就不陈述了,因为百度百科比我讲的更清楚,下面来看一下具体写法儿。
- freemark遍历list集合
<tbody class="text-c">
<#list mPage.getList() as item>
<tr>
<td><input type="checkbox" class="checked_detect" value="${item.t_user_id!}" name=""></td>
<td>${item.name!}</td>
<td>
<#if '0'==item.state!>
<span class="label label-success radius">正常</span>
<#elseif '1'==item.state!>
<span class="label label-warning radius">禁用</span>
<#elseif '2'==item.state!>
<span class="label label-warning radius">驳回</span>
</#if>
</td>
<td>${item.user_name!}</td><!-- 账户 -->
<td>${item.mobile!}</td><!-- 手机 -->
</tr>
</#list>
</tbody>
注:解释一下,<#list mPage.getList() as item>的意思就是myPage取出它里边的list集合,然后起个别名叫item,<#list>则是freeMark的遍历写法;它的判断也比较有特点,elseif啥的都得在<#if>里边写,否则报错;取值的话,就和el表达式写法差不多,需要注意的是感叹号,感叹号后边可以写你的默认值,比方说写个0啥的,通俗易懂。
后台写法和普通list写法一样,该怎么写还怎么写,这里就不在展示了。
2. 批量删除
(1)、jQuery写法
$(function(){
$("#dataBatchDel").click(function(){
//首先获取所有选中的id集合
var check=$(".checked_detect:checked");
var id_list=new Array();
for(var i=0;i<check.length;i++){
id_list[i]=$(check[i]).attr("value");
}
if(id_list.length==0){
layer.msg("请至少选中一个新闻进行删除!",{icon:0,time:1000});
}else{
layer.confirm('确认要进行删除吗?',function(){
$.ajax({
type:"post",
cache:false,
url:"${basePath}/admin/news/batchdel",
contentType : "application/x-www-form-urlencoded; charset=utf-8",
data:{
id_list:id_list
},
dataType:"json",
success:function(data){
for(var i=0;i<check.length;i++){
$(check[i]).parents("tr").remove();
}
layer.msg('已删除!',{icon:1,time:1000});
},
error:function(data){
layer.msg('请求失败!',{icon:2,time:1000});
}
});
});
}
});
})
注:首先你得先让它选择勾选框吧,这段jQuery就是判断是否选择勾选框的,如果没勾选,就提示让用户至少勾选一条。
(2)、遍历
<table id="deptTbl" class="table table-border table-bordered table-hover table-bg">
<thead>
<tr>
<th scope="col" colspan="10">
新闻管理
<!-- 统计共有多少个新闻 -->
<span class="r">共有数据:<strong id="deptCount">${mPage.totalRows}</strong> 条</span>
</th>
</tr>
<tr class="text-c">
<th width="25"><input type="checkbox" value="" name=""></th>
<th width="200">新闻标题</th>
<th width="200">新闻内容</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
<#list mPage.getList() as item>
<tr class="text-c">
<td><input type="checkbox" class="checked_detect" value="${item.id}" name=""></td>
<td>${item.title!}</td>
<td>
<a onclick="look('新闻内容','${basePath}/admin/news/lookcontent','${item.id}',800,400);">点击查看新闻内容</a>
</td>
<td>
<a title="编辑" href="javascript:;" onclick="edit('新闻编辑','${basePath}/admin/news/edit','${item.id}',800,300)" style="text-decoration:none"><i class="Hui-iconfont"></i></a>
</td>
</tr>
</#list>
</tbody>
</table>
注:checkbox就是勾选框,所以,在下面,需要把id值填一下,这样才能传过去值,然后通过上面的jQuery把id组成一个集合,传给后台,后台再来解析。
(3)、后台写法
public String deleFinance(@RequestParam("id_list[]") List<Integer> id_list) {
boolean f = newsDao.batchDeleteNews(id_list);
return getResultJSON(f);
}
注:首先你得接到这个集合,用注解requestparam接一下
public boolean batchDeleteNews(List<Integer> idList) {
boolean f = true;
for (int i = 0; i < idList.size(); i++) {
String s = "delete from t_news where id=" + idList.get(i);
System.out.println(s);// 打印sql语句
f&=jdbcTemplate.update(s)>0;
String d="delete from t_news_view_type where news_id=" + idList.get(i);
System.out.println(d);
f&=jdbcTemplate.update(d)>0;
}
return f;
}
注:然后就是循环删除,应该都可以看懂吧,就不具体解释了。
就先写到这里吧,如果有什么不太清楚的地方,欢迎小伙伴们给我留言,或者发我qq也行,qq号码:2978975101;谢谢大家。