1. 程式人生 > 程式設計 >ng-alain的sf如何自定義部件的流程

ng-alain的sf如何自定義部件的流程

一、背景

最近使用ng-alain做前端,sf的部件很豐富,但是做起來之後就會發現,多多少少會有一些不符合需求的東西,比如:

ng-alain的sf如何自定義部件的流程

這是一個string的部件,後邊跟上一個單位看著很不錯,但是我們通常在使用number時會更需要這個單位,然而官方的部件並沒有
再比如:

ng-alain的sf如何自定義部件的流程

我想做一個編輯框,要求內容不可編輯,並且該內容要從別的列表進行選擇,下拉選擇可以滿足需求,但是如果內容太多,有時就不方便使用下拉框了,那麼這時候我們就需要自定義

二、自定義ng-alain部件的流程

1、元件的整體結構

ng-alain的sf如何自定義部件的流程

2、首先,元件click-input.component.html,自定義元件要包在sf-item-wrap特殊標籤裡面

<sf-item-wrap [id]="id" [schema]="schema" [ui]="ui" [showError]="showError" [error]="error" [showTitle]="schema.title">
 <!-- 開始自定義控制元件區域 -->
 <div nz-row>
  <div nz-col nzSpan="16"><input type="text" [placeholder]="placeholder" nz-input [(ngModel)]="content" [disabled]="inputDisable" (ngModelChange)="onChange()"></div>
  <div nz-col nzSpan="2" nzPush="2"></div>
  <div nz-col nzSpan="6"><button nz-button type="button" nzType="primary" (click)="test()" >{{btnTitle}}</button></div>
 </div>
 <!-- 結束自定義控制元件區域 -->
</sf-item-wrap>

3、寫元件click-input.component,繼承ControlWidget

import {Component,OnInit} from '@angular/core';
import { ControlWidget } from '@delon/form';

@Component({
 selector: 'app-product-widget-click-input',templateUrl: './click-input.component.html',})
export class ClickInputComponent extends ControlWidget implements OnInit {

 /* 用於註冊小部件 KEY 值 */
 static readonly KEY = 'click-input';

 // 價格區間
 content: any;
 // to: any;

 placeholder: string;  // 使用的時候用來繫結 ui: {placeholder: '請選擇業務系統' } 
 inputDisable: boolean; // 使用的時候用來繫結 ui: {inputDisable: true,// input是否可用}
 btnTitle: string; // 使用的時候用來繫結 ui: {btnTitle: '選擇資料',}  按鈕名稱

 ngOnInit(): void {
  this.placeholder = this.ui.placeholder || '請輸入'; // 使用的時候用來繫結 ui: {placeholder: '請選擇業務系統' } 
  this.inputDisable = this.ui.inputDisable || false; // 使用的時候用來繫結 ui: {inputDisable: true,// input是否可用}
  this.btnTitle = this.ui.btnTitle || '按鈕'; // 使用的時候用來繫結 ui: {btnTitle: '選擇資料',}
 }

 getData = () => {
  return this.content;
 }

 onChange() {
  const v = this.getData();
  this.setValue(v);
 }

 // reset 可以更好的解決表單重置過程中所需要的新資料問題
 reset(value: any) {
  if (value) {
   console.log(value);
   const content = value;
   this.content = content;
   // this.to = to;
   this.setValue(value);
  }

 }

 test(value?: string){
  if (this.ui.click) {
   this.ui.click(value);  // 可以在元件裡直接呼叫使用ui的配置那裡的方法 ui: {click: (value) => this.test(value),}
  }
 }

}

4、在shared.module.ts中註冊部件

涉及到專案內容,以下只展示註冊部件的住要內容

// 自定義 小部件
const WIDGETS = [
 RangeInputComponent,ClickInputComponent
];

@NgModule({
 declarations: [
  // your components
  ...COMPONENTS,...DIRECTIVES,...WIDGETS
 ],})

export class SharedModule {
 constructor(widgetRegistry: WidgetRegistry) {
  // 註冊 自定義的 widget
  for (const widget of WIDGETS){
   widgetRegistry.register(widget.KEY /* 'range-input' */,widget);
  }
 }
}

5、使用自定義部件

ng-alain的sf如何自定義部件的流程

channel-select.component.html

<sf [schema]="schema" (formSubmit)="submit($event)">
</sf>

channel-select.component.ts

schema: SFSchema = {
  properties: {
   btn: { type: 'string',title: '編輯框+按鈕',default: '1234',// 設預設值
    ui: {
     widget: 'click-input',placeholder: '請選擇業務系統',// inputDisable: true,// input是否可用
     btnTitle: '選擇資料',click: (value) => this.test(value),}
   },}
 };

總結

到此這篇關於ng-alain的sf如何自定義部件的文章就介紹到這了,更多相關ng-alain的sf如何自定義部件內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!