简体   繁体   中英

Setting Width of Typeahead Drop Down in AngularJS project

I've been wrestling with the Bootstrap UI Typeahead control. I am trying to set the width of the drop down box at run time. The last SO question I asked dealt with setting the width of elements at runtime. While answered properly, the answer does not work in the context of the Typeahead directive for some reason. Currently, I am using the Typeahead control in my own directive, which is defined like this:

.directive('myDirective', function () {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      showLinks: '=?',
      query: '='
    },
    templateUrl: '/directives/my-directive.html',
    controller: function ($scope) {
      if (angular.isUndefined($scope.showLinks)) {
        $scope.showLinks = true;
      }

      $scope.getLocation = function(l) {
    var searchField = element.find('input');
        var width = searchField[0].offsetWidth;

        var dropdown = $element.find('.dropdown-menu');
        angular.element(dropdown[0]).css('width', (width + 'px'));    
      };
    }
  };
});

my-directive.html looks like this:

<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      {{showLinks}} <!-- Never renders -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>
</div>

How do I set the width of the dropdown menu that appears to be the same width as the textbox in my directive? My textbox is a different size on different screens. That's why I do not just hard-code a value.

An interesting side effect of how the typeahead directive position works, let's you do this with a simple template overwrite. The active difference is: ", width: position.width+'px'"

of course the effect is global.

angular.module("template/typeahead/typeahead-popup.html").run(["$templateCache", function($templateCache) {
  $templateCache.put("template/typeahead/typeahead-popup.html",
    "<ul class=\"dropdown-menu\" ng-show=\"isOpen()\" ng-style=\"{top: position.top+'px', left: position.left+'px', width: position.width+'px'}\" style=\"display: block;\" role=\"listbox\" aria-hidden=\"{{!isOpen()}}\">\n" +
    "    <li ng-repeat=\"match in matches track by $index\" ng-class=\"{active: isActive($index) }\" ng-mouseenter=\"selectActive($index)\" ng-click=\"selectMatch($index)\" role=\"option\" id=\"{{match.id}}\">\n" +
    "        <div typeahead-match index=\"$index\" match=\"match\" query=\"query\" template-url=\"templateUrl\"></div>\n" +
    "    </li>\n" +
    "</ul>\n" +
    "");
}]);

Referring to the answer to this SO question , you can try to wrap the results inside a div and then assign the required width to it. Just to illustrate, have set the width of the div to 800px .

<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      <div style="width:800px;">{{showLinks}}</div>
      <a>
        <span ng-bind-html="match.label | typeaheadHighlight:query"></span>
      </a>
    </script>
</div>

I managed to get it working by setting up a watcher on a variable assigned to typeahead-is-open.

In the directive template

<input type="text" ng-model="asyncSelected" placeholder="Type something" uib-typeahead="address for address in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" typeahead-is-open="typeaheadIsOpen" class="form-control">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
<div ng-show="noResults">
  <i class="glyphicon glyphicon-remove"></i> No Results Found
</div>

In the directive controller

$scope.$watch('typeaheadIsOpen', function(newVal, oldVal) {
  if (newVal == oldVal) {
    return;
  }
  if (newVal) {
    var searchField = angular.element.find('input');
    var width = searchField[0].offsetWidth;

    var dropdown = angular.element.find('.dropdown-menu');
    angular.element(dropdown[0]).css('width', (width + 'px'));
  }
});

 .drp .dropdown-menu{ left: 8px !important; width: calc(100% - 15px); } 
 <div style="position: relative;" class="drp"> <input type="text" ng-model="customSelected" placeholder="Custom template" uib-typeahead="state as state.Value for state in $ctrl.dimObj.Text | filter:{Value:$viewValue}" typeahead-template-url="customTemplate.html" class="form-control" > </div> 

Angular js type head result set width same as input size,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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