我对使用 Angular Material 7 多选提出了一些建议。该文档对我正在尝试做的事情没有多大帮助。
关于我正在尝试做的事情的一些背景知识。
所以我们正在创建一个记录对象,作为其中的一部分,我们需要知道是什么项目资助了他们。ProgrammeList 是具有 {ProgrammeKey, Name, Description} 的 Program 对象数组。以前我们使用过单选,效果很好。问题是每个报告都可以由多个项目资助,所以我们需要使用多选。
首先,说到数据库。多选选项将如何保存?这个想法是多选将形成自己的选定对象数组,然后将它们传递回后端以保存在链接表中。
我已经包含以下相关区域的代码
记录.dto.ts
import {Programme} from "./programme.dto";
export class Record {
Programme: Programme[];
CcfLead: string;
NihrRef: string;
AddRef: string;
AwardTitle: string;
ProjectTitle: string;
StartDate: Date;
EndDate: Date;
}
通用组件.ts ...
programmes = new FormControl();
programmeList: Programme[];
createRecordFrm() {
this.recordFrm = this.fb.group({
Programme : [{}, Validators.required],
NihrRef: ["", Validators.required],
AddRef:[""],
CCFLead: ["", Validators.required],
AwardTitle: ["", Validators.required],
ProjectTitle: ["", Validators.required],
StartDate: Date,
EndDate: [Date, Validators.required]
});
}
updateRecordFrm(record: Record) {
this.recordFrm.setValue({
Programme: record.Programme,
NihrRef: record.NihrRef,
AddRef: record.AddRef,
CCFLead: record.CcfLead,
AwardTitle: record.AwardTitle,
ProjectTitle: record.ProjectTitle,
StartDate: record.StartDate,
EndDate: record.EndDate
});
}
open(content: any, modal?: Record) {
this.msg = null;
this.modalMsg = null;
if (modal != null) {
this.updateRecordFrm(modal);
// update recordFrm
}
this.activeModal = this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title', backdrop: "static", keyboard: false });
}
general.component.html
<ng-template #recordWizard let-c="close" let-d="dismiss" width="50%">
<form novalidate (ngSubmit)="saveRecord(recordFrm.value)" [formGroup]="recordFrm">
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">New Record</h4>
<button type="button" class="close" (click)="d('Cross Click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div *ngIf="modalMsg" role="alert" class="alert alert-info alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<mat-icon>report_problem</mat-icon>
<span class="sr-only">Error:</span>
{{modalMsg}}
</div>
<div>
<mat-tab-group>
<mat-tab label="First">
<div class="form-group">
<span>NIHR Reference*</span>
<input type="text" class="form-control" readonly formControlName="NihrRef" />
</div>
<div class="form-group">
<span>Additional Reference</span>
<input type="text" class="form-control" readonly formControlName="AddRef" />
</div>
<div class="form-group">
<span>Funding Programme:*</span>
<mat-select class="form-control" placeholder="Funding Programmes" formControl="programme" multiple [compareWith]="compare">
<mat-option *ngFor="let programme of programmeList" [value]="programmeList">{{programme.Description}}</mat-option>
</mat-select>
</div>
</mat-tab>
<mat-tab label="Second">
Content 2
</mat-tab>
<mat-tab label="Third">
Content 3
</mat-tab>
<mat-tab label="Fourth">
Content 4
</mat-tab>
<mat-tab label="Fifth">
Content 5
</mat-tab>
<mat-tab label="Sixth">
Content 6
</mat-tab>
</mat-tab-group>
</div>
</div>
<div class="modal-footer">
<div>
<button type="submit" (click)="c" [disabled]="newRecordFrm.invalid" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-outline-dark" (click)="c('Save click')">Cancel</button>
</div>
</div>
</form>
</ng-template>
更新
我遇到的主要问题是 dto 不正确,需要更多值。
有一个新问题,下拉菜单没有被带到视图的前面。而是在活动模型后面。有没有我可以申请的css来解决这个问题?
非常感谢您的帮助
刘易斯