动态合并表格单元格:实现按部门统一PDF下载链接
Got it, let's fix this issue where your PDF download link repeats on every row. The core idea is to group your data by department first so we can render the link only once per department, even when the number of subjects per department changes dynamically. Here's a step-by-step implementation:
Step 1: Group Data by Department
First, we'll load all your ResultSet data into a map where each key is a department name, and the value is a list of all rows belonging to that department. This lets us know exactly how many rows each department has for the rowspan attribute.
<% // Initialize a map to hold grouped data: key = department name, value = list of rows Map<String, List<Map<String, String>>> deptGroupedData = new HashMap<>(); ResultSet rs = statement.executeQuery(selectQuery); while(rs.next()) { String ID = rs.getString("ID"); String NAME = rs.getString("NAME"); String DEPT = rs.getString("DEPT"); String SUBJECT = rs.getString("SUBJECT"); String REPORTS = rs.getString("REPORTS"); // Store current row data in a map Map<String, String> row = new HashMap<>(); row.put("ID", ID); row.put("NAME", NAME); row.put("DEPT", DEPT); row.put("SUBJECT", SUBJECT); row.put("REPORTS", REPORTS); // Add row to the corresponding department group if (!deptGroupedData.containsKey(DEPT)) { deptGroupedData.put(DEPT, new ArrayList<>()); } deptGroupedData.get(DEPT).add(row); } rs.close(); statement.close(); connection.close(); %>
Step 2: Render the Table with Merged PDF Links
Now we'll iterate over the grouped data. For each department, we'll render the PDF link only in the first row of the department, and use rowspan to make that cell span all rows of the department.
<table id="DataTable" class="table table-striped table-bordered table-responsive table-hover" cellspacing="0" width="100%" height="300px"> <tr style="color:white; background-color:#206aae;"> <th>ID</th> <th>NAME</th> <th>DEPT</th> <th>SUBJECT</th> <th>REPORTS</th> <th>PDF DOWNLOAD</th> </tr> <tbody> <% // Loop through each department group for (Map.Entry<String, List<Map<String, String>>> deptEntry : deptGroupedData.entrySet()) { String department = deptEntry.getKey(); List<Map<String, String>> deptRows = deptEntry.getValue(); int totalRowsInDept = deptRows.size(); // Loop through each row in the department for (int i = 0; i < deptRows.size(); i++) { Map<String, String> currentRow = deptRows.get(i); String id = currentRow.get("ID"); String name = currentRow.get("NAME"); String subject = currentRow.get("SUBJECT"); String reports = currentRow.get("REPORTS"); %> <tr> <td><%=id%></td> <td><%=name%></td> <td><%=department%></td> <td><%=subject%></td> <td><%=reports%></td> <% // Only render the PDF link in the first row of the department if (i == 0) { String pdfpath = path+"\\"+currentDateFolder+"\\Report_"+id+"_"+name+".pdf"; String filePDF_pathtodownload = "..\\upload\\"+currentDateFolder+"\\Report_"+id+"_"+name+".pdf"; String filename = "Report_"+id+"_"+name+".pdf"; %> <td rowspan="<%=totalRowsInDept%>"> <a href="file_pdfileDownload.jsp?currentDateFolder=<%=currentDateFolder%>&name=<%=name%>&filename=<%=filename%>&id=<%=id%>">PDF</a> </td> <% } %> </tr> <% } } %> </tbody> </table>
Key Notes:
- Sort Your SQL Query: Make sure your
selectQueryincludesORDER BY DEPTat the end. This ensures all rows from the same department are grouped together in the ResultSet, which is critical for the grouping logic to work correctly. - Adjust PDF Link Logic: If you want a single PDF that includes all reports for the department (instead of a single row's PDF), you'll need to modify the download link to pass the department name instead of individual ID/name, and update your
file_pdfileDownload.jspto generate a combined PDF for the department.
内容的提问来源于stack exchange,提问作者GodKnows




