如何在DataTables jqueryPlugin中实现排序?

时间:2015-10-15 11:51:09

标签: spring-mvc datatables

     @RequestMapping(value = "/CustomerData", method = RequestMethod.GET)
        public @ResponseBody String customerData(
                     HttpServletRequest  request,HttpServletResponse response,Model model,
                     @RequestParam(value = "order",required = false, defaultValue = "ASC") String order,
                     @RequestParam(value = "orderBy",required = false, defaultValue = "customerId") String orderBy) throws IOException {

            //Fetch the page number from client
         Integer pageNumber = 0;
            if (null != request.getParameter("iDisplayStart"))
                pageNumber = (Integer.valueOf(request.getParameter("iDisplayStart"))/10)+1;     

            //Fetch search parameter
            String searchParameter = request.getParameter("sSearch");
            String orderByVal = request.getParameter("iSortCol_0");
            String orderVal = request.getParameter("sSortDir_0");

            if(!Employee.isEmptyString(orderByVal))
            {
                if(!Employee.isEmptyString(orderVal))
                {
                    order = orderVal;
                }
                else
                {
                    order = "desc";
                }

                if(orderByVal.equalsIgnoreCase("0"))
                    orderBy = "customerId";
                else if(orderByVal.equalsIgnoreCase("1"))
                    orderBy = "customerName";
                else if(orderByVal.equalsIgnoreCase("2"))
                    orderBy = "phoneNo";
                else if(orderByVal.equalsIgnoreCase("3"))
                    orderBy = "area";
                else if(orderByVal.equalsIgnoreCase("4"))
                    orderBy = "city";
                else
                {
                    orderBy = "customerId";
                    order = "desc";
                }
            }

            //Fetch Page display length
            Integer pageDisplayLength = Integer.valueOf(request.getParameter("iDisplayLength"));

            Integer record = (pageNumber-1)*pageDisplayLength;
            //Create page list data

            List<Customer> customerList = customerService.getCustomerList(record,pageDisplayLength);

            customerList = getCustomerListBasedOnSearchParameter(searchParameter, customerList);
            customerList = getSortedCustomer(customerList, order,orderBy);
            response.setContentType("application/Json");    

            Integer count= null;

            if(customerList != null && customerList.size() > 0){
                 count=customerList.size();
            }else{
                count=customerService.count();
                customerList = customerService.getCustomerList(record,pageDisplayLength);
            }
            model.addAttribute("order", order);
            model.addAttribute("orderBy", orderBy);

            CustomerJsonObject customerJson=new CustomerJsonObject();
            //Set Total display record
            customerJson.setiTotalDisplayRecords(count);
            //Set Total record
            customerJson.setiTotalRecords(count);
            customerJson.setAaData(customerList);
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(customerJson);
            return json;
        }

//上面的代码是我的控制器代码。 但它在我的项目中没有表现。 如果有任何错误,你能否告诉我控制器的变化。我已经尝试过这样做了但我的我不可能。我能知道它的解决方案。

//I have implemented in the below way but i dont know how to send these values to jsp through json object.

public List<Customer> getSortedCustomer(List<Customer> customerList,
                final String order, final String orderBy) {
            final boolean desc = order.equals("DESC");
            final int sortDirection=desc ? -1:1;
            Collections.sort(customerList, new Comparator<Customer>() {

                @Override
                public int compare(Customer c1, Customer c2) {

                     if (orderBy.equals("customerId")) {
                        return c1.getCustomerId().compareTo(c2.getCustomerId()) * sortDirection;
                    } else if (orderBy.equals("customerName")) {
                        return c1.getCustomerName().compareTo(c2.getCustomerName()) * sortDirection;
                    } else if (orderBy.equals("phoneNo")) {
                        return c1.getPhoneNo().compareTo(c2.getPhoneNo()) * sortDirection;
                    } else if (orderBy.equals("email_Id")) {
                        return c1.getEmail_Id().compareTo(c2.getEmail_Id()) * sortDirection;
                    } else if (orderBy.equals("area")) {
                        return c1.getArea().compareTo(c2.getArea()) * sortDirection;
                    }else if (orderBy.equals("city")) {
                        return c1.getCity().compareTo(c2.getCity()) * sortDirection;
                    }
                    return 0;
                }
            });
            return customerList;
     }

0 个答案:

没有答案