1. 程式人生 > >angular2.x 下拉多選框選擇組件

angular2.x 下拉多選框選擇組件

下拉 round ice osi 界面 poi top iou open

angular2.x - 5.x 的下拉多選框選擇組件 ng2 -- ng5。最近在學angular4,經常在交流群看見很多人問 下拉多選怎麽做。。。 今天就隨便寫的個。

組件源碼 百度雲 鏈接: https://pan.baidu.com/s/1dEHwKmt 密碼: mhta

下面貼代碼:

界面

技術分享圖片

技術分享圖片

引用 selectList 是 下拉框的數據列表 redSelList() 方法是獲取 選擇完成後的數據

<app-select-checkbox [itemList]="selectList" (selListOut)="redSelList($event)"></app-select-checkbox
>

    // 初始化下拉框數據列表 放在 ngOnInit 裏
this.selectList = [];
      for(let i = 1; i<= 30; i++){
        this.selectList.push({
          id: i,
          name: "選項"+ i
        })
      }


// 獲取傳遞過來的數組
redSelList(event){
    console.log(event);
    this.selList = event;
}

html

<div class="select-checkbox-div"
[ngClass]="{ ‘selectOpen‘ : isSelectOpen }"> <div class="select-checkbox-show clear-float" (click)="clickSelect()"> <div class="show-data">{{ selectedName }}</div> <i class="fa-select"></i> </div> <div class="select-checkbox-content"> <
div class="select-checkbox-list"> <div class="select-checkbox-item" *ngFor="let item of itemList"> <div>{{i}}</div> <div class="input-checkbox-div"><input type="checkbox" [checked]="isCheck(item)" (click)="clickItem($event,item)" /></div> <div class="item-name" (click)="checkItem($event, item)">{{ item.name }}</div> </div> </div> </div> </div>

ts

import { Component, OnInit, Input, Output, EventEmitter } from ‘@angular/core‘;
import * as $ from ‘jquery‘;
@Component({
  selector: ‘app-select-checkbox‘,
  templateUrl: ‘./select-checkbox.component.html‘,
  styleUrls: [‘./select-checkbox.component.css‘]
})
export class SelectCheckboxComponent implements OnInit {
  @Input() itemList: Array<any>;
  @Output() selListOut = new EventEmitter();
  public selected: Array<any>;
  public selectName: Array<any>;
  public selectItemList: Array<any>;
  public selectedName: string;
  public isSelectOpen: boolean;
  constructor() { }

  ngOnInit() {
    this.selected = [];
    this.selectName = [];
    this.selectItemList = [];
    this.isSelectOpen = false;
    this.selectedName = "下拉選擇";
    let thisT = this;
    document.addEventListener("click", (e) =>{
      var target = $(e.target); 
      if(target.closest(‘.select-checkbox-div‘).length == 0){ 
        thisT.isSelectOpen = false;
      }
    })
  }
  // 點擊了顯示框( 是否打開下拉框 )
  clickSelect(){
    this.isSelectOpen = !this.isSelectOpen;
  }
  // 點擊整塊div執行選擇
  checkItem(e, item){
    const ele = e.target;
    const checkbox = ele.previousElementSibling.firstElementChild;
    const action = (checkbox.checked ? ‘remove‘ : ‘add‘);
    this.updateSelected(action, item);
  }
  //   點擊input時執行
  clickItem(e, item) {
    const checkbox = e.target;
    const action = (checkbox.checked ? ‘add‘ : ‘remove‘);
    this.updateSelected(action, item);
  }
  // 用來判斷input 的checked
  isCheck(item) {
    return this.selected.findIndex(value => value == item.id) >= 0;
  }
  //  執行增加、刪除
  private updateSelected(action, item) {
    if (action == ‘add‘ && this.selected.findIndex(value => value == item.id) == -1) {
      this.selected.push(item.id);
      this.selectName.push(item.name);
      this.selectItemList.push(item);
    }
    if (action == ‘remove‘ && this.selected.findIndex(value => value == item.id) != -1) {
      this.selectItemList.splice(this.selected.findIndex(value => value == item.id), 1);
      this.selectName.splice(this.selected.findIndex(value => value == item.id), 1);
      this.selected.splice(this.selected.findIndex(value => value == item.id), 1);
    }
    if(this.selectName.length === 0){
      this.selectedName = "下拉選擇";
    }else{
      this.selectedName = this.selectName.join(",");
    }
    this.selListOut.emit(this.selectItemList);
  }
}

