首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ag网格单元格编辑仅允许数字

ag网格单元格编辑仅允许数字
EN

Stack Overflow用户
提问于 2020-09-10 06:18:13
回答 2查看 3K关注 0票数 1

如何让简单输入的数字“type=”在农业网格单元?如下所示:

代码语言:javascript
运行
复制
cellRenderer: params => {
        if (isNaN(Number(params.value))
        {
            return params.value.replace(/[^0-9\.]+/g, '');
        } else {
            return params.value;
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2020-09-10 21:26:42

不幸的是,没有一种简单的方法可以做到这一点。在我的例子中,我创建了单独的组件,比如在官方页面ag-grid上。我希望在不久的将来他们会改变这一点,因为现在它非常不清楚和复杂。感谢大家的帮助!

票数 2
EN

Stack Overflow用户

发布于 2020-09-10 13:38:45

你所做的就像是“猎枪”。它将处理无效输入,不仅是在编辑时,而且即使在渲染时提供了一些错误的值(从源获取数据)。

您必须定义一个自定义组件来处理用户输入。

代码语言:javascript
运行
复制
var columnDefs = [
    {
        field: "value",
        editable: true,
        cellEditorSelector: function (params) {
            if (params.data.type === 'age') { // here you can check using your logic if it needs numericCelleditor or not
                return {
                    component: 'numericCellEditor'
                };
            }
             return null;
        ...
        ...

自定义编辑器组件Ref Documentation

代码语言:javascript
运行
复制
// function to act as a class
function NumericCellEditor() {
}

// gets called once before the renderer is used
NumericCellEditor.prototype.init = function (params) {
    // create the cell
    this.eInput = document.createElement('input');

    if (isCharNumeric(params.charPress)) {
        this.eInput.value = params.charPress;
    } else {
        if (params.value !== undefined && params.value !== null) {
            this.eInput.value = params.value;
        }
    }

    var that = this;
    this.eInput.addEventListener('keypress', function (event) {
        if (!isKeyPressedNumeric(event)) {
            that.eInput.focus();
            if (event.preventDefault) event.preventDefault();
        } else if (that.isKeyPressedNavigation(event)){
            event.stopPropagation();
        }
    });

    // only start edit if key pressed is a number, not a letter
    var charPressIsNotANumber = params.charPress && ('1234567890'.indexOf(params.charPress) < 0);
    this.cancelBeforeStart = charPressIsNotANumber;
};

NumericCellEditor.prototype.isKeyPressedNavigation = function (event){
    return event.keyCode===39
        || event.keyCode===37;
};


// gets called once when grid ready to insert the element
NumericCellEditor.prototype.getGui = function () {
    return this.eInput;
};

// focus and select can be done after the gui is attached
NumericCellEditor.prototype.afterGuiAttached = function () {
    this.eInput.focus();
};

// returns the new value after editing
NumericCellEditor.prototype.isCancelBeforeStart = function () {
    return this.cancelBeforeStart;
};

// example - will reject the number if it contains the value 007
// - not very practical, but demonstrates the method.
NumericCellEditor.prototype.isCancelAfterEnd = function () {
    var value = this.getValue();
    return value.indexOf('007') >= 0;
};

// returns the new value after editing
NumericCellEditor.prototype.getValue = function () {
    return this.eInput.value;
};

// any cleanup we need to be done here
NumericCellEditor.prototype.destroy = function () {
    // but this example is simple, no cleanup, we could  even leave this method out as it's optional
};

// if true, then this editor will appear in a popup 
NumericCellEditor.prototype.isPopup = function () {
    // and we could leave this method out also, false is the default
    return false;
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63820281

复制
相关文章

相似问题

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