angular - 动态更改 angular2-query-builder 中的配置

标签 angular html css typescript

在这里,我尝试根据 Angular2-query-builder 中的字段值更改运算符。我的 TS 文件。

    @Component({
      selector: 'my-app',
      template: `
        <div>
          <h2>Hello {{name}}</h2>
        </div>
        <query-builder [(ngModel)]='uiExpression' [config]='config' (ngModelChange)="apply()" #que (onChangeCallback)="onchange()">
              <ng-container *queryButtonGroup="let ruleset; let addRule=addRule; let addRuleSet=addRuleSet; let removeRuleSet=removeRuleSet">
                <button mat-button (click)="addRule()">+ Rule</button>
                <button mat-button (click)="addRuleSet()">+ Ruleset</button>
                <button mat-button (click)="removeRuleSet()">- Ruleset</button>
              </ng-container>
              <ng-container *queryRemoveButton="let rule; let removeRule=removeRule">
                <button mat-icon-button color="accent" (click)="removeRule(rule)">
                  <mat-icon>remove</mat-icon>
                </button>
              </ng-container>
              <ng-container *querySwitchGroup="let ruleset">
                <mat-radio-group *ngIf="ruleset" [(ngModel)]="ruleset.condition">
                  <mat-radio-button value="and">And</mat-radio-button>
                  <mat-radio-button value="or">Or</mat-radio-button>
                </mat-radio-group>
              </ng-container>
              <ng-container *queryInput="let rule; type: 'string'">
                <mat-form-field>
                  <input matInput [(ngModel)]="rule.value">
                </mat-form-field>
              </ng-container>
              <ng-container *queryInput="let rule; type: 'number'">
                <mat-form-field>
                  <input matInput type="number" [(ngModel)]="rule.value">
                </mat-form-field>
              </ng-container>
              <ng-container *queryInput="let rule; type: 'date'">
                <mat-form-field>
                  <input matInput [matDatepicker]="picker" [(ngModel)]="rule.value">
                  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                  <mat-datepicker #picker></mat-datepicker>
                </mat-form-field>
              </ng-container>
              <ng-container *queryInput="let rule; let field=field; let options=options; type: 'multiselect'">
                <mat-form-field>
                  <mat-select multiple [(ngModel)]="rule.value">
                    <mat-option *ngFor="let opt of options" [value]="opt.value">
                      {{ opt.name }}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
              </ng-container>
              <ng-container *queryField="let rule; let fields=fields; let changeField=changeField">
                <mat-form-field>
                  <mat-select [(ngModel)]="rule.field" (ngModelChange)="changeFields($event, rule)">
                    <mat-option *ngFor="let field of fields" [value]="field.value">{{field.name}}</mat-option>
                  </mat-select>
                </mat-form-field>
              </ng-container>
              <ng-container *queryOperator="let rule; let operators=operators">
                <mat-form-field>
                  <mat-select [(ngModel)]="rule.operator" (ngModelChange)="changeOperator(rule)">
                    <mat-option *ngFor="let value of operators" [value]="value">{{value}}</mat-option>
                  </mat-select>
                </mat-form-field>
              </ng-container>
              </query-builder>
            <div>{{userExpression}}</div>
      `,
    })    
    export class App {
public operators = [
        {
            "attributeType": "STRING",
            "operators": [
                { "displayOperator": "Equals", "sqlOperator": " =(VALUE)" },
                { "displayOperator": "Does not Equal", "sqlOperator": "<>(VALUE)" },
                { "displayOperator": "Starts With", "sqlOperator": "LIKE %(VALUE)" },
                { "displayOperator": "Ends With", "sqlOperator": "LIKE (VALUE)%" },
                { "displayOperator": "Contains", "sqlOperator": "LIKE %(VALUE)%" },
                { "displayOperator": "Does Not Contain", "sqlOperator": "NOT LIKE %(VALUE)" },
                { "displayOperator": "Does Not Start With", "sqlOperator": "NOT LIKE (VALUE)%" },
                { "displayOperator": "Does Not End With", "sqlOperator": "NOT LIKE %(VALUE)%" }
            ]
        },
        {
            "attributeType": "Numeric",
            "operators": [
                { "displayOperator": "Equals", "sqlOperator": " =(VALUE)" },
                { "displayOperator": "Does not Equal", "sqlOperator": "<>(VALUE)" },
                { "displayOperator": "Greater", "sqlOperator": ">(VALUE)" },
                { "displayOperator": "Equal or Greater", "sqlOperator": ">=(VALUE)" },
                { "displayOperator": "Less", "sqlOperator": "<(VALUE)" },
                { "displayOperator": "Equal or Less", "sqlOperator": "<=(VALUE)" },
                { "displayOperator": "Within", "sqlOperator": "BETWEEN (VALUE1) AND (VALUE2)" },
            ]
        },
        {
            "attributeType": "Date",
            "operators": [
                { "displayOperator": "Equals", "sqlOperator": " =(VALUE)" },
                { "displayOperator": "On or After", "sqlOperator": ">=(VALUE)" },
                { "displayOperator": "Before", "sqlOperator": "<(VALUE)" },
                { "displayOperator": "Between", "sqlOperator": "BETWEEN (VALUE1) AND (VALUE2)" }
            ]
        }
    ]
          AttributeDummy: any[] = [
            {
              "userColumnName": "Attribute 1",
              "colType": "multiselect",
              "isListType": "Y",
              "userColumnOptions": [
                { name: "Male", value: "m" },
                { name: "Female", value: "f" }
              ]
            },
            {
              "userColumnName": "Attribute 2",
              "colType": "date",
              "isListType": "N",
              "userColumnOptions": ""
            },
            {
              "userColumnName": "Attribute 3",
              "colType": "string",
              "isListType": "N",
              "userColumnOptions": ""
            },
            {
              "userColumnName": "Attribute 4",
              "colType": "number",
              "isListType": "N",
              "userColumnOptions": ""
            }
          ]
          name: string;
          uiExpression = {};
          fieldsS = {}
          @Input() config: QueryBuilderConfig = {
            fields: {}
          }
          userExpression: String = 'Attribute = undefined';
          constructor() {
            this.name = `Plunker! v${VERSION.full}`;
            for (var i = 0; i < this.AttributeDummy.length; i++) {
              // operators: (this.AttributeDummy[i].colType.toLowerCase()==this.operator.operators[0].attributeType.toLowerCase()) ? this.operator.operators[0].operators : 
              // (this.AttributeDummy[i].colType.toLowerCase()==this.operator.operators[1].attributeType.toLowerCase()) ? this.operator.operators[1].operators : 
              // (this.AttributeDummy[i].colType.toLowerCase()==this.operator.operators[2].attributeType.toLowerCase()) ? this.operator.operators[2].operators : ''
              this.fieldsS[this.AttributeDummy[i].userColumnName] = {
                name: this.AttributeDummy[i].userColumnName,
                type: this.AttributeDummy[i].colType.toLowerCase(),
                operators: this.operators,
                options: this.AttributeDummy[i].userColumnOptions
              }
              this.config.fields = this.fieldsS;
              this.detect.markForCheck();
              console.log('config ', JSON.stringify(this.config))
            }
            if (this.AttributeDummy.length > 0) {
              console.log('attributes length > 0');
              this.uiExpression = {
                condition: 'and',
                rules: [
                  {
                    field: this.AttributeDummy[0].userColumnName,
                    operator: this.operators[0]
                  }
                ]
              }
            }
          }
        }

在更改属性时,我尝试将查询生成器中的运算符动态更新到 UI 中。我将运算符(operator)列表更新到配置中,但它不会更新到 UI 中。

这里有一个引用链接:https://zebzhao.github.io/Angular-QueryBuilder/demo/我想根据字段更新运算符。

最佳答案

我找不到任何好的文档。通过阅读 query-builder.component.js src 来了解它是如何工作的,我能够让它工作。

我正在使用

  • angular2-query-builder 0.4.2
  • Angular 8.0.0

添加对 QueryBuilderComponent 的引用。

  @ViewChild(QueryBuilderComponent, { static: true }) queryBuilder: QueryBuilderComponent;

然后在更新配置后,调用 queryBuilder 上的 ChangeField 以使用新的配置值更新 UI。

//set the options for your field
this.config.fields['myField'].options = [
   {name: 'a', value:'1'}, 
   {name: 'b', value:'2'}, 
   {name: 'c', value:'3'}
]

this.refreshField('myField);



private refreshField(field: string): void {
    // get the current rule
    const srcRule = this.queryBuilder.data.rules.find((x: Rule) => x.field === field) as Rule;

    if (srcRule) {

      // cache the current rule's selected value from our datasource
      const value = srcRule ? srcRule.value : undefined;

      // call change field to rebind new options to the UI
      this.queryBuilder.changeField(field, srcRule);

      // reset the previously selected value to the dropdown because changeField nulls out the value.
      srcRule.value = value;
    }
}

关于angular - 动态更改 angular2-query-builder 中的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312918/

相关文章:

javascript - 如何点击超链接打开打印预览页面?

Angular - 类型 'data' 上不存在属性 'Object'

Angular2 2.0.0 组件单元测试导致错误 : Bootstrap at least one component before injecting Router

c# - Angular 到 C# Http 传递查询参数

javascript - 为 td 添加背景颜色

python - 在 IPython 笔记本中对齐文本框小部件

angular - 将 Ionic 存储项作为字符串获取

javascript - 在移动设备上的 pageshow() 事件上使用 jcarousel 框架时延迟几秒

javascript - 我想在包含 Material 的 div 上添加透明覆盖,我该怎么做?

javascript - 动态加载 html 并尝试正确显示 i