简体   繁体   English

数据表列排序不起作用服务器端Spring MVC

[英]Data Table column sorting not working server side Spring MVC

I am fetching some data and showing in a table. 我正在获取一些数据并显示在表格中。 Here I am using data table. 在这里我正在使用数据表。 Everything is working fine but not able to sorting data by clicking the column heading of data table. 一切工作正常,但无法通过单击数据表的列标题对数据进行排序。 I am not able to understand what is the issue. 我无法理解问题所在。 please help me to find the solution. 请帮助我找到解决方案。 My HTML, Javascript and Server side code written below: 我的HTML,Javascript和服务器端代码编写如下:

<div class="widget-body no-padding table-responsive">       
    <table id="manage_quote" class="table table-striped table-bordered table-hover" width="100%">
        <thead>                         
            <tr>
                <th data-hide="phone">Id</th>
                <th data-class="expand"><i class="fa fa-fw fa-user text-muted hidden-md hidden-sm hidden-xs"></i>User Name</th>
                <th data-class="expand"><i class="fa fa-fw fa-user text-muted hidden-md hidden-sm hidden-xs"></i> Agent Name</th>
                <th data-hide="phone,tablet"><i class="fa fa-fw fa-calendar txt-color-blue hidden-md hidden-sm hidden-xs"></i> Created On</th>
                <th data-hide="phone,tablet">Status</th>        
                <th data-hide="phone,tablet">Action</th>                                
            </tr>
        </thead>
    </table>
</div>

Javascript 使用Javascript

<script>
$( document ).ready(function() {

    var table = $('#manage_quote').dataTable({
        "bServerSide": true,
        "sAjaxSource": "/common/getDetails",
        "bProcessing": true,
        "bSort": true,
        "pageLength" : 10,
        language: {
            paginate: {
              next: '&#8594;', // or 'â'
              previous: '&#8592;' // or 'â' 
            }
          }
    }); 

});
</script>

Spring Controller Class 弹簧控制器类

@RequestMapping(value="/common/getDetails")
    public void manageAdvisorDetails(HttpServletRequest request, HttpServletResponse response) 
    {   
        try
        {           
            HashMap<String, String> userMap = (HashMap<String, String>) request.getSession().getAttribute("userMap");
            if(userMap != null && userMap.get("user_type").equalsIgnoreCase(Utils.ADMIN))
            {
               System.out.println("calling");
               String sEcho = request.getParameter("sEcho");
               String sSearch = request.getParameter("sSearch");
               String sColumns = request.getParameter("sColumns");
               String iDisplayStart = request.getParameter("iDisplayStart");
               String iDisplayLength = request.getParameter("iDisplayLength");
               String iColumns = request.getParameter("iColumns");  

               String iTotalRecords = "";
               String iTotalDisplayRecords = "";
               JsonArray data = new JsonArray();

               ManageDetails manageUsersResponse = commonDAO.getDetails( sSearch, iDisplayStart, iDisplayLength);        

               iTotalRecords = manageUsersResponse.getNoOfTotalRecords();
               iTotalDisplayRecords = manageUsersResponse.getNoOfRecords();



               List<Quote> quoteList  = manageUsersResponse.getQuoteList();

               for (Quote quote : quoteList) 
               {
                    System.out.println(quote.getId());
                    System.out.println(quote.getFirst_name());
                    System.out.println(quote.getLast_name());
                    System.out.println(quote.getAgent_name());

                    JsonArray row = new JsonArray();                
                    row.add(new JsonPrimitive(quote.getId()));
                    row.add(new JsonPrimitive(quote.getFirst_name() + " " + quote.getLast_name()));                             
                    row.add(new JsonPrimitive(quote.getAgent_name()));
                    row.add(new JsonPrimitive(quote.getQuote_date().toString()));                   
                    if(quote.getPayment_status().equalsIgnoreCase(Utils.PAYMENT_COMPLETE))
                    {
                        String html = "<a href='/admin?id=" + quote.getId() + "' ><span class='text-success' aria-hidden='true'>" + quote.getPayment_status() + "</span></a>";
                        row.add(html);
                    }
                    else
                    {
                        String html = "<a href='/admin?id=" + quote.getId() + "' ><span class='text-info' aria-hidden='true'>" + quote.getPayment_status() + "</span></a>";
                        row.add(html);
                    }

                   row.add(new JsonPrimitive("<a href='/admin/getClientDetails?id=" + quote.getId() + "'><span class='btn btn-xs btn-success'>View</span></a>"));

                    data.add(row);
               }

                Gson gson = new Gson();        
                JsonObject jsonResponse = new JsonObject();

                jsonResponse.addProperty("sEcho", sEcho);
                jsonResponse.addProperty("iTotalRecords", iTotalRecords);
                jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
                jsonResponse.add("aaData", gson.toJsonTree(data));

                response.setContentType("application/Json");
                response.getWriter().print(jsonResponse.toString());
            }
            else
            {

            }           
        }
        catch (Exception ex) 
        {           
            ex.printStackTrace();
        }
    }

