0

我有一个页面editpatient.jsp,其中包括一个页面patientlist.jsp。当您运行 editpatient.jsp 时,它会显示数据库中存在的所有记录。我有一个下拉菜单和一个搜索字段来指定搜索。因此,当我运行 editpatient.jsp 时,它会以存储在 DB 中的方式显示所有记录。所以我想根据名称和显示对其进行排序。所以请告诉我如何做同样的事情。当您点击姓名或电子邮件或城市时,它会相应地排序

病人列表.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="DB.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.evenRow{
                height: 50px;
                background-color: white;
                text-transform: none;
                text-shadow: none;
                color: black;
        }

        .evenRow:hover 
        {
            background-color: #C2FEF0;
        }

            .row{
                height: 50px;
                background-color: #E4E6E6;
                text-transform: none;
                text-shadow: none;
                color: black;
                }

            .row:hover {
            background-color: #C2FEF0;
            }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>

<table style="border-collapse: collapse;overflow-x: scroll; width:97%">
                            <tr style="background-color:grey;height:50px"> 
                                <th style="min-width: 100px">
                                    NAME    
                                </th>
                                <th style="min-width: 100px">
                                    CITY    
                                </th>
                                <th style="min-width: 100px">
                                    LAST VISIT  
                                </th>
                                <th style="min-width: 100px">
                                    MOBILE  
                                </th>
                                <th style="min-width: 100px">
                                    EMAIL   
                                </th>
                                <th style="min-width: 100px">
                                    STATUS  
                                </th>
                                <th style="min-width: 100px">
                                    VIEW    
                                </th>
                                <th style="min-width: 100px">
                                    EDIT    
                                </th>
                            </tr>
            <%
                DataBaseConnection db = new DataBaseConnection();
                Connection con = db.connet();
                PreparedStatement pt = con
                        .prepareStatement("select * from Patient");
                ResultSet rs = pt.executeQuery();

                String searchBy = request.getParameter("searchBy");
                String searchElement = request.getParameter("searchElement");

                int count = 0;
                int index = -1;
                boolean name = false;
                if ("city".equalsIgnoreCase(searchBy))
                    index = 9;//change the index to the index of the city
                else if ("firstName".equalsIgnoreCase(searchBy))
                    index = 1;
                else if ("lastName".equalsIgnoreCase(searchBy))
                    index = 2;
                else if ("name".equalsIgnoreCase(searchBy)) {
                    index = 1;
                    name = true;
                }

                while (rs.next()) {
                    if (searchElement == null
                            || ((searchElement.equals(rs.getString(index)) && !name) || (name && searchElement
                                    .equalsIgnoreCase(rs.getString(index) + " "
                                            + rs.getString(index + 1))))) {

                    if (count++ % 2 == 0) {
            %>

                                <tr class="evenRow" >
                                    <td>
                                        <%=rs.getString(1)%>
                                    </td>
                                    <td>
                                        <%=rs.getString(2)%>                                        
                                    </td>
                                    <td>
                                        <%=rs.getString(3)%>
                                    </td>
                                    <td>
                                        <%=rs.getString(4)%>                                        
                                    </td>
                                    <td>
                                        <%=rs.getString(5)%>                                        
                                    </td>
                                    <td>
                                        <%=rs.getString(6)%>                                        
                                    </td>
                                    <td>
<form action="getPatientDetails.jsp"><input type="hidden" name="hidden" value="<%=count%>"/><input type="submit" value="view"></form>
                                    </td>
                                    <td>
                                        <a onclick="renderEdit(<%out.println("edit");%>)"><%
                                            out.println("edit");
                                        %></a>
                                    </td>
                                </tr>   
                <%
                        } else {
                    %>
                                <tr class="row">
                                    <td>
                                                                                <%=rs.getString(1)%>

                                    </td>
                                    <td>
                                                                                <%=rs.getString(2)%>

                                    </td>
                                    <td>
                                                                                <%=rs.getString(3)%>

                                    </td>
                                    <td>
                                                                                <%=rs.getString(4)%>

                                    </td>
                                    <td>
                                                                                <%=rs.getString(5)%>

                                    </td>
                                    <td>
                                                                                <%=rs.getString(6)%>

                                    </td>
                                    <td>
                                        <a onclick="renderView(<%out.println("view");%>)"><%
                                            out.println("view");
                                        %></a>
                                    </td>
                                    <td>
                                        <a onclick="renderEdit(<%out.println("edit");%>)"><%
                                            out.println("edit");
                                        %></a>
                                    </td>
                                </tr>
                            <%
                                }
                    }
                }
                            %>
                        </table>
