@eliot-ragueneau/ngx-query-builder

中文文档
20.0.2发布于 2025/07/01MITTypeScriptES Module近 30 天下载 2337

npm version [npm downloads a month](https://i

原始使用文档
文档目录12 节
  1. Install
  2. Basic Usage
  3. Custom Input Components
  4. Property Bindings Quick Reference
  5. Structural Directives
  6. Dependencies
  7. dev
  8. demo preview
  9. Code structure
  10. build
  11. publish
  12. For use in other local projects

ngx-query-builder

npm version npm downloads a month npm downloads a week Contribute with Gitpod

A modernized Angular 4+ query builder based on jQuery QueryBuilder. Support for heavy customization with Angular components and provides a flexible way to handle custom data types.

Forked from (https://github.com/zebzhao/Angular-QueryBuilder) as the original project has stopped updating and does not support the latest Angular versions, so I will continue to maintain it and support the latest versions of Angular.

Getting Started

Install

  • Angular 20, use 20.X.X
  • Angular 19, use 19.X.X

npm install @eliot-ragueneau/ngx-query-builder


  • Angular 9, use 0.6.0
  • Angular 8, use 0.5.1
  • Angular 6-7, use 0.4.2
  • Angular 4-5, use 0.3.2

npm install angular2-query-builder

See https://github.com/zebzhao/Angular-QueryBuilder for more details on the usage of the old version.

Examples

Basic Usage

app.module.ts
import {QueryBuilderModule} from "@eliot-ragueneau/ngx-query-builder";
import {AppComponent} from "./app.component"

@NgModule({
  imports: [...,
  QueryBuilderModule,
  IonicModule.forRoot(AppComponent) // (Optional) for IonicFramework 2+
]})
export class AppModule {
}
app.component.html
...
<ngx-query-builder [(ngModel)]='query' [config]='config'></ngx-query-builder>
...
app.component.ts
import {QueryBuilderConfig} from '@eliot-ragueneau/ngx-query-builder';

export class AppComponent {
  query = {
    condition: 'and',
    rules: [
      {field: 'age', operator: '<=', value: 'Bob'},
      {field: 'gender', operator: '>=', value: 'm'}
    ]
  };

  config: QueryBuilderConfig = {
    fields: {
      age: {name: 'Age', type: 'number'},
      gender: {
        name: 'Gender',
        type: 'category',
        options: [
          {name: 'Male', value: 'm'},
          {name: 'Female', value: 'f'}
        ]
      }
    }
  }
}

Custom Input Components

app.component.html

<ngx-query-builder [(ngModel)]='query' [config]='config'>
  <ng-container *queryInput="let rule; type: 'date'">
    <custom-datepicker [(ngModel)]="rule.value"></custom-datepicker>
  </ng-container>
</ngx-query-builder>
app.component.ts
query = {
  condition: 'and',
  rules: [
    {field: 'birthday', operator: '=', value: new Date()}
  ]
};

config: QueryBuilderConfig = {
  fields: {
    birthday: {
      name: 'Birthday',
      type: 'date',
      operators: ['=', '<=', '>'],
      defaultValue: () => new Date()
    }
  }
}

Property Bindings Quick Reference

ngx-query-builder

NameTypeRequiredDefaultDescription
allowRulesetbooleanOptionaltrueDisplays the + Ruleset button if true.
allowCollapsebooleanOptionalfalseEnables collapsible rule sets if true.
classNamesQueryBuilderClassNamesOptionalCSS class names for different child elements in query-builder component.
configQueryBuilderConfigRequiredConfiguration object for the main component.
dataRulesetOptional{ condition: 'and', rules: [] }(Use ngModel or value instead.)
emptyMessagestringOptionalMessage to display for an empty Ruleset if empty rulesets are not allowed.
ngModelRulesetOptional{ condition: 'and', rules: [] }Object that stores the state of the component. Supports 2-way binding.
operatorMap{ [key: string]: string[] }OptionalUsed to map field types to list of operators.
persistValueOnFieldChangebooleanOptionalfalseIf true, when a field changes to another of the same type, and the type is one of: string, number, time, date, or boolean, persist the previous value. This option is ignored if config.calculateFieldChangeValue is provided.
config.calculateFieldChangeValue(currentField: Field, nextField: Field, currentValue: any) => anyOptionalUsed to calculate the new value when a rule's field changes.
valueRulesetOptional{ condition: 'and', rules: [] }Object that stores the state of the component.

Structural Directives

Use these directives to replace different parts of query builder with custom components.

queryArrowIcon

Directive to replace the expand arrow used in collapse/accordion mode of the query builder.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
getDisabledState() => booleanRetrieves or determines the disabled state of the component

querySwitchGroup

Useful for replacing the switch controls, for example the AND/OR conditions. More custom conditions can be specified by using this directive to override the default component.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
onChange() => voidCallback to handle changes to the switch group component
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryButtonGroup

For replacing the default button group for Add, Add Ruleset, Remove Ruleset buttons.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
parentValueRuleSetFor parent node's data if exists
addRule() => voidFunction to handle adding a new rule
addRuleSet() => voidFunction to handle adding a new rule set
removeRuleSet() => voidFunction to handle removing the current rule set
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryRulesetAddRuleButton

For replacing the default button for Add Rule.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
addRule() => voidFunction to handle adding a new rule
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryRulesetAddRulesetButton

For replacing the default button for Add Ruleset.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
addRuleSet() => voidFunction to handle adding a new rule set
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryRulesetRemoveButton

For replacing the default button for Remove Ruleset.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
removeRuleSet() => voidFunction to handle removing the current rule set
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryEntity

Used to replace entity selection component.

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
entitiesEntity[]List of entities for the component, specified by config
onChange(entityValue: string, rule: Rule) => voidCallback to handle changes to the entity component
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryField

Used this directive to replace the query field selection component.

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
getFields(entityName: string) => voidGet the list of fields corresponding to an entity
fieldsField[]List of fields for the component, specified by config
onChange(fieldValue: string, rule: Rule) => voidCallback to handle changes to the field component
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryOperator

Used to replace the query operator selection component.

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
operatorsstring[]List of operators for the field, returned by getOperators
onChange() => voidCallback to handle changes to the operator component
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryInput

Used to replace the input component. Specify the type/queryInputType to match specific field types to input template.

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
fieldFieldCurrent field object which contains the field's value and name
optionsOption[]List of options for the field, returned by getOptions
onChange() => voidCallback to handle changes to the input component
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryRuleRemoveButton

Directive to replace the default remove single rule button component.

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
removeRule(rule: Rule) => voidFunction to handle removing a rule
getDisabledState() => booleanRetrieves or determines the disabled state of the component

queryEmptyWarning

Can be used to customize the default empty warning message, alternatively can specify the emptyMessage property binding.

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
messagestringValue passed to emptyMessage
getDisabledState() => booleanRetrieves or determines the disabled state of the component

Dependencies

  • Angular 18+

Development

dev

  • pnpm watch
  • pnpm start
  • vist http://localhost:4200 to see the demo

demo preview

demo demo

Code structure

  • BaseQueryBuilder (Logic container - hidden)
    • QueryBuilderComponent (Root component - exported)
    • InnerQueryBuilderComponent (Recursive component - hidden)

build

  • pnpm build

publish

cd dist/ngx-query-builder
npm publish --access public

For use in other local projects

cd dist/ngx-query-builder
npm link

cd /path/to/other/project
npm link ngx-query-builder

That's it.

版本与发布记录

@eliot-ragueneau/ngx-query-builder 共发布 7 个版本,首次发布于 2025/07/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
20.0.2latest2025/07/011.4 年前
19.0.32025/07/011.4 年前
20.0.12025/07/011.4 年前
19.0.22025/07/011.4 年前
19.0.12025/07/011.4 年前
20.0.02025/07/011.4 年前

关于 @eliot-ragueneau/ngx-query-builder 的常见问题

@eliot-ragueneau/ngx-query-builder 是什么?

[](https://i

如何安装 @eliot-ragueneau/ngx-query-builder?

在项目目录运行 npm install @eliot-ragueneau/ngx-query-builder,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(^20.0.0)、@angular/core(^20.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@eliot-ragueneau/ngx-query-builder 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eliot-ragueneau/ngx-query-builder 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@eliot-ragueneau/ngx-query-builder 可以商用吗?

@eliot-ragueneau/ngx-query-builder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eliot-ragueneau/ngx-query-builder 的下载量有多少?

根据 npm 官方下载统计,@eliot-ragueneau/ngx-query-builder 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,337 次,日均约 78 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eliot-ragueneau/ngx-query-builder 还在维护吗?

最新版本 20.0.2 发布于 2025/07/01(1.4 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/07/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。