ЦЕЛЬ
Я хочу, чтобы элемент выделился красным, если элемент найден в списке обязательных элементов selectedMandatoryItems.
ОПИСАНИЕ
- items: список элементов, отображаемых с использованием select с разрешенным множественным выбором
- vm.selectedItems: список выбранных элементов
- vm.selectedMandatoryItems: список выбранных обязательных элементов.
- vm.selectedChosenItems: список множественного выбора элементов (используется только в ng-модели)
- vm.addOrRemoveMandatoryItems (vm.selectedChosenItems): этот метод позволяет удалить или добавить выбранные выбранные элементы в список selectedMandatoryItems.
<select class="form-control" size="60" id="field_item" multiple name="item"
ng-dblclick="vm.addOrRemoveMandatoryItem(vm.selectedChosenItems)"
ng-model="vm.selectedChosenItems"
ng-options="item as item.libelle for item in vm.selectedItems|orderBy:'libelle' track by item.id">
</select>
TRY
Я пробовал несколько вещей, включая использование директив: Как использовать ng-класс в select с ng-options .
Мне удалось использовать директиву с ее обычным назначением (например, проверка на id).
<select class="form-control" size="60" id="field_item" multiple name="item"
ng-dblclick="vm.addOrRemoveMandatoryItem(vm.selectedChosenItems)"
ng-model="vm.selectedChosenItems"
ng-options="item as item.libelle for item in vm.selectedItems|orderBy:'libelle' track by item.id"
>
options-class="{ 'lightRed' : id == 48}">
</select>
angular.module('app')
.directive('optionsClass', function ($parse) {
return {
require: 'select',
link: function (scope, elem, attrs, ngSelect) {
// get the source for the items array that populates the select.
//I need the string after the keyword 'in'
debugger;
var parts = attrs.ngOptions.split(' ');
var optionsSourceStr = parts[parts.indexOf('in') + 1];
// use $parse to get a function from the options-class attribute
// that you can use to evaluate later.
var getOptionsClass = $parse(attrs.optionsClass);
scope.$watchCollection(optionsSourceStr, function (items) {
scope.$$postDigest(function () {
// when the options source changes loop through its items.
angular.forEach(items, function (item, index) {
// evaluate against the item to get a mapping object for
// for your classes.
var classes = getOptionsClass(item),
// also get the option you're going to need. This can be found
// by looking for the option with the appropriate index in the
// value attribute.
option = elem.find('option[value="' + item.id + '"]');
debugger;
// now loop through the key/value pairs in the mapping object
// and apply the classes that evaluated to be truthy.
angular.forEach(classes, function (add, className) {
if (add) {
debugger;
angular.element(option).addClass(className);
}
});
});
});
});
}
};
});
Мне не удается изменить его, чтобы передать список vm.selectedMandatoryItems
и проверить наличие элемента из vm.selectedItems
внутри vm.selectedMandatoryItem
, чтобы можно было выделить товар красного цвета.
Не могли бы вы мне помочь, пожалуйста?
Спасибо