简体   繁体   中英

How can I filter a table displayed using Expression Language in JSP?

Here's my code that I am using to display data from the database.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Book List</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/BookTracker" user="root" password="school" />

    <sql:query dataSource="${snapshot}" var="result">
SELECT * from BookTrackerSystem;
</sql:query>

    <table border="1" width="100%">
        <tr>
            <th>Book ID</th>
            <th>Book Name</th>
            <th>Book Author</th>
            <th>Book Genre</th>
            <th>Book Description</th>
            <th>Book Due Date</th>
            <th>Book Status</th>
            <th>Full Name</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td><c:out value="${row.id}" /></td>
                <td><c:out value="${row.bookName}" /></td>
                <td><c:out value="${row.bookAuthor}" /></td>
                <td><c:out value="${row.bookGenres}" /></td>
                <td><c:out value="${row.bookDesc}" /></td>
                <td><c:out value="${row.bookDueDate}" /></td>
                <td><c:out value="${row.bookStatus}" /></td>
                <td><c:out value="${row.fullname}" /></td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

What I want to do, using a simple textbox I want to be able to filter the table displayed using the code above, I believe I can do it using jQuery but is jQuery compatible with Expression language or is there any other method I can make use of?

Despite the fact almost nobody will support you in what you are doing :) .. I mean all is in JSP..

The exact answer is the following:

  1. Add an html form with a textbox.

  2. Add code that reads the textbox value as request.getParameter(). http://www.tutorialspoint.com/jsp/jsp_form_processing.htm

  3. Add the value from (2) into your query <sql:param value="..." /> and add where clause. Perhaps, you will need IF for two cases: a) filtering off - no text value and no where b) filtering on - text value exists and where used.

This will be a server-side processing (filtering) though.

If you want client-side filtering consider using DataTables http://www.datatables.net/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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