使用SQL数据库中的值过滤JSP上的表值

Filter table values on JSP with values from SQL database

本文关键字:JSP 过滤 SQL 使用 数据库      更新时间:2024-04-14

我正在尝试制作一个基于servlet的应用程序,该应用程序从SQL数据库中获取值,并将其显示在JSP上动态创建的表中,该表已经在运行。下一步是创建某种过滤器,只显示其中一列中具有特定值的表行。我的想法是添加一个下拉菜单,并在"问题"列中选择包含该项目的行。我认为这有点直截了当。问题是,要在dropdowm菜单中填充的值在SQL数据库的表"Problem"中;并且它的每一行都具有id和name属性。目前,我正在尝试使用name属性,并尝试将其与从我的查询(SQL Server 2008 R2)中获得的"Problem"列(

SELECT h.[name], c.[department], p.[name] AS problem, it.[name] AS interaction, ip.[title], ip.[date]
FROM [Database].[dbo].[Input] ip, [Database].[dbo].[Interaction] it, [Database].[dbo].[Problem] p, [Database].[dbo].[HelpdeskUser] h, [Database].[dbo].[Costumer] c
WHERE h.[id] = ip.[user_id]
AND it.[id] = ip.[interaction_id]
AND c.[id] = ip.[costumer_id]
AND p.[id] = ip.[problem_id]
ORDER BY date DESC";

),但是,很明显,我的头撞到墙上了,因为这不起作用。所以我想问你,你是否能以某种方式帮助我,如果我忽略了一些显而易见的东西,可以开启整个过程。

ViewInputServlet.java

public class ViewInputServlet extends GenericServlet {
    public static final String PROBLEMS = "problems";
    public static List<Problem> problems;
    private void setProblems(HttpServletRequest req, HttpServletResponse resp) throws EmptyResultSetException {
        Session session = HibernateUtilities.getSessionFactory().openSession();
        if (ProblemDAO.hasRecords(session)) {
            problems = ProblemDAO.selectProblems(session);
            req.setAttribute(PROBLEMS, problems);
        }
    }
}

ViewInput.jsp

<% List<Problem> problems = (List<Problem>) request.getAttribute(ViewInputServlet.PROBLEMS); %>
<div class="innerField">
    <table class="datatable" id="tableResults">
        <thead>
            <tr>
                <th>NAME</th>
                <th>DEPARTMENT</th>
                <th>PROBLEM</th> 
                <th>TITLE</th>
                <th>DATE</th>
            </tr>
        </thead>
        <%  for (Issue issue : (List<Issue>) request.getAttribute(ViewInputServlet.LIST)) {%>
        <tr>
            <td><%=issue.getName()%></td>
            <td><%=issue.getDepartment()%></td>
            <td id="prob_type"><%=issue.getProblem()%></td>
            <td><%=issue.getTitle()%></td>
            <td><%=issue.getDate()%></td>                
         </tr> 
         <%}%>        
    </table>
        <div class="label">Show by Problems:      </div>  
            <div class="field">
                <div class="ui-widget">
                    <select name="<%=ViewInputServlet.PROBLEMS%>" id="chooseProblems">
                        <%if (problems != null) {
                            for (Problem problem : problems) {%>
                            <option value="<%=problem.getName()%>"><%=problem.getName()%></option>
                            <%}
                    }%>
                </select> <input type="button" value="Reset" id="btn_reset" />
           </div>
       </div>
  </div>

("问题"列是这里的关键:这是我想要过滤值的列,这就是为什么我在之前的一次尝试中给了它一个ID,但失败了)

函数.js

$("#chooseProblems").change(function () {
    $("#tableResults").find("td").each(function () {
        if ($(this).text !== $("#chooseProblems").val())
            $(this).hide();
        else
            $(this).show();
    });
});

如果你需要更多的信息或对我的推理有疑问,请询问:)

java脚本中的字符串比较是否存在问题。应该使用match方法而不是!==。

另外,当您说$(this).text或$(this).hide或$(this).show时,实际上您指的是行的列,而不是行。