javascript 以角度向表格添加滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28350930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:50:24  来源:igfitidea点击:

Add scrollbar to table in angular

javascripthtmlcssangularjs

提问by Anush

I have following code for table in angular. I want a vertical scroll bar only for table body (table rows excluding header) how can I do that?

我有以下角度表代码。我想要一个仅用于表格主体的垂直滚动条(表格行不包括标题)我该怎么做?

Since All rows are generated by ng-repeat. I don't know how to add overflow style.

由于所有行都是由 ng-repeat 生成的。我不知道如何添加溢出样式。

html:

html:

            <div class="nu-table">
                <div class="nu-table-row nu-header">
                    <div class="nu-table-cell">A</div>
                    <div class="nu-table-cell" style="width: 33%">B</div>
                    <div class="nu-table-cell" style="width: 34%">C</div>
                </div>
                <div class="nu-table-row nu-striped pointer-cursor" ng-repeat=" map in mapList">
                    <div class="nu-table-cell"  ng-bind="map.A"></div>
                    <div class="nu-table-cell">{{map.B}}</div>
                    <div class="nu-table-cell" ng-bind="map.C"></div>
                </div>
            </div>

Following is the CSS content:

以下是 CSS 内容:

.nu-border-table{
    border: solid 1px #ccc; 
}

.nu-border{
    border: solid 1px #ccc;
}


.nu-table{
    background-color: #fff;
    padding: 5px;
    overflow: scroll;
    display: table;
    table-layout: fixed;
    width: 100%;
}

.nu-table-row{
    display: table-row;
    position: relative;
    width: 100%;
}

.nu-table-row:hover{
    background-color: #cee6fa;
}


.nu-table-row.nu-striped.selected{
    background-color: #cee6fa;
}

.nu-table-row:last-child{
    border-bottom: none;
}

.nu-margin{
    margin:5px;
}

.nu-table-cell{
    display: table-cell;
    border-right: solid 1px #ccc;
    border-top: solid 1px #ccc;
    min-height: 2em;
    padding-top: .3em;
    position: relative;
    word-wrap: break-word;
    padding-left: 2px;
}

.nu-table-cell:last-child{
    border-right: none;
}

.nu-striped:nth-child(even) {
    background-color: #f9f9f9;
}

.nu-striped:nth-child(even):hover{
    background-color: #cee6fa;
}

.nu-header {
    background-color: #dedede;
    border-bottom: solid 2px #bebebe;
    font-weight: bold;
}

回答by Thierry

Try to add a div around the rows (not tested):

尝试在行周围添加一个 div(未测试):

    <div class="nu-table">
        <div class="nu-table-row nu-header">
            <div class="nu-table-cell">A</div>
            <div class="nu-table-cell" style="width: 33%">B</div>
            <div class="nu-table-cell" style="width: 34%">C</div>
        </div>
        <div class="nu-table-body">
           <div class="nu-table-row nu-striped pointer-cursor" ng-repeat=" map in mapList">
              <div class="nu-table-cell"  ng-bind="map.A"></div>
              <div class="nu-table-cell">{{map.B}}</div>
              <div class="nu-table-cell" ng-bind="map.C"></div>
          </div>
        </div>
    </div>

and css (set the height you want)

和 css(设置你想要的高度)

.nu-table-body {
    overflow-y:auto;
    max-height:500px;
}

回答by Prateek Rai

You can place two div where 1st div (Header) will have transparent scroll bar and 2nd div will be have data with visible/auto scroll bar. Sample has angular code snippet for looping through the data.

您可以放置​​两个 div,其中第一个 div(标题)将具有透明滚动条,第二个 div 将具有可见/自动滚动条的数据。示例具有用于循环数据的角度代码片段。

Below code worked for me -

下面的代码对我有用 -

<div id="transparentScrollbarDiv" class="container-fluid" style="overflow-y: scroll;">
    <div class="row">
        <div class="col-lg-3 col-xs-3"><strong>{{col1}}</strong></div>
        <div class="col-lg-6 col-xs-6"><strong>{{col2}}</strong></div>
        <div class="col-lg-3 col-xs-3"><strong>{{col3}}</strong></div>
    </div>
</div>
<div class="container-fluid" style="height: 150px; overflow-y: auto">
    <div>
        <div class="row" ng-repeat="row in rows">
            <div class="col-lg-3 col-xs-3">{{row.col1}}</div>
            <div class="col-lg-6 col-xs-6">{{row.col2}}</div>
            <div class="col-lg-3 col-xs-3">{{row.col3}}</div>
        </div>
    </div>
</div>

Additional style to hide header scroll bar -

隐藏标题滚动条的附加样式 -

<style>
        #transparentScrollbarDiv::-webkit-scrollbar {
            width: inherit;
        }

        /* this targets the default scrollbar (compulsory) */

        #transparentScrollbarDiv::-webkit-scrollbar-track {
            background-color: transparent;
        }

        /* the new scrollbar will have a flat appearance with the set background color */

        #transparentScrollbarDiv::-webkit-scrollbar-thumb {
            background-color: transparent;
        }

        /* this will style the thumb, ignoring the track */

        #transparentScrollbarDiv::-webkit-scrollbar-button {
            background-color: transparent;
        }

        /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

        #transparentScrollbarDiv::-webkit-scrollbar-corner {
            background-color: transparent;
        }

        /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
    </style>