固定标题表 - AngularJs

时间:2016-06-07 15:12:46

标签: javascript angularjs

我对AngularJs比较陌生,我在桌面上实现固定标题时遇到了麻烦。我正在使用我在我的应用程序的其他部分使用的库的修改版本没有问题,但是在这个特定的表上它不起作用。我甚至尝试过两个单独的表,一个用于头部,一个用于主体,但由于可以使用Angular和Maximize动态,这不起作用。

js代码:

                     <div id="test"
                         class="panel-body" style="height: 222px; overflow-y: scroll;">
                            <table id="data-sources-table" class="table table-striped drag-drop" fixed-header-custom >
                                <thead>
                                <tr>
                                    <th style="position: relative">TestProperty</th>
                                    <th style="position: relative" ng-repeat="ds in model.dataSamples"
                                        style="line-height: 16px; vertical-align: top;">
                                        <span tooltip="{{ds.dsName}}"
                                              tooltip-placement="top">
                                            {{ds.dsName.slice(0, 20)}}&nbsp;
                                        </span>
                                        <button class="btn-graphic-only btn-remove" type="button"
                                                ng-show="ds"
                                                ng-model="singleModel"
                                                tooltip="Test data sample"
                                                tooltip-placement="left"
                                                ng-click="removeDs($index)">
                                        </button>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr ng-repeat="(property, linkedDs) in model.properties">
                                    <td ng-class="{'narrow-column': $first}"
                                        ng-style="highlightIfInDetails(model.properties[property])">
                                        <span>
                                            <ul style="margin-bottom: 0px; list-style-type: none; padding: 0px;">
                                                <li dnd-draggable="ds.dsProfile[property]"
                                                    dnd-effect-allowed="copy"
                                                    dnd-copied="dragEnd(property, ds.dsProfile[property])"
                                                    dnd-selected="dropzoneModels.selected = ds.dsProfile[property]">
                                                    <label class="btn-property" style="font-size: 100%;">
                                                        {{property}}
                                                        &nbsp;
                                                    </label>
                                                    <button class="btn-graphic-only btn-remove" type="button"
                                                            ng-model="singleModel"
                                                            tooltip="Remove property from schema"
                                                            tooltip-placement="right"
                                                            ng-click="removeProperty(property)">
                                                    </button>
                                                    <div ng-show="modifySchemaMode === true">
                                                        <!--keep the following on the same line to prevent spaces between values-->
                                                        {{model.properties[property]["main-type"]}}<span
                                                        ng-show="model.properties[property]['main-type']">:{{model.properties[property]["detail"]["detail-type"]}}</span>
                                                    </div>
                                                </li>
                                            </ul>
                                        </span>
                                    </td>
                                    <td ng-repeat="ds in model.dataSamples"
                                        ng-style="highlightIfInDetails(ds.dsProfile[property])">
                                        <span class="btn-property">
                                            <label class="btn-property"
                                                   style="font-size: 100%; font-weight: normal;
                                                          -webkit-user-select: none; -moz-user-select: none;
                                                          -ms-user-select: none;"
                                                   unselectable="on"
                                                   single-click="showInDetails1(ds, property, false)"
                                                   ng-dblclick="showInDetails2(ds, property);">
                                                <div ng-show="ds.dsProfile[property]['original-name']">
                                                    <label style="font-size: 100%;">{{ds.dsProfile[property]['original-name']}}</label>
                                                </div>
                                                <!--keep the following on the same line to prevent spaces between values-->
                                                {{ds.dsProfile[property]["main-type"]}}<span
                                                ng-show="ds.dsProfile[property]['main-type']">:{{ds.dsProfile[property]["detail"]["detail-type"]}}</span>
                                                <span class="btn-merge-indicator"
                                                      ng-show="ds.dsProfile[property]['merged-into-schema']">
                                                    &nbsp;&nbsp;&nbsp;
                                                </span>
                                            </label>
                                            <span class="btn-use-check"
                                                  ng-show="ds.dsProfile[property]['used-in-schema']">
                                                &nbsp;&nbsp;&nbsp;
                                            </span>
                                            <br>
                                            <select name="altNameSelected" id="altNameSelected"
                                                    ng-options="option.name for option in ds.dsProfile[property]['match-names'].availableOptions track by option.id"
                                                    ng-model="ds.dsProfile[property]['match-names'].selectedOption"
                                                    ng-show="ds.dsProfile[property]['match-names'].availableOptions.length > 0"
                                                    ng-change="changeMatchedProperty(ds, property)">
                                            </select>
                                        </span>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>

库:

/**
 * AngularJS fixed header scrollable table directive
 * @author Jason Watmore <jason@pointblankdevelopment.com.au> (http://jasonwatmore.com)
 * @version 1.2.0
 */
(function () {
    angular
        .module('anguFixedHeaderTableCustom', [])
        .directive('fixedHeaderCustom', fixedHeaderCustom);

    fixedHeaderCustom.$inject = ['$timeout'];

    function fixedHeaderCustom($timeout) {
        return {
            restrict: 'A',
            link: link
        };

        function link($scope, $elem, $attrs, $ctrl) {
            var elem = $elem[0];

            // wait for data to load and then transform the table
            $scope.$watch(tableDataLoaded, function(isTableDataLoaded) {
                if (isTableDataLoaded) {
                    transformTable();
                }
            });

            function tableDataLoaded() {
                // first cell in the tbody exists when data is loaded but doesn't have a width
                // until after the table is transformed
                var firstCell = elem.querySelector('tbody tr:first-child td:first-child');
                return firstCell && !firstCell.style.width;
            }

            function transformTable() {
                // reset display styles so column widths are correct when measured below
                angular.element(elem.querySelectorAll('thead, tbody, tfoot')).css('display', '');

                // wrap in $timeout to give table a chance to finish rendering
                $timeout(function () {
                    // set widths of columns
                    angular.forEach(elem.querySelectorAll('tr:first-child th'), function (thElem, i) {

                        var tdElems = elem.querySelector('tbody tr:first-child td:nth-child(' + (i + 1) + ')');
                        var tfElems = elem.querySelector('tfoot tr:first-child td:nth-child(' + (i + 1) + ')');

                        var columnWidth = tdElems ? tdElems.offsetWidth : thElem.offsetWidth;
                        if (tdElems) {
                            tdElems.style.width = columnWidth + 'px';
                        }
                        if (thElem) {
                            thElem.style.width = columnWidth + 'px';
                        }
                        if (tfElems) {
                            tfElems.style.width = columnWidth + 'px';
                        }
                    });

                    // set css styles on thead and tbody
                    angular.element(elem.querySelectorAll('thead, tfoot')).css('display', 'block');

                    angular.element(elem.querySelectorAll('tbody')).css({
                        'display': 'block',
                        'height': $attrs.tableHeight || 'inherit',
                        'overflow': 'auto'
                    });

                    // reduce width of last column by width of scrollbar
                    var tbody = elem.querySelector('tbody');
                    var scrollBarWidth = tbody.offsetWidth - tbody.clientWidth;
                    if (scrollBarWidth > 0) {
                        // for some reason trimming the width by 2px lines everything up better
                        scrollBarWidth -= 2;
                        var lastColumn = elem.querySelector('tbody tr:first-child td:last-child');
                        lastColumn.style.width = (lastColumn.offsetWidth - scrollBarWidth) + 'px';
                    }
                });
            }
        }
    }
})();

0 个答案:

没有答案