在AngularJS下拉菜单中显示更多

bq3bfh9z  于 2022-11-21  发布在  Angular
关注(0)|答案(1)|浏览(216)

我有一个使用AngularJS创建的选择下拉列表。我们设置了一个显示前5个项目的限制,剩下的5个项目应该对用户可见。当他点击名为“海岸more”的第5个项目时(5)”,而不失去焦点,这意味着不关闭下拉菜单,剩下的5个项目必须显示,如果达到最大高度,滚动条应该自动出现。我尝试了这个,但找不到解决方案。
下面是我创建一个选择下拉列表的代码

<div ng-if="items.groups.length>5"
    class="bx--select-input__wrapper" style="width: 100%">
    <select carbon-select class="dropDown-text-overflow bx--select-input"
        ng-model="selectedGroup"
        ng-change="selectNegotiatedGroup(selectedGroup)">
        <option class="bx--select-option"
            data-ng-repeat="groupName in items.groups|limitTo:5"
            value="{{groupName.label}}" ng-selected="{{groupName.selected}}">{{groupName.label}}</option>
        <option ng-selected="false" value="show_more" ng-style="hideVisibility">Show More({{items.groups.length-5}})</option>
    </select>
</div>
wooyq4lh

wooyq4lh1#

// create some vars for pagination control
var displayIndex = 0;
var displayCount = $scope.items.groups.length < 5 ? $scope.items.groups.length : 5;

// create another array to display partial results
$scope.itemsToDisplay = $scope.items.groups.slice(displayIndex, displayCount);

// create a method to move pagination forward when called
$scope.showMore = function() {
  // calculate the index and max item count for the next page
  displayIndex++;
  var max = displayIndex * 5;
  if (max > $scope.items.groups.slice.length - 1) max = $scope.items.groups.slice.length - 1;
  // appends the array's calculated range to the array that is displayed in the view
  $scope.itemsToDisplay = $scope.itemsToDisplay.concat($scope.items.groups.slice(displayIndex, max - displayIndex));
   
}

在视图中,必须显示itemsToDisplay数组并去掉过滤器:

<div ng-if="itemsToDisplay.length>5"
    class="bx--select-input__wrapper" style="width: 100%">
    <select carbon-select class="dropDown-text-overflow bx--select-input"
        ng-model="selectedGroup"
        ng-change="selectNegotiatedGroup(selectedGroup)">
        <option class="bx--select-option"
            data-ng-repeat="groupName in itemsToDisplay"
            value="{{groupName.label}}" ng-selected="{{groupName.selected}}">{{groupName.label}}</option>
        <option ng-selected="false" value="show_more" ng-style="hideVisibility" ng-click="showMore()">Show More({{items.groups.length-5}})</option>
    </select>
</div>

相关问题