programing

ui-select angular에서 선택한 옵션 지우기

telebox 2023. 3. 8. 21:02
반응형

ui-select angular에서 선택한 옵션 지우기

ui-select 박스의 선택된 값을 각도별로 지우는 방법을 아는 사람?

셀렉트 박스에 작은 x가 있는 select2의 기능을 원합니다.select2의 허가 클리어 메서드는 없는 것 같습니다.

select2 테마를 사용하는 경우,allow-clear옵션ui-select-match당신을 위해 이 일을 하는 지시문입니다.오른쪽에는 x 가 표시되고, 클릭하면 클리어 할 수 있습니다.https://github.com/angular-ui/ui-select/wiki/ui-select-match

간단한 예:

<ui-select-match allow-clear="true" placeholder="Select or search a country in the list...">
  <span>{{$select.selected.name}}</span>
</ui-select-match>

작업 예: http://plnkr.co/edit/DbbUE68QlNLjx97pBZ56?p=preview

현재 부트스트랩 또는 선택 테마를 사용할 때는 이 작업이 작동하지 않습니다.

선택 항목을 표시할 때 작은 X 버튼을 추가할 수 있습니다.

<ui-select-match placeholder="Select or search a country in the list...">
  <span>{{$select.selected.name}}</span>
  <button class="clear" ng-click="clear($event)">X</button>
</ui-select-match>

그런 다음 클릭 이벤트가 버블하지 않도록 하고 열린 이벤트를 트리거합니다.그리고 선택한 모델을 덮어쓰면 필드를 지울 수 있습니다.

$scope.clear = function($event) {
   $event.stopPropagation(); 
   $scope.country.selected = undefined;
};

여기 있습니다.http://plnkr.co/edit/qY7MbR

부트스트랩을 사용하는 경우 설계 관점에서 fa-remove 아이콘을 사용할 수도 있습니다.

또한 사용 편의성 측면에서 제거 아이콘을 왼쪽에 정렬할 수 있습니다.

JS:

<ui-select-match placeholder="Select or find...">
    <button class="clear-btn" ng-click="clear($event)">
        <span class="fa fa-remove"></span>
    </button>
    <span class="clear-btn-offset">{{$select.selected}}</span>
</ui-select-match>

CSS:

.select2 .clear-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 5px 10px;
    position: absolute;
    left: -2px;
    top: 1px;
}

.clear-btn-offset {
    position: absolute;
    left: 25px;
}

지시 코드:

$scope.clear = function($event) {
   $event.stopPropagation();
   // Replace the following line with the proper variable
   $scope.country.selected = undefined;
};

참고: 이 경우 태그 부착 및 태그 부착-label="false"를 사용한 경우 허용 지우기 기능이 작동하지 않습니다.

커스텀 클리어 기능

HTML 코드

<ui-select-match placeholder=”Enter table…”>
 <span>{{$select.selected.description || $select.search}}</span>
 <a class=”btn btn-xs btn-link pull-right” ng-click=”clear($event, $select)”><i class=”glyphicon glyphicon-remove”></i></a>
</ui-select-match>

컨트롤러 액션코드

function clear($event, $select){ 
 //stops click event bubbling
 $event.stopPropagation(); 
 //to allow empty field, in order to force a selection remove the following line
 $select.selected = undefined;
 //reset search query
 $select.search = undefined;
 //focus and open dropdown
 $select.activate();
}

언급URL : https://stackoverflow.com/questions/26389542/clear-selected-option-in-ui-select-angular

반응형