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

2024-01-09

在这里,我尝试根据 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 中。我将操作员列表更新到配置中,但它不会更新到用户界面中。

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


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

我在用着

  • 角度2查询构建器0.4.2
  • 角度 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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动态更改 angular2-query-builder 中的配置 的相关文章

随机推荐

  • 如何在 Python 中创建常量?

    如何在 Python 中声明常量 在 Java 中 我们这样做 public static final String CONST NAME Name 在 Python 中不能将变量或值声明为常量 To indicate对于程序员来说 变量是
  • 重复排列:避免溢出

    背景 Given n球使得 a balls are of colour GREEN b balls are of colour BLUE c balls are of colour RED 当然a b c n 这些球可以排列的排列数量由下式
  • 如果只需要结果的低位部分,可以使用哪种 2 的补码整数运算而无需将输入中的高位清零?

    在汇编编程中 想要从寄存器的低位计算某些内容是相当常见的 但不能保证其他位清零 在 C 等高级语言中 您只需将输入转换为小尺寸 然后让编译器决定是否需要分别将每个输入的高位清零 或者是否可以在事实 This is especially co
  • CUDA 素数生成器性能低下

    我正在用 CUDA 编写我的第一个程序 它是一个素数生成器 它可以工作 但只比同等的单线程 C 代码快 50 CPU版本100 使用一个核心 GPU版本仅使用20 的GPU CPU 是 i5 2310 GPU是GF104 如何提高该算法的性
  • IIS7.5 max-age问题(asp.net mvc输出缓存)

    我们使用Windows Server 2008 R2 Enterprise和IIS7 5 7600 16385 我在服务器上部署了一个简单的Web asp net mvc c net Framework 4 5 1 如下所示的控制器 并且
  • 使用 msbuild 进行构建并动态设置项目引用

    我有几个引用 SQL Server 程序集的项目 对于 SQL Server 2005 和 SQL Server 2008 我当前维护 2 个项目文件 它们指向相同的源文件 唯一的区别是对 SQL Server 程序集的引用 有没有一种方法
  • 如何使用带有代理的discord python bot?

    需要使用带有代理的discord bot https 或socks 例如 代理为 192 168 1 1 3125 代理授权为 proxy login proxy pass 我已经尝试过这个例子 如何通过代理连接不和谐机器人 https s
  • 具有drive.file范围的更改源的预期行为是什么?

    我的期望是 如果我查询范围为drive file 的更改源 我将仅收到对我的应用程序拥有的文件的更改 然而 在我所做的测试中 我发现提要中的文件与我的应用程序无关 至少其中一些是与我共享的文件 有人确切知道这应该如何工作吗 Edit 0 类
  • 如果“mousemove”和“click”事件同时触发怎么办?

    不知道是否只是Chrome问题 现在无法检查 但是让我们尝试下面的代码 我们将两个事件绑定到某个元素 div on mousemove function e console log move click function e console
  • Windows cmd - 使用 oneliner 与用户输入提示 [npm] 交互

    我正在尝试将输入作为一行传递给用户输入提示 但很难弄清楚如何正确执行此操作 具体来说 我正在尝试使用登录到 npmnpm adduser npm login是它的别名 当它是一个奇异值时 它可以工作 但这只能让我到目前为止 echo exa
  • SiteCatalyst:跟踪 Webkit 浏览器上的自定义链接

    我的查询是我有一个重定向到另一个页面的链接 在 webkit 浏览器中 如何强制 sitecatalyst 服务器调用 脚本执行 在重定向发生之前完成 我正在使用 sitecatalyst 作为门户 我有 配置自定义链接调用以包含 didA
  • 在可选参数中设置日期时间的默认值[重复]

    这个问题在这里已经有答案了 如何在可选参数中设置日期时间的默认值 public SomeClassInit Guid docId DateTime addedOn DateTime Now Init codes here 有一个解决方法 利
  • 如何在一行内遍历这个哈希值?

    哈希中的每个键都有一个值 该值也是哈希 100 gt 1 gt ruby 2 gt enumerables 50 gt 3 gt can 4 gt cause 15 gt 5 gt occassional 6 gt insanity 对于每
  • 正确关闭数据库连接 - VBScript + MS SQL

    我有一个连接到 SQL 服务器的经典 ASP vbscript Web 应用程序 在某些页面上 我打开多个数据库连接来提取数据 我的问题是 是在每个页面底部添加关闭连接功能更好 还是在使用后立即显式关闭连接更好 请记住 在这些特定页面上 每
  • Java 单元测试:替换被测私有方法

    运行 JUnit 测试时是否有任何方法可以替换私有方法中的逻辑 一些背景知识 我们有一些与 OSGi 容器内的包交互的私有方法 这在单元测试中不可用 因此该方法将失败 我们已经研究过 JMockIt 但方法替换功能似乎想要强制您替换类中相互
  • 线性渐变还需要浏览器前缀吗? [复制]

    这个问题在这里已经有答案了 我是否仍然需要使用线性渐变属性的浏览器前缀 background image webkit linear gradient top 2F2727 1a82f7 background image moz linea
  • 检查文件是否在 Delphi 中打开

    有没有办法检查文件是否已被 Delphi 中的 ReWrite 打开 代码会是这样的 AssignFile textfile somefile txt if not textFile IsOpen then Rewrite textFile
  • “类型对象指针”和“同步块索引”中存储了什么样的数据?

    在 CLR 中 每个实例都有 2 个附加字段来存储一些数据来管理对象 类型对象指针 同步块索引 您能否简单解释一下它们内部存储的内容以及 CLR 如何使用它们 Thanks 类型对象指针是指向对象类型描述的指针 这用于找出对象的实际类型是什
  • Pandas 在多列上应用元组解包函数

    Given a function它接受多个参数并返回多个值 如下所示 def tuple unpack value another value does some interesting stuff return value another
  • 动态更改 angular2-query-builder 中的配置

    在这里 我尝试根据 Angular2 query builder 中的字段值更改运算符 我的 TS 文件 Component selector my app template div h2 Hello name h2 div