简体   繁体   English

如何为表格设置垂直和水平滚动条?

[英]How to set vertical and horizontal scrollbar for table?

I have a table under div and i want to set horizontal and vertical scrollbar according to that table我在 div 下有一个表格,我想根据该表格设置水平和垂直滚动条

I am unable to set vertical and horizontal scrollbar according to window size, I want to set scrollbar for my table, when the table goes out of window size then i should be able to go through the table by using scrollbar我无法根据 window 大小设置垂直和水平滚动条,我想为我的表格设置滚动条,当表格超出 window 大小时,我应该能够通过表格使用滚动条 Z34D1F91FB2E514B8576FAB1A75A89A6B

here is the code-这是代码-

            var $container = $("#container");
            var $button = $("<button>" + 'xyz' + "</button>");
            $container.prepend($button);
            var table = $("<table>");
            table.append($("<tr><th>column1</th><th>column2</th></tr>"));

           // Button click handler..
           $button.on("click", function(e) {
           e.preventDefault();
           for (var i = 0; i < 2; i++) {
           var row = $('<tr ><td>' + 'data' + '</td><td>' + "" + '</td> 
           </tr>');

           table.append(row);

           for (var j = 0; j < 2; j++) {

           var row = $('<tr><td>' + "" + '</td><td>' + 'data' + '</td> 
           </tr>');
           table.append(row);


          $("#table1").html(table);
          }}
          });

div content- div内容-

             <div id="container">
             <div id="table1">
             </div>

             </div>

full code- https://jsfiddle.net/gaurav10022/h7eqb209/12/完整代码- https://jsfiddle.net/gaurav10022/h7eqb209/12/

I want the table scrollbar to get adjust by window size, that is my table scrollbar should behave according to webpage window size.我希望表格滚动条通过 window 大小进行调整,即我的表格滚动条应根据网页 window 大小进行调整。

Add set default width of table and set overflow:scroll to it, Working example below,添加表格的设置默认宽度并设置overflow:scroll到它,下面的工作示例,

 var $container = $("#container"); // Loop through items in JSON data.. var $button = $("<button>" + 'xyz' + "</button>"); $container.prepend($button); var table = $("<table>"); table.append($("<tr><th>column1</th><th>column2</th></tr>")); // Button click handler.. $button.on("click", function(e) { e.preventDefault(); for (var i = 0; i < 2; i++) { // Replace row HTML.. //parent row var row = $('<tr ><td>' + 'data' + '</td><td>' + "" + '</td></tr>'); table.append(row); for (var j = 0; j < 2; j++) { //child row var row = $('<tr><td>' + "" + '</td><td>' + 'data' + '</td></tr>'); table.append(row); $("#table1").html(table); }} }); // Show table if it's not already visible..
 #table2 { margin-top: 20px; border-collapse: collapse; } #container{ height:300px; overflow:scroll; width:150px; } #table1 { margin-top: 20px; border-collapse: collapse; } #table1 th { //border: 1px solid black; text-align: left; } #table1 td { //border: 1px solid black; text-align: left; } #table1 tr { background-color: #f2f2f2; } #table2 th { //border: 1px solid black; text-align: left; } #table2 td { //border: 1px solid black; text-align: left; } #table2 tr { background-color: #f2f2f2; } button { margin-left: 15px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="table1"> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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