Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSorting对java类中的JTable列表进行排序_Java_Sorting - Fatal编程技术网

使用JSorting对java类中的JTable列表进行排序

使用JSorting对java类中的JTable列表进行排序,java,sorting,Java,Sorting,我有一个问题,我正在努力解决它没有运气,所以我想知道是否有人可以帮助我,任何帮助都将非常感谢 我有一个java类在前面的html页面中执行操作,如过滤、分页,现在我想用JSorting执行排序,但我做不到,我的java代码如下,我们目前正在进行过滤和分页。。但我已经尝试了许多方法来执行排序,但netbeans总是对我说不合适的方法。。有人知道我代码中的排序方法吗?请帮帮忙,提前谢谢你的努力。。这是我的代码,我已删除排序方法,原因是无法工作..: /** * Retrieves all th

我有一个问题,我正在努力解决它没有运气,所以我想知道是否有人可以帮助我,任何帮助都将非常感谢

我有一个java类在前面的html页面中执行操作,如过滤、分页,现在我想用JSorting执行排序,但我做不到,我的java代码如下,我们目前正在进行过滤和分页。。但我已经尝试了许多方法来执行排序,但netbeans总是对我说不合适的方法。。有人知道我代码中的排序方法吗?请帮帮忙,提前谢谢你的努力。。这是我的代码,我已删除排序方法,原因是无法工作..:

  /**
 * Retrieves all the topics (backend - admin side)
 *
 * @param jtStartIndex Indicates the first record of paging
 * @param jtPageSize Number of records to show in each page
 * @param topic
 * @param forumId
 * @param status
 * @param search
 * @return an instance of javax.ws.rs.core.Response
 */
@RolesAllowed({AllConstants.USER_ROLE_ADMIN})
@Path("/getTopics")
@GET
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response getTopics(@QueryParam("jtStartIndex") int jtStartIndex,
        @QueryParam("jtPageSize") int jtPageSize,
        @QueryParam("topic") String topic,
        @QueryParam("forum") int forumId,
        @QueryParam("state") int status,
        @QueryParam("jtSorting") String jtSorting,
        @QueryParam("search") int search) {
    try {
        EntityManager em = JpaUtils.getEntityManager();

        List<JforumTopic> topicEntities;

        //Filtering
        if (search == 1) {
            if (topic == null) {
                topic = "";
            }
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT t FROM JforumTopic t WHERE LOWER(t.topicTitle) LIKE :topicTitle ");

            if (forumId > 0) {
                sb.append(" AND t.forumId = :forumId ");
            }

            if (status > -1) {
                sb.append(" AND t.topicStatus = :topicStatus ");
            }

            Query query = em.createQuery(sb.toString());

            query.setParameter("topicTitle", "%" + topic.toLowerCase() + "%");

            if (forumId > 0) {
                query.setParameter("forumId", forumId);
            }

            if (status > -1) {
                query.setParameter("topicStatus", status);
            }

            topicEntities = query.getResultList();

        } else {
            TypedQuery<JforumTopic> query = em.createNamedQuery("JforumTopic.findAll", JforumTopic.class);
            topicEntities = query.getResultList();              
        }

       //Paging
        PagingHelper<JforumTopic> ph = new PagingHelper<JforumTopic>(jtStartIndex, jtPageSize, topicEntities);
        topicEntities = ph.getSublist();
        String jsonFinal = ph.getJson(topicEntities);
        return Response.status(Response.Status.OK).entity(jsonFinal).build();
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "ForumResource.getTopics() threw exception: ", ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

但是我不知道如何创建列表。。。请大家帮忙,谢谢。

这是我的代码,以防有人需要排序!!!!谢谢你的阅读

   /**
 * Retrieves all the topics (backend - admin side)
 *
 * @param jtStartIndex Indicates the first record of paging
 * @param jtPageSize Number of records to show in each page
 * @param topic
 * @param forumId
 * @param status
 * @param search
 * @return an instance of javax.ws.rs.core.Response
 */
@RolesAllowed({AllConstants.USER_ROLE_ADMIN})
@Path("/getTopics")
@GET
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response getTopics(@QueryParam("jtStartIndex") int jtStartIndex,
        @QueryParam("jtPageSize") int jtPageSize,
        @QueryParam("topic") String topic,
        @QueryParam("forum") int forumId,
        @QueryParam("state") int status,
        @QueryParam("jtSorting") String jtSorting,
        @QueryParam("search") int search) {
    try {
        EntityManager em = JpaUtils.getEntityManager();

        List<JforumTopic> topicEntities;            

        //Filtering
        if (search == 1) {
            if (topic == null) {
                topic = "";
            }
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT t FROM JforumTopic t WHERE LOWER(t.topicTitle) LIKE :topicTitle ");

            if (forumId > 0) {
                sb.append(" AND t.forumId = :forumId ");
            }

            if (status > -1) {
                sb.append(" AND t.topicStatus = :topicStatus ");
            }

            Query query = em.createQuery(sb.toString());

            query.setParameter("topicTitle", "%" + topic.toLowerCase() + "%");

            if (forumId > 0) {
                query.setParameter("forumId", forumId);
            }

            if (status > -1) {
                query.setParameter("topicStatus", status);
            }

            topicEntities = query.getResultList();

        } else {
            TypedQuery<JforumTopic> query = em.createNamedQuery("JforumTopic.findAll", JforumTopic.class);
            topicEntities = query.getResultList();              
        }

       //Sorting
        if (jtSorting != null && !jtSorting.isEmpty()) {
            if (jtSorting.equals("relatedId ASC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top1, JforumTopic top2) {
                        return top1.getRelatedId().compareTo(top2.getRelatedId());
                    }
                });                    
            }                
        if(jtSorting.equals("relatedId DESC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top1, JforumTopic top2) {
                        return top2.getRelatedId().compareTo(top1.getRelatedId());
                    }
                });
            }          
      if (jtSorting.equals("relatedTitle ASC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top3, JforumTopic top4) {
                        return top3.getRelatedTitle().compareTo(top4.getRelatedTitle());
                    }
                });                    
            }                
        if(jtSorting.equals("relatedTitle DESC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top3, JforumTopic top4) {
                        return top4.getRelatedTitle().compareTo(top3.getRelatedTitle());
                          }
                });
            }
        }

     // end of sorting

       //Paging
        PagingHelper<JforumTopic> ph = new PagingHelper<JforumTopic>(jtStartIndex, jtPageSize, topicEntities);
        topicEntities = ph.getSublist();
        String jsonFinal = ph.getJson(topicEntities);
        return Response.status(Response.Status.OK).entity(jsonFinal).build();
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "ForumResource.getTopics() threw exception: ", ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