</body>
</html>

编辑病人.jsp

    <%@ page import="java.util.*" %>
    <!DOCTYPE html 
         PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
        <head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
    <script>
    $(function() {
    $( "#datepicker" ).datepicker();
    });
        </script>
        <script type="text/javascript">
var request;

function getRequestObject()
{
    if (window.ActiveXObject) 
    {
        return(new ActiveXObject("Microsoft.XMLHTTP"));
    } 
    else if (window.XMLHttpRequest) 
    {
        return(new XMLHttpRequest());
    }
    else {
        return(null);
    }
}

function sendRequest() 
{
    request = getRequestObject();
    request.onreadystatechange = handleResponse;
    var address =  "patientList.jsp?searchBy=" + document.getElementById("searchBy").value + "&searchElement="+ document.getElementById("searchElement").value;
    request.open("GET", address, true);
    request.send(null);
}

function handleResponse() 
{
    if (request.readyState == 4 && request.status == 200)
    {
        document.getElementById("table").innerHTML = request.responseText;
    }
}
</script>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Edit Patient</title>
        <link rel="stylesheet" type="text/css" href="styles.css" />
    </head>
    <body>
        <form id="f1" name="f1" method="post" onsubmit="ccheck();" >
        <script>
          $(document).ready(function() {
            $("#datepicker").datepicker();
          });
        </script>
        <section id="page" > <!-- Defining the #page section with the section tag -->
            <header > <!-- Defining the header section of the page with the appropriate tag -->
                    <hgroup align="center">
                        <h3>Edit Patient</h3>
                    </hgroup>
            </header>
            <section id="articles"> <!-- A new section with the articles -->
                <!-- Article 1 start -->
                <div class="line"></div>  <!-- Dividing line -->
                <article id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
                    <div class="articleBody clear">
                        search:
                        <select id="searchBy">
                            <option value="lastName">Last Name</option>
                            <option value="firstName">First Name</option>
                            <option value="name">Name</option>
                            <option value="city">City</option>
                        </select>
                        <input id="searchElement"/> 
                        <input type="button" value="Search" onclick="sendRequest();"/>
                    </div>
                </article>
                        <div id="table" align="center">
                            <jsp:include page="patientList.jsp" />
                        </div>
                </article>
            </section>
            <footer> <!-- Marking the footer section -->
               <div class="line"></div>
               <a href="#" class="up">Go UP</a>
            </footer>   
        </section> <!-- Closing the #page section -->
            <!-- JavaScript Includes -->
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
            <script src="jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
            <script src="script.js"></script>
      </form>
      </body>
    </html>
4

4 回答 4

1

看看这个链接是否对你有帮助。

http://tympanus.net/codrops/2009/10/03/33-javascript-solutions-for-sorting-tables/

http://www.allmyscripts.com/Table_Sort/

如果您已经尝试过任何东西,也请告诉我们

于 2013-04-23T06:18:57.213 回答
0

1.首先将下拉/搜索值存储在模型类中(使用setter)。2.当您触发查询以从数据库中获取详细信息时,附加存储在模型类中的下拉/搜索值(使用 getter)。3.从数据库中获取值后渲染 dataTable 。建议:请遵循任何一种 MVC 架构(如 Spring MVC 架构)以避免项目的复杂性。谢谢。

于 2013-04-23T05:13:24.857 回答
0

这并不完全是问题的答案。
尝试像jqGrid这样的网格,它可以处理sorting,searching等。

于 2013-04-23T07:14:17.960 回答
0

ASFAIK,您的问题的解决方案是,您可以在 jsp 代码中使用 jquery,这样您就可以找到所有库并包含在其中。无需担心排序和编辑。Jquery 具有Data Tables内置的 API 来对列出的表中的数据进行排序,它可以编辑表中的数据。 参考编辑 参考排序 jsp页面中如何使用数据表

于 2013-04-23T04:51:43.127 回答