因为工作原因,需要使用到checkbox list多选项功能。

一直在尝试在checkbox组件中添加NgModel的属性,但是只能在单个checkbox复选框上使用,checkbox list就没办法了。

好吧,其实是想差了。

因为是对checkbox的div添加ngFor的循环,所以每个checkbox都相当于list中的item,直接在item的属性,多加一个checked就好了,然后使用选中的list时filter checked==true就好了。

checkbox-list.component.html

 <div *ngIf="list" class="form-row">
<div class="col-{{colNum}} mb-2" *ngFor="let item of list">
<div class="form-check abc-checkbox abc-checkbox-primary">
<input class="form-check-input" type="checkbox" [value]="item.id" [(ngModel)]="item.checked" (change)="changeSelected()" id="{{name}}_{{item.id}}">
<label class="form-check-label" for="{{name}}_{{item.id}}">
{{item.name}}
</label>
</div>
</div>
</div>

checkbox-list.component.ts

 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { CheckboxItem } from '../../model/checkbox';
import { NgModel } from '@angular/forms';
import { filter } from 'rxjs/operator/filter'; @Component({
selector: 'app-checkbox-list',
templateUrl: './checkbox-list.component.html',
styleUrls: ['./checkbox-list.component.css']
})
export class CheckboxListComponent implements OnInit {
@Input() list: CheckboxItem[];
@Input() name: string;
@Input() colNum: number = 6;
@Output("selectChange") onChange: EventEmitter<any> = new EventEmitter<any>(); constructor() { } ngOnInit() {
}
changeSelected() {
let filters = this.list.filter(x => x.checked);
let ids = [] as any[];
for (var i = 0; i < filters.length; i++) {
ids.push(filters[i].id);
} this.onChange.emit({ value: ids, name: this.name });
}
}

使用的时候,直接在module中添加引用:

 import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms'; import { CheckboxListComponent } from '../components/checkbox-list/checkbox-list.component';
......
export const routes = [
{ path: '', component: OrderBuyDataComponent, pathMatch: 'full' }
]; @NgModule({
imports: [FormsModule
, CommonModule
, ...],
declarations: [CheckboxListComponent
, ...],
providers: []
})
export class OrderModule {
static routes = routes;
}

因为写的比较仓促,所以一些代码还没整理好。

Angular5中提取公共组件之checkbox list的更多相关文章

  1. Angular5中提取公共组件之radio list

    上一篇说到了Checkbox List的公共组件提取,现在说一下Radio List的公共组件提取. Radio List组件提取起来很方便,不想Checkbox那么复杂. radio-list.co ...

  2. webpack学习笔记--提取公共代码

    为什么需要提取公共代码 大型网站通常会由多个页面组成,每个页面都是一个独立的单页应用. 但由于所有页面都采用同样的技术栈,以及使用同一套样式代码,这导致这些页面之间有很多相同的代码. 如果每个页面的代 ...

  3. webpack 多页面支持 & 公共组件单独打包

    webpack - 多页面/入口支持 & 公共组件单独打包 webpack系列目录 webpack 系列 一:模块系统的演进 webpack 系列 二:webpack 介绍&安装 we ...

  4. webpack 多页面|入口支持和公共组件单独打包--转载

    转载自:http://www.jb51.net/article/117490.htm 本篇主要介绍:如何自动构建入口文件,并生成对应的output:公共js库如何单独打包. 多入口文件,自动扫描入口. ...

  5. 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之 Http 请求客户端

    一个技术汪的开源梦 —— 目录 想必大家在项目开发的时候应该都在程序中调用过自己内部的接口或者使用过第三方提供的接口,咱今天不讨论 REST ,最常用的请求应该就是 GET 和 POST 了,那下面开 ...

  6. Net Core 的公共组件之 Http 请求客户端

    Net Core 的公共组件之 Http 请求客户端 想必大家在项目开发的时候应该都在程序中调用过自己内部的接口或者使用过第三方提供的接口,咱今天不讨论 REST ,最常用的请求应该就是 GET 和 ...

  7. SSIS中循环遍历组件[Foreach Loop Container]

    背景 每月给业务部门提取数据,每个分公司都要提取一般,先跑SQL,再粘贴到Excel中,然后发邮件给相关的人员.费时费力,还容易粘贴错位.因此,需要通过一个程序完成这些步骤.我首先想到的是通过SSIS ...

  8. Vue(三十)公共组件

    以 分页 组件为例:(根据自己具体业务编写) 1.pagination.vue <template> <!-- 分页 --> <div class="table ...

  9. Vue(小案例_vue+axios仿手机app)_公共组件(路由组件传参)

    一.前言                    1.公共轮播图的实现                    2.组件传参,公共组件的实现 二.主要内容 1.公共轮播图的实现 (1)分析:当渲染不同的轮 ...

随机推荐

  1. 高性能的数据压缩库libzling-20160105

    libzling(https://github.com/richox/libzling,求观看[watch],求星[star],求叉[fork])是一款高性能的数据压缩库,参见原贴:http://ww ...

  2. encodeURI、encodeURIComponent、btoa及其应用场景

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,’,(,),*,+,,,-,.,/,:,;,=,?,@ ...

  3. Java多线程(一)初步了解

    1:线程 1.1:线程是什么?线程与进程. 进程:正在运行的程序.windows是多进程的系统.每一个进程有自己的内存区域,也就是每个进程都会占用一定的内存.一般打开应用程序就会有进程的了. 线程:进 ...

  4. Strut2 ognl取出存放在request,session,application和对象栈的中的值

    1.取出request,session,applicaiton中的值 a.往里面加入request,session,application中加入值 public String testServlet( ...

  5. How can I set ccshared=-fPIC while executing ./configure?

    解决方式如下: make clean ./configure CFLAGS=-fPIC CXXFLAGS=-fPIC

  6. json-c开发指南

    网上看到的一片关于json-c的文章.收藏一下,忘记了出处,尽请作者谅解. JSON c语言开发指南   1.    引言 本文档是基于json-c 库对数据交换进行开发所编写的开发指南,及详细解释j ...

  7. leetcode5:subsets问题

    问题描述: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subse ...

  8. HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)

    Elven Postman Elves are very peculiar creatures. As we all know, they can live for a very long time ...

  9. flask框架的学习

    ---恢复内容开始--- 第一个flask程序讲解:1. 第一次创建项目的时候,要添加flask的虚拟环境.添加虚拟环境的时候,一定要选择到python这个执行文件.比如你的flask的虚拟环境的目录 ...

  10. webstorm中sass编译时目录或内容包含中文字符报错

    ruby版本:ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32] sass版本:Sass 3.4.22 (Selective Steve) ...