/**
   /**
 * Retrieves all the topics (backend - admin side)
 *
 * @param jtStartIndex Indicates the first record of paging
 * @param jtPageSize Number of records to show in each page
 * @param topic
 * @param forumId
 * @param status
 * @param search
 * @return an instance of javax.ws.rs.core.Response
 */
@RolesAllowed({AllConstants.USER_ROLE_ADMIN})
@Path("/getTopics")
@GET
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response getTopics(@QueryParam("jtStartIndex") int jtStartIndex,
        @QueryParam("jtPageSize") int jtPageSize,
        @QueryParam("topic") String topic,
        @QueryParam("forum") int forumId,
        @QueryParam("state") int status,
        @QueryParam("jtSorting") String jtSorting,
        @QueryParam("search") int search) {
    try {
        EntityManager em = JpaUtils.getEntityManager();

        List<JforumTopic> topicEntities;            

        //Filtering
        if (search == 1) {
            if (topic == null) {
                topic = "";
            }
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT t FROM JforumTopic t WHERE LOWER(t.topicTitle) LIKE :topicTitle ");

            if (forumId > 0) {
                sb.append(" AND t.forumId = :forumId ");
            }

            if (status > -1) {
                sb.append(" AND t.topicStatus = :topicStatus ");
            }

            Query query = em.createQuery(sb.toString());

            query.setParameter("topicTitle", "%" + topic.toLowerCase() + "%");

            if (forumId > 0) {
                query.setParameter("forumId", forumId);
            }

            if (status > -1) {
                query.setParameter("topicStatus", status);
            }

            topicEntities = query.getResultList();

        } else {
            TypedQuery<JforumTopic> query = em.createNamedQuery("JforumTopic.findAll", JforumTopic.class);
            topicEntities = query.getResultList();              
        }

       //Sorting
        if (jtSorting != null && !jtSorting.isEmpty()) {
            if (jtSorting.equals("relatedId ASC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top1, JforumTopic top2) {
                        return top1.getRelatedId().compareTo(top2.getRelatedId());
                    }
                });                    
            }                
        if(jtSorting.equals("relatedId DESC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top1, JforumTopic top2) {
                        return top2.getRelatedId().compareTo(top1.getRelatedId());
                    }
                });
            }          
      if (jtSorting.equals("relatedTitle ASC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top3, JforumTopic top4) {
                        return top3.getRelatedTitle().compareTo(top4.getRelatedTitle());
                    }
                });                    
            }                
        if(jtSorting.equals("relatedTitle DESC")) {
                Collections.sort(topicEntities, new Comparator<JforumTopic>(){
                    @Override
                    public int compare(JforumTopic top3, JforumTopic top4) {
                        return top4.getRelatedTitle().compareTo(top3.getRelatedTitle());
                          }
                });
            }
        }

     // end of sorting

       //Paging
        PagingHelper<JforumTopic> ph = new PagingHelper<JforumTopic>(jtStartIndex, jtPageSize, topicEntities);
        topicEntities = ph.getSublist();
        String jsonFinal = ph.getJson(topicEntities);
        return Response.status(Response.Status.OK).entity(jsonFinal).build();
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "ForumResource.getTopics() threw exception: ", ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

/**