1. 程式人生 > 實用技巧 >2020年11月30號圖論程式碼

2020年11月30號圖論程式碼

關於所搜框中日期搜尋的問題

運用場景,常見的搜尋框搜尋

頁面

html頁面

<div class="layui-col-md3">
   <div class="item_serach">
     <label class="layui-form-label">起止時間: </label>
     <div class="layui-input-block">
     <input type="text" value="" name="search_createTime" class="layui-input " id="test10" placeholder="請選擇時間">
   </div>
  </div>
</div>

js

外部js

 layui.use('laydate', function(){
        var laydate = layui.laydate;
        //日期範圍
        laydate.render({
            elem: '#test10'
            ,range: '~'

        });
    });

內部js

table.render({
            elem: '#userdata'
            , url: '/fjcj/wfcj/pageQuery'
            , method: 'post'
            , cols: [[
                {type: 'numbers', title: '序號', width: '40'}
                , {field: 'hphm', width: 100, title: '車牌號碼' }
                , {field: 'hydd', width: 100, title: '核驗地點'}
                , {field: 'wfmc', width: 100, title: '違法名稱'}
                , {field: 'shzt', title: '稽核狀態', width: 100, templet: '#userStatus'}
                , {field: 'sfsb', title: '是否同步', width: 100, templet: '#Status'}
                , {field: 'xm', width: 100, title: '輔警姓名'}
                , {field: 'fjjh', width: 100, title: '輔警警號'}
                ,{
                    field: 'createTime', title: '採集時間', templet: function (d) {
                        return layui.util.toDateString(new Date(d.createTime).getTime(), "yyyy-MM-dd HH:mm:ss");
                    }, width: 160, unresize: true
                }
                , {title: '操作', align: 'center', width: '130', toolbar: '#table-system-user'}
            ]]
            , page: true
            , limit: 10
            , limits: [10, 20, 30, 40, 50]
            , text: {none: '暫無資料'}
            , done: function (res) {
                element.render('progress');

            }
        });

Controller

Map<String, Object> searchParams = WebUtils.getParametersStartingWith(request, "search_");
        if(searchParams.get("createTime")!=null&&StringUtils.isNotBlank(searchParams.get("createTime").toString())) {
            String timeStr = searchParams.get("createTime").toString();
            String timeArray[] = timeStr.split("~");
            searchParams.put("startTime", timeArray[0].trim());
            searchParams.put("endTime", timeArray[1].trim());
        }

Service : 在EntityManager工廠類中寫法

    if(searchParams.get("startTime") != null && StringUtils.isNotBlank(searchParams.get("startTime").toString())
                && searchParams.get("endTime") != null && StringUtils.isNotBlank(searchParams.get("endTime").toString())
        ){
            String startTime = searchParams.get("startTime").toString()+" 00:00:00";
            Date dateStar = DateUtil.getDate(startTime, "yyyy-MM-dd HH:mm:ss");
            String endTime = searchParams.get("endTime").toString()+" 23:59:59";
            Date dateEnd = DateUtil.getDate(endTime, "yyyy-MM-dd HH:mm:ss");

            predicate =  ExpressionUtils.and(predicate,qWfcj.createTime.between(dateStar,dateEnd));
        }

另一種寫法

Controller

  Sort sort = new Sort(Sort.Direction.DESC, "modiTime");
        String userid = UserHelper.getCurrentUserId();
        if(searchParams.get("createTime")!=null&&StringUtils.isNotBlank(searchParams.get("createTime").toString())) {
        	String timeStr = searchParams.get("createTime").toString();
        	String timeArray[] = timeStr.split("~");
        	searchParams.put("startTime", timeArray[0].trim());
        	searchParams.put("endTime", timeArray[1].trim());
        }

Service

 if(searchParams.get("startTime") != null && StringUtils.isNotBlank(searchParams.get("startTime").toString())){
                    predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), searchParams.get("startTime").toString()+" 00:00:00"));
                }
                
 if(searchParams.get("endTime") != null && StringUtils.isNotBlank(searchParams.get("endTime").toString())){
                    predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), searchParams.get("endTime").toString()+" 23:59:59"));
                }