angular2.x 下拉多选框选择组件

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)
} 
时间: 2024-10-06 14:29:09

angular2.x 下拉多选框选择组件的相关文章

自定义实现 PyQt5 下拉复选框 ComboCheckBox

一.前言 由于最近的项目需要具有复选功能,但过多的复选框会影响界面布局和美观,因而想到把 PyQt5 的下拉列表和复选框结合起来,但在 PyQt5 中并没有这样的组件供我们使用,所以想要自己实现一个下拉复选框,主要就是继承 QComboBox 类,然后将复选框 QCheckBox 加入其中,并实现相应的功能. 最终实现的下拉复选框效果如下: 二.代码实现 1.主要方法 在 PyQt5 中,有几个主要的方法需要了解一下,方法名称和对应的含义如下: QtWidgets.QComboBox.setVi

JS下拉复选框的实现

<html>   <head>     <script src="jquery-1.7.2.min.js"></script>     <script src="jquery.easyui.min.js" ></script>     <link   rel="stylesheet" href="css/themes/metro/easyui.css"

自己用ul模拟实现下拉多选框,

模拟实现下拉多选框 效果如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="../js/plugins/layui/css/lay

selectpicker下拉多选框ajax异步或者提前赋值=》默认值

Bootstrap select多选下拉框赋值 success: function (data) { var oldnumber = new Array(); $.each(data, function (i) { oldnumber.push(data[i].id); }); $('#editcolor .selectpicker').selectpicker('val', oldnumber);//默认选中 $('#editcolor .selectpicker').selectpicker

Mutiselect下拉复选框(保存和设置默认选中项)

HTML代码 <asp:DropDownList ID="ddlWarehouseIds" runat="server" CssClass="ddl"></asp:DropDownList> <input type="hidden" name="<%#Eval("WarehouseNames")%>" id="hid_<%#Eva

下拉复选框

$('#beerId').combobox({ url: '/index.php/Admin/Beer/getBeer', valueField: 'id', textField: 'text', panelHeight: 'auto', multiple: true, formatter: function (row) { var opts = $(this).combobox('options'); return '<input type="checkbox" class=&

c# 下拉多选的实现

1.首先是个TextBox <asp:TextBox ID="txtREFERRINGDOC" Width="130" runat="server" CssClass="txt" onfocus="this.blur();" onclick="showDiv('divREFERRINGDOC','txtREFERRINGDOC');">▼</asp:TextBox>

ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(一)

ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中,这个问题分一下几步走 1.将弹出框真分页后复选框选择的数据保存. 2.将弹出框保存的数据传到父页面上. 3.将数据在父页面上显示. 4.点击保存将信息存入数据库中. 首先来第一步将弹出框真分页后复选框选择的数据保存. 思路很简单就是先真分页,然后在点击下一页的时候扫描这一页所有点击复选框的数据保存到一个变量数组中.下面就是主要代码: ASP代码: <%@ Page Language="C#" AutoEventWire

下拉多选

文件见附件: 使用方法: 1.前台代码 <%--下拉列表 --%> <link href="../Styles/jquery-ui.css" rel="stylesheet" type="text/css" /> <link href="../Styles/jquery.multiselect.css" rel="stylesheet" type="text/css&