primefaces-extensions / primefaces-extensions.github.com

Organization repo, only for homepage, wiki and issue tracker
https://primefaces-extensions.github.io/
70 stars 22 forks source link

Sheet: multiselect with 'ctrl' on numeric cells #766

Closed adjiandov closed 4 years ago

adjiandov commented 4 years ago

1) Environment PrimeFaces version: 6.2.2 Application server + version: JBoss Wildfly 10.0.0 Affected browsers: all 2) Expected behavior Whenever I try to select several cells with space between them (with 'ctrl') the clicked cells should be marked as selected. ...

3) Actual behavior Whenever a numeric cell is clicked then another numeric cell is clicked while holding ctrl, the first cell looses selection and only the second cell is selected (the same as if ctrl was not pressed). The issue is reproducible always when the first selected cell is numeric. If the start of the selection is a non-numeric cell the issue is not reproducible. ..

4) Steps to reproduce Open the showcase (Basic Usage) and try to multi-select several cells in the Price column with ctrl pressed

melloware commented 4 years ago

Most likely related to this method which handles Numeric cells.

https://github.com/primefaces-extensions/core/blob/06b61bc1f9d8fb291663929fb00fb2ee42e5c6f9/src/main/resources/META-INF/resources/primefaces-extensions/sheet/1-sheet.js#L510-L550

melloware commented 4 years ago

Fixed with commit: https://github.com/primefaces-extensions/core/commit/946e568d8c8ac40dc8b567c21fadf59ab640e306

You can try adding this Javascript to your page load.

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;

    // prevent Alpha chars in numeric sheet cells
    if (celltype === "numeric") {
        var evt = e || window.event; // IE support
        var key = evt.charCode || evt.keyCode || 0;

        // #766 do not block if just CTRL key
        if (key === 17) {
            return;
        }

        // check for cut and paste
        var isClipboard = false;
        var ctrlDown = evt.ctrlKey || evt.metaKey; // Mac support

        // 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 == 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) || e.shiftKey) {
            // prevent alpha characters
            e.stopImmediatePropagation();
            e.preventDefault();
        }
    }
};