Rodolfo Dias
Rodolfo Dias

Reputation: 101

Filter table values on JSP with values from SQL database

I'm trying to make an servlet-based application that gets values from a SQL database and presents them in a dynamically created table on a JSP, which is already working. The next step is to create some sort of filter that only exhibits the table lines that have a certain value in one of its columns. My idea was to add a dropdown menu and select the lines with that item in the "Problem" column. I believe that to be a bit straightforward. The catch is that the values to fill in that dropdowm menu are in a table "Problem" on the SQL database; and each of its rows have the id and name attributes. Currently, I'm trying to go with the name attribute and trying to compare it with the "Problem" column that is obtained from my query (SQL Server 2008 R2) (

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";

), but, obviously, I'm hitting my head against the wall because this isn't working. So I'd like to ask you if you can help me somehow with this and if I'm overlooking something obvious that could unlock this whole process.

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>

(the column "Problem" is the key here: it's the one I want to filter values by, and that's why I've given it an ID, in a previous attempt that failed)

Functions.js

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

If there's some more info you require or doubts on my reasoning, ask away :)

Upvotes: 0

Views: 3631

Answers (1)

TimeTraveler
TimeTraveler

Reputation: 1263

Could there be a problem with your string comparison in java script. should you be using match method instead of !==.

Also when you say $(this).text or $(this).hide or $(this).show , you are actually referring to column of row not the row.

Upvotes: 1

Related Questions