繁体   English   中英

PrimeFaces 扩展表限制输入小数位

[英]PrimeFaces Extension Sheet Restrict Decimal Places For Input

我想将小数位数限制为 1。用户不应键入多个点 (1..99) 和 (1.99)。 我想要 1.9 2.6 for ex。 不是 2.66 然后转换成 2.7 等。我必须使用 pe:sheetcolumn。 我尝试添加 p:inputnumber 和其他 p: 组件而不是 pe 扩展。 但是 pe:sheetcolumn 必须是。 使用以下方法,它只允许用户键入多个点和多个小数位。 在用户使用 #0.0 在屏幕上输入值后,它只是转换为 1 位小数。 但我想限制输入输入多个小数位而不是 1 和多个点。 我考虑过 javascript keyup 事件,但我认为这是一种糟糕的方法。 我怎样才能做到这一点

<pe:sheetcolumn headerText="SOME" value="#{SOME.some}" colWidth="200"
                        colType="numeric" numericPattern="#0.0" >
        </pe:sheetcolumn>

例如,对于 p:inputNumber,正如您在图像中看到的那样,用户不能键入多个点,并且他们不能添加超过 6 位的小数位。

例子

我想对 pe:sheetColumn 做同样的事情。 我怎样才能做到这一点

我的 JSF 版本:2.2.1 PRİMEFACES 和 PRIMEFACES 扩展版本:6.2

如果你安装了这个 MonkeyPatch,你就可以调整 output 来做任何你想做的事。 我很确定您可以使用var currentValue = this.getDataAtCell(row, col)获取当前单元格 如果您将此 JS 添加到您的应用程序,则可以调整它处理按键和验证的方式。

我为你添加了这一行

 var currentValue = this.getDataAtCell(row , col); // VALUE HERE!

所以你可以用你的代码验证你想要的任何东西,如果已经有一个“。” 不要接受另一个“。” ETC。

if (PrimeFaces.widget.ExtSheet) {
    PrimeFaces.widget.ExtSheet.prototype.handleHotBeforeKeyDown = function(e) {
        var selectedLast = this.getSelectedLast();
        if (!selectedLast) {
            return;
        }
        var row = selectedLast[0];
        var col = selectedLast[1];
        var celltype = this.getCellMeta(row, col).type;
        var currentValue = this.getDataAtCell(row , col); // VALUE HERE!

        var evt = e || window.event; // IE support
        var key = evt.charCode || evt.keyCode || 0;
        var shiftDown = e.shiftKey;

        // #740 tab on last cell should focus this next component
        if (this.allowTabOffSheet && key == 9) {
            var lastRow = this.countRows() - 1;
            var lastCol = this.countCols() - 1;
            if ((!shiftDown && row === lastRow && col === lastCol) ||
                (shiftDown && row === 0 && col === 0)) {
                e.stopImmediatePropagation();
                this.unlisten();
                this.deselectCell();

                //add all elements we want to include in our selection
                var focusableElements = 'a:not([disabled]), button:not([disabled]), input[type=text]:not([disabled]):not([hidden]):not([aria-hidden="true"]), [tabindex]:not([disabled]):not([tabindex="-1"]):not([aria-hidden="true"])';
                if (document.activeElement && document.activeElement.form) {
                    var focusable = Array.prototype.filter.call(document.activeElement.form.querySelectorAll(focusableElements),
                        function(element) {
                            //check for visibility while always include the current activeElement
                            return element.offsetWidth > 0 || element.offsetHeight > 0 || element === document.activeElement
                        });
                    var index = focusable.indexOf(document.activeElement);
                    if (index > -1) {
                        var nextElement = focusable[index + 1] || focusable[0];
                        nextElement.focus();
                    }
                }
            }
            return;
        }

        // prevent Alpha chars in numeric sheet cells
        if (celltype === "numeric") {
            // #766 do not block if just CTRL or SHIFT key
            if (key === 16 || key === 17) {
                return;
            }

            // #739 allow navigation
            var ctrlDown = evt.ctrlKey || evt.metaKey; // Mac support
            if (shiftDown || ctrlDown) {
                // navigation keys
                if (key == 9 || (key >= 35 && key <= 40)) {
                    return;
                }
            }

            // check for cut and paste
            var isClipboard = false;
            // Check for Alt+Gr (http://en.wikipedia.org/wiki/AltGr_key)
            if (ctrlDown && evt.altKey) isClipboard = false;
            // Check for ctrl+c, v and x
            else if (ctrlDown && key == 65) isClipboard = true; // a (select all)
            else if (ctrlDown && key == 67) isClipboard = true; // c
            else if (ctrlDown && key == 86) isClipboard = true; // v
            else if (ctrlDown && key == 88) isClipboard = true; // x

            // allow backspace, tab, delete, enter, arrows, numbers and keypad numbers
            // ONLY home, end, F5, F12, minus (-), period (.)
            // console.log('Key: ' + key + ' Shift: ' + e.shiftKey + ' Clipboard: ' + isClipboard);
            var isNumeric = ((key == 8) || (key == 9) || (key == 13) ||
                (key == 46) || (key == 110) || (key == 116) ||
                (key == 123) || (key == 188) || (key == 189) ||
                (key == 190) || ((key >= 35) && (key <= 40)) ||
                ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)));

            if ((!isNumeric && !isClipboard) || shiftDown) {
                // prevent alpha characters
                e.stopImmediatePropagation();
                e.preventDefault();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM