欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

angular基于ng-alain定义自己的select组件示例

程序员文章站 2022-06-26 12:37:26
1、首先是my-select2.component.html页面,这里是在ng-alain的select基础上根据业务需求添加新的功能;代码如下:

1、首先是my-select2.component.html页面,这里是在ng-alain的select基础上根据业务需求添加新的功能;代码如下:

<nz-select #select style="width:100%;" [(ngmodel)]="selectedoption" [nzplaceholder]="myplaceholder" nzallowclear [nzshowsearch]="true" [nznotfoundcontent]="'无匹配'">
  <nz-option
    *ngfor="let option of options"
    [nzlabel]="option.label"
    [nzvalue]="option"
    [nzdisabled]="option.disabled">
  </nz-option>
</nz-select>

2、再者是my-select2.component.ts页面,代码里面有注释;代码如下:

import { controlvalueaccessor } from '@angular/forms/src/directives';
import { component, forwardref, input,oninit,elementref,output,eventemitter} from '@angular/core';
import { ng_value_accessor } from '@angular/forms';
import { router, navigationend } from '@angular/router';
import { formgroup, formbuilder, validators } from '@angular/forms';
import { selectservice } from './my-select2.service';
declare var $: any;
@component({
 selector: 'nz-select2',
 templateurl: './my-select2.component.html',
 providers: [ 
     {
      provide: ng_value_accessor,
      useexisting: forwardref(() => nzselect2component),//注入表单控件
      multi: true
     }]
})
export class nzselect2component implements oninit{
  constructor(private selectservice:selectservice) { 
  }
 innervalue: any = ''; 
 //监听绑定的值,与外岑的ngmodel相互绑定
 set selectedoption(val:any){
   if (val !== this.innervalue) {
      this.innervalue = val;
      this.onchangecallback(val.value);
      this.databack.emit(val.value); // 事件
    }
 }
 get selectedoption():any{
    return this.innervalue;
 }
 options = [];//接收select的数组
  _datasource:any;//接收本地的自定义数组或者请求返回的数组
 @input()
 url:any;//请求的url
 @input()
 myplaceholder:any;//自定义的placeholder
 @input()
 //下拉框的数据格式
  fieldkey:any = {
    text: 'text',
     value: 'value'
  };
 @input()
  set datasource(val: any) {
    this._datasource = val;
    if ($.isarray(this._datasource)) {   
    this.options=this._datatransform(this._datasource);//如果是本地数组或直接请求的数组直接复制
    }
  }
  get datasource(): any {
    return this._datasource;
  }
 @output() databack = new eventemitter<any>();
 registeronchange(fn: (value: any) => void) { 
   this.onchangecallback = fn;
 }
 registerontouched(fn: any) {
   this.ontouchedcallback = fn;
 }
  writevalue(value: string) {

  }
 onchangecallback = (value: any) => {};
 ontouchedcallback = (value: any) => {};
 ngoninit() {
     //如果url存在则直接请求
    if(this.url){
      this.selectservice.getvalue(this.url).subscribe(data => { 
        data = data.rows || data.data;    
        this.options=this._datatransform(data);
      });
    }   
 }
 //转换下拉框下的字段
  _datatransform(data: array<any>){
    let _data = [];
    for (let i = 0; i < data.length; i++) {
     _data[i] = {};
     _data[i].label = data[i][this.fieldkey.text];
     _data[i].value = data[i][this.fieldkey.value];
    }
    return _data;
 }
}

3、然后是my-select2.service.ts页面,这里主要是请求后台接口返回的下拉数组,url为父组件传过来的链接,代码如下:

import { injectable } from '@angular/core';
import { headers, http, urlsearchparams,requestoptions } from '@angular/http';
import { httpclient, httperrorresponse } from '@angular/common/http';
import 'rxjs/add/operator/topromise';
// import { environment } from '../../environments/environment';
@injectable()
export class selectservice {
  constructor(private http: httpclient) {}
  getvalue(url: any):any{
    return this.http
      .get(url);
   
  }
}

4、然后是myselect.module.ts页面,这里,使用该组件的前提是要引入 import { nzselectmodule } from 'ng-zorro-antd',代码如下:

import { ngmodule, modulewithproviders }    from '@angular/core';
import { commonmodule }  from '@angular/common';
import { formsmodule,reactiveformsmodule } from '@angular/forms';
import { nzselect2component } from './my-select2.component';
import { selectservice } from './my-select2.service';
import { nzselectmodule } from 'ng-zorro-antd';
@ngmodule({
  imports: [
    commonmodule,
    formsmodule,
    nzselectmodule,
    reactiveformsmodule
  ],
  exports:[
    nzselect2component
  ],
  declarations: [
    nzselect2component
  ],
  providers: [
     selectservice
  ]
}) 

export class myselectmodule {
  constructor() {

  }
}

5、使用方法,在你需要的模块引入:myselectmodule

import { myselectmodule } from 'bizapp/base/components/myselect/myselect.module';

6、如何调用:url为请求后台的接口,fieldkey为数组的格式,这里可以根据后台返回来的格式定义这里的字段,如:后台返回格式为[{dmsm1:5,dmz:5}]则fieldkey的定义如下,myplaceholder为初始化时显示的内容,如果是本地数组,则只需要加上[datasource]="peer",这里的peer为本地数组

复制代码 代码如下:

<nz-select2  [url]="'analysis/api/data/code/list/030107'" [(ngmodel)]="search2.hpzl" [fieldkey]="{text:'dmsm1',value:'dmz'}" [myplaceholder]="'号牌种类'"></nz-select2>

7、总结:通过这个组件,我们只需要修改url和fieldkey就可以在任意模块引入然后使用,减少代码的使用,方便维护

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。