zoukankan      html  css  js  c++  java
  • 随笔8

    easyui的批量操作,因需求的需要,还要增加相应的评论数功能:

    首先在页面上添加了批量通过和批量驳回的按钮并添加复选框:

     <a onclick="auditSuccess('0','2');" href="javascript:void(0);" class="easyui-linkbutton"
               data-options="plain:true,iconCls:'icon-edit'">批量通过</a>
            <a onclick="auditFail('0','3');" href="javascript:void(0);" class="easyui-linkbutton"
               data-options="plain:true,iconCls:'icon-edit'">批量驳回</a>

    '2'和'3'是状态,2为通过,3为驳回

    添加checkbox复选框,并将属性singleSelect改成false

    批量通过的前端代码如下:

        function auditSuccess(e,status) {
            var msg = '确定审核通过?';
            $.messager.confirm('提示', msg,function (r) {
                if (r) {
                    var ids = [];
                    var postbarIds=[];
                    var rows = $('#dg').datagrid('getSelections');
                    if (rows.length==0) {
                        $.messager.alert("提示", "请选择要删除的行!", "info");
                        return;
                    }
                    for(var i=0;i<rows.length; i++){
                        if (rows[i].status!==status){
                        ids.push(rows[i].id);
                        postbarIds.push(rows[i].postbarId);
                        }
                    };
                    $.ajax({
                        url: '${request.contextPath}/postbar/auditCommentBatch',
                        type: 'POST',
                        data:{ids:JSON.stringify(ids),
                              status:status,
                              postbarIds:JSON.stringify(postbarIds)},
                        success: function (result) {
                            if (result.code == 0) {
                                $.messager.show({
                                    title: '提示',
                                    msg: result.message
                                });
                                $('#dg').datagrid('reload');
                            } else {
                                $.messager.show({
                                    title: '提示',
                                    msg: result.message
                                });
                            }
                        }
                    });
                }
            });
        }
    var rows = $('#dg').datagrid('getSelections'); 是获取复选框选中的多行数据
    var row = $('#dg').datagrid('getSelection');是获取单选框选中的一条数据
    一个s的差距相差很大,这是第一个注意点,还有一点需要注意的是传输的数据格式,json传到后台的是[160][161][155][154]的形式,用JSON.stringify转换成字符串后是[160,161,155,154],后台直接string接收参数后转化成list来进行业务逻辑操作
    对于需求来说,另外需要注意的一点是,批量通过重复操作的话会让后台增加评论数出现bug,因此我在这里添加了一句if(row[i].status!==status)的判断,让已经通过的评论在被重复选中后不把值传给后台,这样就不会出现重复增加评论的bug

    数据库是通过原生SQL语句:

    上面是更改数据库中未通过已通过已驳回的状态,下面一个是根据id增加评论数
     @Transactional(rollbackFor = Exception.class)
        @Modifying
        @Query(value = "update postbar set status = :status where id in :ids", nativeQuery = true)
        void updateStatusByIds(@Param("ids")List<Long> ids, @Param("status") Integer status);
    
        /**
         * 更新帖子评论数
         *
         * @param id
         * @param num
         */
        @Transactional(rollbackFor = Exception.class)
        @Modifying
        @Query(value = "update postbar set comment_num = comment_num + :num where id = :id", nativeQuery = true)
        void updateCommentNumById(@Param("id") Long id, @Param("num") Integer num);

    注意一点,因为传过来的ids是一个集合的关系where后面是id in :ids而不是之前单个id时的id=:id

    业务逻辑层的操作:

    先放上代码:

     @Transactional(rollbackFor = Exception.class)
        public Result auditCommentBatch(String ids, Integer status,String postbarIds){
            List<Long> newIds  = JSONArray.parseArray(ids,Long.class);//1
            postBarCommentDAO.updateStatusByIds(newIds,status);
            List<Long> newPostBarIds  = JSONArray.parseArray(postbarIds,Long.class);//2
            Map<Long,Long> map=new HashMap();
            for(Long id :newPostBarIds){
                map.put(id,map.get(id)==null?1:map.get(id)+1);//3
            }
            if (Constant.AuditStatus.APPROVED ==status) {//4
                for(Long id:map.keySet()){
                    postBarDAO.updateCommentNumById(id,  map.get(id).intValue());//5
                }
            }
            return Result.builder();
        }

    //1和//2:一开始用用拆解再遍历得到list,后来网上查了后发现json的字符串可以直接通过这个方法直转List,括号内前一个是参数,后一个是希望转List的类型.

    //3:循环遍历,通过三元运算符得到对应每个postbarId的循环次数也就是他们在集合中的个数

    //4:判断前端传过来的状态是不是为2是通过,是的话继续执行,不是的话直接返回

    //5:遍历map,通过keyset()获得map的key值也就是括号中的id,也是postbarId再通过map.get()获得value值,就是上面得到的在集合中的个数,有几个就加几个评论数



    上面有提到json字符串直接转List的方法,后来又去查了下也有json字符串直转map的方法,写了个demo,放在下面:
    不过要注意要在pom.xml中添加相关的依赖
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.32</version>
                <scope>test</scope>
            </dependency>
    demo:
    package com.lk.menu.json;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    
    import java.util.Map;
    
    /**
     * @Author: Lukizzz
     * @Date: 2018/8/23 15:50
     * @Description:
     */
    public class JsonToMapTest {
    
        public static void main(String[] args){
            String string = "{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}";
    
            Map maps = (Map) JSON.parse(string);
            System.out.println("这个是用JSON类来解析JSON字符串!!!");
            for (Object map:maps.entrySet()){
                System.out.println(((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());
            }
    
            Map mapType = JSON.parseObject(string);
            System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
            for (Object object:mapType.keySet()){
                System.out.println("key为:"+object+"值为:"+mapType.get(object));
            }
    
            Map mapType1 = JSON.parseObject(string,Map.class);
            System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
            for (Object object:mapType1.keySet()){
                System.out.println("key值为:"+object+"值为:"+mapType1.get(object));
            }
    
            Map mapType2 = (Map) JSONObject.parse(string);
            System.out.println("这个是用JSONObject的parseObject方法来解析JSON字符串!!!");
            for (Object map:mapType2.entrySet()){
                System.out.println(((Map.Entry)map).getKey()+"  "+((Map.Entry)map).getValue());
            }
    
            Map json = JSONObject.parseObject(string);
            System.out.println("第六种");
            for (Object map: json.entrySet()){
                System.out.println(((Map.Entry)map).getKey()+"  "+((Map.Entry)map).getValue());
            }
        }
    
    }



  • 相关阅读:
    c#两级菜单menu的动态实现
    单引号写入数据库,并结合写成函数和动静态类中方法对比小结
    google地图路径查询
    c# 图像旋转
    google地图简单
    asp.net gridview 添加属性
    linq to entity Oracle 执行存储过程或函数
    c# 判断非法字符
    c# 写入文件
    google map Inspecting DirectionsResults
  • 原文地址:https://www.cnblogs.com/Lukizzz/p/9546617.html
Copyright © 2011-2022 走看看