1. 程式人生 > >freeMark標籤的使用和批量刪除

freeMark標籤的使用和批量刪除

親愛的小夥伴們好久不見,最近比較忙,所以有將近半年的時間沒寫部落格了,從今天起,我決定要重新實行起我的這個習慣。今天要總結的內容呢,就是最近接觸比較多的freeMark標籤,一開始我都不知道這是個幹啥的,其實吧,我個人感覺就和jstl差不多,只是寫法兒可能略有不同,freeMark的具體概念我就不陳述了,因為百度百科比我講的更清楚,下面來看一下具體寫法兒。

  1. 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">&#xe6df;</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;謝謝大家。