一種table超出高度自動出滾動條的解決方案

在平常的開發過程當中,咱們可能會遇到這樣一種需求,在指定高度內顯示table,超太高度時表格出滾動條。css

讓咱們帶着這個問題,一塊兒來探討吧!html

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml" ng-app="app">
 3 <head>
 4     <title></title>
 5     <link href="../css/bootstrap.css" rel="stylesheet" />
 6     <link href="../css/index.css" rel="stylesheet" />
 7 </head>
 8 <body ng-controller="tableCtrl">
 9 <div>
10     <div>
11         <table class="table table-striped table-bordered table-hover table-condensed">
12             <thead>
13                 <tr>
14                     <th>Id</th>
15                     <th>Name</th>
16                     <th>Email</th>
17                     <th>操做</th>
18                 </tr>
19             </thead>
20             <tbody>
21                 <tr ng-repeat="person in persons">
22                     <td ng-bind="person.id"></td>
23                     <td ng-bind="person.name"></td>
24                     <td ng-bind="person.email"></td>
25                     <td ng-click="persons.remove(person)" class="del-person">刪除</td>
26                 </tr>
27             </tbody>
28         </table>
29     </div>
30 </div>
31 <script src="../js/angular.js"></script>
32 <script src="../js/index.js"></script>
33 </body>
34 </html>
html
 1 var app = angular.module("app", []);
 2 
 3 app.controller("tableCtrl", [
 4     '$scope', function($scope) {
 5         $scope.persons = [];
 6         for (var i = 0; i < 15; i++) {
 7             var index = i + 1;
 8             var person = {
 9                 id: index,
10                 name: 'person' + index,
11                 email: 'person' + index + '@qq.com'
12             };
13             $scope.persons.push(person);
14         }
15 
16         //刪除person
17         $scope.deletePerson= function(person) {
18             $scope.persons.remove(person);
19         }
20     }
21 ]);
22 
23 /**
24  *刪除數組指定下標或指定對象
25  */
26 Array.prototype.remove = function (obj) {
27     for (var i = 0; i < this.length; i++) {
28         var temp = this[i];
29         if (!isNaN(obj)) {
30             temp = i;
31         }
32         if (temp == obj) {
33             for (var j = i; j < this.length; j++) {
34                 this[j] = this[j + 1];
35             }
36             this.length = this.length - 1;
37         }
38     }
39 };
js

 

先看下效果,怎樣git

貌似沒什麼問題,若是我給table外面的div,設置一個小點的高度呢?github

那麼問題又來了,紅線是div的區域,很明顯看到,table的調試超出了div的高度。bootstrap

我想實現當table的高度超出div時,出現滾動條,而不是直接超出,這樣太暴力了。數組

那把設置div的overflow:auto;看看效果怎樣。app

貌似能夠了,睜大你的24k鈦金眼看看,會發現滾動條下拉框時,thead不見了,列少還能夠知道哪一個列是什麼,列多的話就,不看列頭,就不知道列名是什麼。ide

那我就將tbody固定高度,overflow:auto;看看效果怎樣。this

thead是固定不動了,tbody也出現了滾動條,可是thead與tbody的列寬度沒對齊,這也太醜了吧。spa

唉!白忙了這麼長時間了...

當作來只有請教大牛了。。。

大牛曰:"不會作,還不會模仿嗎?"。

因而打開了KendoUi官網,找到了這個

這不就是我要的效果嗎,早說嘛。

看了下生成的代碼結構

它用的是兩個div內套了兩個table,一個放thead,一個放thead,看起來像是一個table。

因而,我按這種結構修改代碼。看看效果。

thead與tbody的列寬沒對齊,這不是我想要的結果。

設置下寬度

有滾動條時,仍是有點沒對齊,很明顯,刪除幾條數據試試。

沒滾動條時是對齊的。滾動條佔用了dvTbody的寬度,這裏上面table比下面的table寬出一個滾動條的寬度17px。咱們能夠用腳本控制,來解決這個問題。

當taTbody的高度超過其父元素時,設置dvThead的padding-right:17px.

差很少了吧,有那麼回事了。

最終效果

大功告成,可隨意刪除、增長來看效果。

先寫到這。

代碼下載https://github.com/dengjianjun/MyBlog/tree/master/MyBlog/Pages

若是以爲對你有幫助,請點個贊,謝謝!

不足與錯誤之處,敬請批評指正!

相關文章
相關標籤/搜索