Spring DAO Class 春季DAO班

public static ManageDetails getQuotes(String searchTerm,String iDisplayStart, String iDisplayLength)
    {       
        Session session = null;
        ManageDetails ManageDetails = null;
        UserDetails userdetails = null;
        List<UserDetails> quoteList = null;
        try
        {
            ManageDetails = new ManageDetails();
            session = HibernateUtils.getSession();
            session.getTransaction().begin();

            String query_noOfTotalRecords = "select count(*) from UserDetails";
            String query_str = "from UserDetails ";

            int noOfRecordsPerPage = Integer.parseInt(iDisplayLength);
            int startIndex = Integer.parseInt(iDisplayStart);
            int endIndex = startIndex + noOfRecordsPerPage;

            if (searchTerm !=null && !searchTerm.equalsIgnoreCase("")){
                query_str+=" where (id like '%"+searchTerm+"%'";
                query_str+=" or first_name like '%"+searchTerm+"%'";
                query_str+=" or last_name like '%"+searchTerm+"%'";
                query_str+=" or agent_name like '%"+searchTerm+"%'";
                query_str+=" or title like '%"+searchTerm+"%')";
            }
            query_str+=" order by id desc";

            Query query = session.createQuery(query_str);
            query.setFirstResult(startIndex);
            query.setMaxResults(noOfRecordsPerPage);
            Integer noOfRecords = ((Long)session.createQuery("select count(*) " + query_str).uniqueResult()).intValue();
            quoteList = query.getResultList();
            int noOfPages = noOfRecords / 20;
            int reminderRecords = noOfRecords%20;

            if(noOfPages == 0)
            {
                    noOfPages = 1;
            }
            if(reminderRecords > 0 && noOfRecords > 20)
            {
                    noOfPages++;
            }

            ManageDetails.setQuoteList(quoteList);
            ManageDetails.setNoOfPages(String.valueOf(noOfPages));
            ManageDetails.setNoOfRecords(String.valueOf(noOfRecords));
            ManageDetails.setPageId(String.valueOf(""));
            ManageDetails.setNoOfTotalRecords(String.valueOf(session.createQuery(query_noOfTotalRecords).uniqueResult()));

            session.getTransaction().commit();
        }
        catch(Exception ex)
        {
            session.getTransaction().rollback();
            ex.printStackTrace();
        }
        return ManageDetails;
    }

You have permanent sort by Id statically set in the 您在ID中静态设置了永久按ID排序

query_str+=" order by id desc";

You need to pass sort column from your jQuery call, get the sort on server side, pass to the DAO and use it in the order by . 您需要通过jQuery调用传递sort列,在服务器端进行排序,传递给DAO并按order by

By the way your DAO has SQL injection vulnerability. 顺便说一下,您的DAO具有SQL注入漏洞。 Use params rather than concat SQL query string. 使用参数而不是concat SQL查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM