Spring MVC中搜索功能中的多个过滤器选项

时间:2016-10-25 05:16:05

标签: spring-mvc search

This is a UI page where users can be searched in a bootstrap table. And the users to search can be filtered using the - registered time period - name - living area

我用两个ajax调用编写了下面的js,如果没有选中任何一个复选框,它将被定向到处理程序方法以加载整个用户集。

$(document).ready(function(){
   $("#filterButton").click(function(){
    var from = $('#fromDate').val();
    var to=$('#toDate').val();
    var name=$('#cnameSearch').val();
    var city=$('#citySearch').val();

    if((from == "") && (to == "") && (name == "") && (city == "--Select--")  ){
        $.ajax({
            //type: "POST",
            url: "https://localhost:8443/admin/users/view/customerTable",
            success: function (msg) {

                $('#tableCustomer').bootstrapTable('load', msg);
            },
            error: function () {
                alert("ajax failed" + uname);
            }
        });

    }else{
        $.ajax({
            //type: "POST",
            url: "https://localhost:8443/admin/userFilters/customerTable",
            data: {"from": from, "to": to, "name":name, "city":city},
            success: function (msg) {
                alert("ajax succ" + from +name +city);
                $('#tableCustomer').bootstrapTable('load', msg);
            },
            error: function () {
                alert("ajax failed" + uname);
            }
        });
    }
  });
});

如果选中任何复选框,则会将其定向到另一个调用过滤器查询的处理程序方法。下面是处理程序方法。

@RequestMapping(value = "/customerTable", method = RequestMethod.GET)
public @ResponseBody
List<Map<String, Object>> dateFilteredStaff(@RequestParam("from") String from, @RequestParam("to") String to,
                                            @RequestParam("name") String name, @RequestParam("city") String city)
                                            throws ParseException {


    //convert java.util time to sql time
    SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy");
    Date fromDate= sdf.parse(from);
    Date toDate= sdf.parse(to);
    java.sql.Date sqlfromDate= new java.sql.Date(fromDate.getTime());
    java.sql.Date sqltoDate= new java.sql.Date(toDate.getTime());

    LOG.error("fromSql,{}",sqlfromDate);
    LOG.error("toSql {}",sqltoDate);
    LOG.error("name {}",name);
    LOG.error("city {}", city);

    List<Map<String, Object>> outc = new ArrayList<Map<String, Object>>();

    //when both the date fields are filled
    if((!from.equals("")) && (!to.equals(""))){

     //call the database method to retrieve users filtered within a given date range
        List<User> customerList1= customerRepository.retrieveByDateRange(sqlfromDate,sqltoDate);

        for (int i=0;i<customerList1.size();i++){
            User customerUser=customerList1.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", customerUser.getId());
            map.put("username", customerUser.getUsername());
            map.put("first_name",customerUser.getFirstName());
            map.put("last_name", customerUser.getLastName());
            map.put("mobile", customerUser.getMobile());
            map.put("email", customerUser.getEmail());
            map.put("address_line3", customerUser.getAddressL3());
            map.put("registered_date", customerUser.getRegDate());

            LOG.info("newUser {}", customerUser);
            outc.add(map);
            LOG.info("out {}",outc);
        }
    }

    //when only the from date field is filled
    if(!from.equals("") && (to.equals(""))){

   //call the database method to retrieve users filtered from a given date
        List<User> customerList2= customerRepository.selectbyStartingDate(sqlfromDate);

        for (int i=0;i<customerList2.size();i++){
            User customerUser=customerList2.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", customerUser.getId());
            map.put("username", customerUser.getUsername());
            map.put("first_name",customerUser.getFirstName());
            map.put("last_name", customerUser.getLastName());
            map.put("mobile", customerUser.getMobile());
            map.put("email", customerUser.getEmail());
            map.put("address_line3", customerUser.getAddressL3());
            map.put("registered_date", customerUser.getRegDate());

            LOG.info("newUser {}", customerUser);
            outc.add(map);
            LOG.info("out {}",outc);
        }
    }

    return outc ;
}

但只有当两个日期字段都填满时才会进行过滤。当'to'日期字段为空时,不会发生过滤。请有人帮我解决这个问题......

仍然没有编写使用城市和名称进行过滤的逻辑。但我打算把它放在同一个处理程序方法中,如果条件检查.... 那会没事......?或者我应该为此编写单独的处理程序方法并调用新的ajax调用。

任何指导都将受到高度赞赏 提前致谢

0 个答案:

没有答案