This commit is contained in:
wangshiming
2021-12-13 10:43:03 +08:00
parent d13b27e76b
commit cc7f7c7649
23 changed files with 572 additions and 354 deletions

View File

@ -0,0 +1,3 @@
<nz-select [(ngModel)]="value" (ngModelChange)="change($event)" *ngIf="dictList">
<nz-option [nzValue]="item.value" [nzLabel]="item.label" *ngFor="let item of dictList"></nz-option>
</nz-select>

View File

@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DictSelectComponent } from './dict-select.component';
describe('DictSelectComponent', () => {
let component: DictSelectComponent;
let fixture: ComponentFixture<DictSelectComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ DictSelectComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(DictSelectComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,68 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, forwardRef, Input, OnInit } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { AddressComponent } from '@shared';
import { DictSelectService } from './dict-select.service';
@Component({
selector: 'app-dict-select',
templateUrl: './dict-select.component.html',
styleUrls: ['./dict-select.component.less'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DictSelectComponent),
multi: true,
},
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DictSelectComponent implements OnInit, ControlValueAccessor {
private onChangeFn?: (val: string) => void;
private onTouchedFn?: () => void;
defaultUrl = `/api/mdc/pbc/dictItems/getDictValue`;
@Input() value: string = ''; // 默认选中值
@Input() url: string = ''; // 获取字典数据的地址
@Input() params = {};// 请求参数
dictList: any[] = [];
constructor(public service: DictSelectService, public cdr: ChangeDetectorRef) { }
writeValue(geo: string): void {
if (geo == null) {
this.value = '';
return;
}
this.value = geo;
}
registerOnChange(fn: any): void {
this.onChangeFn = fn;
}
registerOnTouched(fn: any): void {
this.onTouchedFn = fn;
}
ngOnInit(): void {
this.service.getDictList(this.url || this.defaultUrl, this.params).subscribe(res => {
if (res) {
this.dictList = res || [];
if (this.dictList.length > 0) {
const obj = { label: '全部', value: '' };
this.dictList.unshift(obj);
}
this.cdr.markForCheck();
}
});
}
change($event: any) {
this.onChangeFn!($event);
}
isEmpty(val: any) {
return val === undefined || val === null || val === '';
}
}

View File

@ -0,0 +1,16 @@
import { Injectable, Injector } from '@angular/core';
import { BaseService } from '../../services/core/base.service';
@Injectable({
providedIn: 'root'
})
export class DictSelectService extends BaseService {
constructor(public injector: Injector) {
super(injector);
}
getDictList(url: string, params = {}) {
return this.request(url, params);
}
}

View File

@ -0,0 +1,11 @@
/*
* @Author: your name
* @Date: 2021-12-13 10:41:19
* @LastEditTime: 2021-12-13 10:41:57
* @LastEditors: your name
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \tms-obc-web\src\app\shared\components\dict-select\index.ts
*/
export * from './dict-select.component.spec';
export * from './dict-select.component';
export * from './dict-select.service';