如何隐藏表头并显示标题,并仅在所选日期包含数据时滚动 [英] How to hide the table headers and display the headers and scroll only if the selected date has data
问题描述
在我的应用程序中,我将使用数据选择器选择日期,如果在所选日期有数据,那么数据显示在表格中,问题是没有选择日期我最初看到表格的标题我不应该只有在有数据时我才会显示表格标题我应该如何显示标题
< tr>
< td align =center>
< label for =fileName>选择日期:< / label>
< input id =datepickername =pickedDate/> <峰; br />
< / td>
< / tr>
< tr>
< td align =center>
< INPUT TYPE =SUBMITVALUE =Get Uploaded Data>
< INPUT TYPE =RESET>
< / td>
< / tr>
< div style =overflow:scroll; height:500px; width:450%; overflow:auto>
< table border =1style =width:100%>
< tr>< th> Id< / th>< th>名称< / th>< th> FirstName< / th>< th> LastName< / th>< th>密码< /第><的第i;状态< /第>< / TR>
<%
连接conn = null;
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser();
ds.setPassword();
ds.setServerName();
ds.setDatabaseName();
conn = ds.getConnection();
int sumcount = 0;
Statement st;
try {
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
String Date = request.getParameter(pickedDate);
String query =选择Id,Name,PinNo,FirstName,LastName,密码,来自用户的状态,其中UploadedDate ='+ Date +';
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
< tr>< td><%= rs.getString(1)%>< / td>
< td><%= rs.getString(2)%>< / td>
< td><%= rs.getString(3)%>< / td>
< td><%= rs.getDate(4)%>< / td>
< td><%= rs.getString(5)%>< / td>
< td><%= rs.getString(6)%>< / td>
< td><%= rs.getString(7)%>< / td>
< td>< input type =buttonname =editvalue =编辑style =background-color:green; font-weight:bold; color:white;的onclick = editRecord(小于%= rs.getString(1)%>); >< / TD>
< / tr>
<%
}
%>
<%
}
catch(例外e){
e.printStackTrace();
}
%>
< / table>
< / div>
我尝试过:
我试过把代码放在
中( rs.next()){< TR><的第i;标识< /第><的第i;姓名< /第><的第i;姓< /第><的第i;名字< /第><的第i ;密码< / th>< th>状态< / th>< / tr>}
但是每个记录都会显示标题,这意味着有10个行然后相同的标题重复10次,但我应该只获得一次标题
这是因为你甚至在检查之前打印表的标题数据。如果您首先检查数据然后打印HTML文档,则可以轻松修复该问题。
< table border =1style =width:100%>
< tr>< th> Id< / th>< th>名称< / th>< th> FirstName< / th>< th> LastName< / th>< th>密码< / th>< th>状态< / th>< / tr>
您还需要将此部分纳入代码中,您需要检查是否存在是一个记录,然后打印出来。
// 加载连接,读取数据和检查光标中是否有数据
if (dataFound){
< tr>
< th> Id< / th>
< th>名称< / th>
< th> FirstName< / th>
< th> LastName< / th>
< th>密码< / th>
< th>状态< / th>
while (rs.next()){
// 在此处追加行
}
< / tr>
} 其他 {
// 什么都没找到。
< p>未找到任何记录。< / p>
}
这样,您将检查数据是否存在,如果存在,您将打印标题,否则您将跳过它并显示段落声明没有数据。
编辑
我只是保持事情简单,并根据你的代码,因为rs.next()
将用于检查是否有记录,所以可能需要先调用甚至if(dataFound)
的事情。因此,请进行适当的更改,以便此代码可以工作,逻辑将是相同的。
1.查询数据
2.检查数据是否存在
3.创建表格并填写数据
4.如果没有,请显示段落或任何您需要的内容。
这就是流量。 : - )
In my application i will be selecting the date using data picker and if there is data on that selected date then data is displayed in a table the problem is without selecting the date i am seeing the headers of the table initially i should not show the table headers only when there is data i should show the headers how can i do this
<tr> <td align="center"> <label for="fileName">Select Date: </label> <input id="datepicker" name="pickedDate"/> <br/> </td> </tr> <tr> <td align="center"> <INPUT TYPE="SUBMIT" VALUE="Get Uploaded Data"> <INPUT TYPE="RESET"> </td> </tr> <div style="overflow:scroll;height:500px;width:450%;overflow:auto"> <table border="1" style="width:100%"> <tr><th>Id</th><th>Name</th><th>FirstName</th><th>LastName</th><th>Password</th><th>Status</th></tr> <% Connection conn=null; SQLServerDataSource ds = new SQLServerDataSource(); ds.setUser(""); ds.setPassword(""); ds.setServerName(""); ds.setDatabaseName(""); conn = ds.getConnection(); int sumcount=0; Statement st; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String Date = request.getParameter("pickedDate"); String query = "select Id,Name,PinNo,FirstName,LastName,Password,Status from Users where UploadedDate='" + Date + "'"; st = conn.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><%=rs.getString(1)%></td> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getDate(4)%></td> <td><%=rs.getString(5)%></td> <td><%=rs.getString(6)%></td> <td><%=rs.getString(7)%></td> <td><input type="button" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:white;" onclick="editRecord(<%=rs.getString(1)%>);" ></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> </div>
What I have tried:
I had tried by placing the code in
while(rs.next()){<tr><th>Id</th><th>Name</th><th>FirstName</th><th>LastName</th><th>Password</th><th>Status</th></tr>}
but headers are getting displayed for every record that means if there are 10 rows then the same headers are getting repeated 10 time but i should get the headers only once
That is because you are printing the headers of the table before even checking for the data. Which can be easily fixed if you first check for the data and then print the HTML document.
<table border="1" style="width:100%"> <tr><th>Id</th><th>Name</th><th>FirstName</th><th>LastName</th><th>Password</th><th>Status</th></tr>
You need to bring this section into the code as well and you need to check if there is a record, then print it.
// Load the connection, read the data and check if there is data in the cursor if(dataFound) { <tr> <th>Id</th> <th>Name</th> <th>FirstName</th> <th>LastName</th> <th>Password</th> <th>Status</th> while (rs.next()) { // Append the rows here } </tr> } else { // Nothing found. <p>No records found.</p> }
This way, you will be checking if the data exists, and if it does you will print the headers otherwise you will skip it and show a paragraph stating there is no data.
Edit
I was just keeping things simple, and as per your code because thers.next()
will be used to check if there is a record or not, so perhaps it would be required to call before even theif(dataFound)
thing. So, make the appropriate changes please so that this code can work, the logic will be same.
1. Query for the data
2. Check if the data exists
3. Create a table and fill with the data
4. If not, show a paragraph, or whatever you need to.
That is the flow. :-)
这篇关于如何隐藏表头并显示标题,并仅在所选日期包含数据时滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!