首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们如何覆盖angular2-query-builder中的getInputType函数

我们如何覆盖angular2-query-builder中的getInputType函数
EN

Stack Overflow用户
提问于 2020-09-17 12:46:28
回答 1查看 185关注 0票数 0

我在我的项目中使用了angular2-query-builder的角度材料代码,我面临着一个问题,我必须根据从上一个屏幕中选择的内容动态地更改config.field。当我更改字段时,我可以看到新的字段,但是,浏览器控制台抛出以下错误:

代码语言:javascript
复制
    QueryBuilderComponent.html:109 ERROR Error: No configuration for field 'Customer Name' could be found! Please add it to config.fields.
    at QueryBuilderComponent.push../node_modules/angular2-query-builder/dist/components/query-builder/query-builder.component.js.QueryBuilderComponent.getInputType (query-builder.component.js:228)
    at QueryBuilderComponent.push../node_modules/angular2-query-builder/dist/components/query-builder/query-builder.component.js.QueryBuilderComponent.findTemplateForRule (query-builder.component.js:165)
    at Object.eval [as updateDirectives] (QueryBuilderComponent.html:124)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
    at checkAndUpdateView (core.js:10451)
    at callViewAction (core.js:10692)
    at execEmbeddedViewsAction (core.js:10655)
    at checkAndUpdateView (core.js:10452)
    at callViewAction (core.js:10692)
    at execEmbeddedViewsAction (core.js:10655) 

在调试时,它来自QueryBuilderComponent.getInputType我想绕过这个条件,如果找不到字段,则返回null:

代码语言:javascript
复制
QueryBuilderComponent.prototype.getInputType = function (field, operator) {
        if (this.config.getInputType) {
            return this.config.getInputType(field, operator);
        }
        if (!this.config.fields[field]) {
            return null; //MY CODE
            // throw new Error("No configuration for field '" + field + "' could be found! Please add it to config.fields."); // EXISTING CODE
        }
        var type = this.config.fields[field].type;
        switch (operator) {
            case 'is null':
            case 'is not null':
                return null; // No displayed component
            case 'in':
            case 'not in':
                return type === 'category' || type === 'boolean' ? 'multiselect' : type;
            default:
                return type;
        }
    };

我不能在node_modules的QueryBuilderComponent.js文件中更改它。如何覆盖或自定义此"getInputType“函数。

EN

回答 1

Stack Overflow用户

发布于 2020-09-17 13:44:03

实际上,我自己找到了答案。只需在ngOnInit()中添加所需的更改代码

代码语言:javascript
复制
ngOnInit() {

QueryBuilderComponent.prototype.getInputType = function (field, operator) {
        if (this.config.getInputType) {
            return this.config.getInputType(field, operator);
        }
        if (!this.config.fields[field]) {
            return null; //MY CODE
            // throw new Error("No configuration for field '" + field + "' could be found! Please add it to config.fields."); // EXISTING CODE
        }
        var type = this.config.fields[field].type;
        switch (operator) {
            case 'is null':
            case 'is not null':
                return null; // No displayed component
            case 'in':
            case 'not in':
                return type === 'category' || type === 'boolean' ? 'multiselect' : type;
            default:
                return type;
        }
    };
}

因为它是一个原型,所以它将覆盖更改

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63931426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档