Angular ui-select. Add selection separately from selection - repeat

I need to display an array of options in ui-select and another user element. How to add this custom item to the current directive? I cannot insert this custom item into an array because there is a different user interface for displaying this custom item

+4
source share
1 answer

Found a solution: added a custom directive for the ui-select element, which is manually added at the bottom of the list of elements.

Code (it contains some additional attributes and functions than described in the section. I placed it as is):

.directive('customOption', function($timeout){
  return {
    restrict: 'A',
    link: function(scope, el, attrs){

      var options = JSON.parse(attrs.customOption);

      var buttonText = options.buttonText;
      var func = options.click;
      var type = options.type;
      var condition = options.showIf;
      var template = "<div class='custom-option-container'><button type='button' class='btn btn-primary'><span class='glyphicon glyphicon-plus-sign'></span>" + buttonText + "</button></div>";

      //if condition evaluated to true or no condition
      if(scope.$eval(condition) || !condition){

        el.find('li.ui-select-choices-group').append(template);
        el.find('ul.ui-select-choices').removeAttr('ng-show');

        //watch and
        //remove ng-hide class to display this custom item even if there are no more items
        scope.$watch(function(){
          return el.find('ul.ui-select-choices').hasClass('ng-hide');
        }, function(newVal){
          if(newVal){
            $timeout(function() {
              el.find('ul.ui-select-choices').removeClass('ng-hide');
            });
          }
        });

        el.find('div.custom-option-container button').bind('click', function(){
          scope[func].apply(null, [type]);
        })
      };
    }
  };
})

HTML:

            <ui-select ng-model="asset.category" custom-option='{"buttonText": "Add New Category", "click" : "showAddModal", "type": "category"}'>
                <ui-select-match>{{ $select.selected.categoryName }}
                </ui-select-match>
                <ui-select-choices repeat="category in categories | filter: $select.search">
                  <span ng-bind-html="category.categoryName | highlight: $select.search"></span>
                </ui-select-choices>
              </ui-select>
+1
source

Source: https://habr.com/ru/post/1583840/


All Articles