java - JSP 中的可滚动表

标签 java css jsp

我正面临在 JSP 中创建可滚动表(带固定头)的问题。请帮忙。有什么需要可以告诉我

<div class = "scrollable">
 <table class = "table table-condensed table-hover">
    <thead>
    <tr class= "info">
        <th>Prod id</th>
        <th>Name </th>
        <th>Manufacturer</th>
        <th>Quantity</th>
        <th>Purchased Price</th>
        <th>Selling Price</th>
        <th>Weight/Packet</th>
        <th>Type</th>
        <th>Category</th>
     </tr>
     </thead>
     <tbody >
    <%  while(rs.next()){ %>

    <tr class = "default">

    <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><%= rs.getString(7) %> </td>
    <td><%= rs.getString(8) %> </td>
    <td><%= rs.getString(9) %> </td>

    </tr>
    </tbody>
    <% } %>
    </table>
 </div>

其中“rs”是结果集

最佳答案

试试这个,它会起作用。

// Change the selector if needed
var $table = $('table.scroll'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

// Adjust the width of thead cells when window resizes
$(window).resize(function() {
    // Get the tbody columns width array
    colWidth = $bodyCells.map(function() {
        return $(this).width();
    }).get();
    
    // Set the width of thead columns
    $table.find('thead tr').children().each(function(i, v) {
        $(v).width(colWidth[i]);
    });    
}).resize(); // Trigger resize handler
table.scroll {
    width: 100%; /* Optional */
    /* border-collapse: collapse; */
    border-spacing: 0;
    border: 2px solid black;
}

table.scroll tbody,
table.scroll thead { display: block; }

thead tr th { 
    height: 30px;
    line-height: 30px;
    /*text-align: left;*/
}

table.scroll tbody {
    height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
}

tbody { border-top: 2px solid black; }

tbody td, thead th {
    width: 20%; /* Optional */
    border-right: 1px solid black;
}

tbody td:last-child, thead th:last-child {
    border-right: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table class="scroll">
    <thead>
        <tr>
            <th>Head 1</th>
            <th>Head 2</th>
            <th>Head 3</th>
            <th>Head 4</th>
            <th>Head 5</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Lorem ipsum dolor sit amet.</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
    </tbody>
</table>

关于java - JSP 中的可滚动表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42887324/

相关文章:

java - 为什么我得到相同 JSP 方法的客户端和服务器端输出?

java - 一点java中的类设计,一点模式?

html - 调整浏览器大小时文本重叠

java - 如何在运行时将 JSR-045 SMAP 信息添加到 Java 堆栈跟踪?

java - 如何从 servlet 获取响应到 jsp?

java - Realm 中的 allObjects() 方法是否已弃用?

java - 使用 ResultSet 时 mysql 内存 (RAM) 使用量增加?

font-awesome 的 css 样式无法正常工作

html - 我需要为特定的类和 id 应用 css

java - 将值从 <% %> 传递到 &lt;script&gt; (JSP)