css:

input[type="radio"], input[type="checkbox"]{
    margin: 0;
}
.clear-float:after{
    content: "";
    display: block;
    clear: both;
}
.select-checkbox-item:hover{
    background-color: #eee;
}
.select-checkbox-item{
    cursor: pointer;
    -moz-user-select: none;
    -ms-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}
.input-checkbox-div{
    float: left;
    height: 18px;
    box-sizing: content-box;
}
.input-checkbox-div input[type=checkbox]{
    outline: none!important;
    -webkit-appearance: none;
    -moz-appearance: none;
    line-height: 18px;
    vertical-align: top;
    margin: 0;
    padding: 10px;
}
.input-checkbox-div input[type=checkbox]:before{
    content: url(../../assets/images/checkbox1.png);
    display: block;
}
.input-checkbox-div input[type=checkbox]:checked:before{
    content: url(../../assets/images/checkbox2.png);
    display: block;
}
.item-name{
    line-height: 18px;
    padding: 10px 0;
}
.check-item{
    float: left;
    padding: 1px 0 1px 10px;
    position: relative;
    border: 1px solid #ccc;
    border-radius: 4px;
    margin-right: 5px;
}
.check-close{
    float: right;
    width: 20px;
    height: 20px;
    line-height: 1;
    padding: 2px;
    box-sizing: border-box;
    background-color: #fff;
    cursor: pointer;
    text-align: center;
}
.select-checkbox-div {
    position: relative;
}
.select-checkbox-content{
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: 34px;
    max-height: 300px;
    overflow-y: auto;
    overflow-x: hidden;
    background-color: #fff;
    border-color: #ccc;
    border-width: 0 1px 1px 1px;
    border-style: solid;
    border-radius: 0 0 4px 4px;
    z-index: 10;
}
.selectOpen .select-checkbox-content{
    display: block;
}
.selectOpen .select-checkbox-show{
    border-width: 1px 1px 0 1px;
    border-radius: 4px 4px 0 0;
}
.select-checkbox-show{
    padding: 6px 18px 6px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    width: 100%;
    height: 34px;
    position: relative;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.fa-select{
    display: block;
    border-color: #888 transparent transparent transparent;
    border-style: solid;
    border-width: 6px 5px 0 5px;
    right: 5px;
    top: 14px;
    position: absolute;
    z-index: 10;
}
.show-data{
    width: 100%;
    overflow-y: hidden;
    overflow-x: auto;
    white-space: nowrap;
}
.show-data::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.show-data::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0);
        background-color: rgba(0,0,0,0.01);
}


    .show-data::-webkit-scrollbar-track:hover {
        background-color: rgba(0,0,0,0.01);
    }

    .show-data::-webkit-scrollbar-track:active {
        background-color: rgba(0,0,0,0.05);
    }

.show-data::-webkit-scrollbar-thumb {
    background-color: rgba(0,0,0,0.2);
}

.show-data:hover::-webkit-scrollbar-thumb {
    background-color: rgba(0,0,0,0.2);
}

.show-data::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0,0,0,0.4);
}

.show-data::-webkit-scrollbar-thumb:active {
    background: rgba(0,0,0,0.6)
} 


.select-checkbox-content::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.select-checkbox-content::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0);
    background-color: rgba(0,0,0,0.01);
}


.select-checkbox-content::-webkit-scrollbar-track:hover {
    background-color: rgba(0,0,0,0.01);
}

.select-checkbox-content::-webkit-scrollbar-track:active {
    background-color: rgba(0,0,0,0.05);
}

.select-checkbox-content::-webkit-scrollbar-thumb {
    background-color: rgba(0,0,0,0.2);
}

.select-checkbox-content:hover::-webkit-scrollbar-thumb {
    background-color: rgba(0,0,0,0.2);
}

.select-checkbox-content::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0,0,0,0.4);
}

.select-checkbox-content::-webkit-scrollbar-thumb:active {
    background: rgba(0,0,0,0.6)
} 

angular2.x 下拉多選框選擇組件