angular2.x - 5.x 的下拉多选框选择组件 ng2 -- ng5。最近在学angular4,经常在交流群看见很多人问 下拉多选怎么做。。。 今天就随便写的个。

组件源码 百度云   链接:https://pan.baidu.com/s/1SHV4_ccNPXyqpyEQZ8QQGg 密码: 2uv4

下面贴代码:

界面

引用  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:;
}
.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:;
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:;
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:;
right:;
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:;
}
.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:;
}
.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 下拉多选框选择组件的更多相关文章

  1. 使用jQuery为文本框、单选框、多选框、下拉框、下拉多选框设值及返回值的处理

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. 品优购商城项目(二)AngularJS、自动代码生成器、select2下拉多选框

    品优购商城想项目第二阶段 AngularJS.自动代码生成器.select2下拉多选框 完成了课程第三天.第四天的的任务. 1.学习了AngularJs前端的mvc分层思想,js部分分成control ...

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

    一.前言 由于最近的项目需要具有复选功能,但过多的复选框会影响界面布局和美观,因而想到把 PyQt5 的下拉列表和复选框结合起来,但在 PyQt5 中并没有这样的组件供我们使用,所以想要自己实现一个下 ...

  4. 我的第一个jquery插件:下拉多选框

    <!DOCTYPE HTML> <html> <head> <title> New Document </title> <meta n ...

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

    模拟实现下拉多选框 效果如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  6. jquery--获取多选框的值、获取下拉多选框的值

    获取多选框的值 var packageCodeList=new Array(); $('#server_id:checked').each(function(){ packageCodeList.pu ...

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

    Bootstrap select多选下拉框赋值 success: function (data) { var oldnumber = new Array(); $.each(data, functio ...

  8. DevExpress下拉多选框 CheckComboboxEdit、CheckedListBoxControl

    CheckComboboxEdit //清空项            checkedComboBoxEdit1.Properties.Items.Clear(); //自定义数组            ...

  9. Extjs下拉多选框

    //------录入时间,下拉列表框------ var inputTimeRow = new Ext.data.Record.create([ { name : 'value' },{ name : ...

随机推荐

  1. Visual Studio 起始页面关闭新闻等

    [工具]->[选项]->[环境]->[启动] 将“下载内容的时间间隔”一项的勾选去掉,然后确定保存.这样,就大功告成啦

  2. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  3. C++关联式容器的排序准则

    stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...

  4. XDU 1031

    #include<stdio.h> #define maxn 1005 int c[maxn][maxn]; int gcd(int a,int b){ ?a:gcd(b,a%b); } ...

  5. Loadrunner自带协议分析工具:Protocol Advisor

    录制脚本之前,选对协议很关键,否则错误的协议会导致Virtual User Generator 录制不到脚本,或录制的脚本不完整,有些应用可能需要选择多个协议才能完整的记录 客户端与服务器端的请求. ...

  6. ASP.NET Core EF 查询获取导航属性值,使用Include封装

    // 引用 using Microsoft.EntityFrameworkCore; // 摘要: // Specifies related entities to include in the qu ...

  7. Lua 可控下标数组遍历

    , , , , , , , , , , , } , , } local j = 1 while i <= #aaa do if bbb[j] == aaa[i] then -- 如果 b下标元素 ...

  8. linux下多线程之pthread_detach(pthread_self())

    写个碰到的问题,记录下自己的技术之路点滴pthread_detach(pthread_self())linux线程执行和windows不同,pthread有两种状态joinable状态和unjoina ...

  9. Python实例1—格式化输出

    老男孩教学学习笔记: 实例1:格式化输出 # Author:Alex Li name = input("name:") # raw_input 2.x input 3.x # in ...

  10. 企业和开发人员究竟该如何适应web标准?

    以下几点注意事项仅供参考:完善的前期策划和分析完善的前期逻辑模型以及项目规范性文档的制定尽可能将行政性干预移到策划阶段(按照国内的情况,做到这一点可能很困难)尽可能向后兼容,在项目规范性文档制定阶段对 ...