ce icon indicating copy to clipboard operation
ce copied to clipboard

Open dropdown when down arrow is pressed and close when the tab key is pressed

Open alfr3dosv opened this issue 6 years ago • 3 comments

Currently the only way the user can open the dropdown is by double clicking

At the moment I have tried this in a jexcel table with multiple sheets, the dropdown is opened with the down arrow key and closed with the tab key but does not save the selected value

    document.getElementById('spreadsheet').onkeydown = e => {
        if (e.code == 'ArrowDown') {
            let cell = document.querySelector('.jexcel_dropdown.highlight-selected');
            let instances = document.querySelectorAll('.jexcel_container');
            let currentInstance = Array.from(instances).find(c => c.style.display != 'none');
            let dropdownHasFocus = document.querySelector('.jdropdown-focus');
            if (cell && currentInstance && !dropdownHasFocus) {
                currentInstance.jexcel.openEditor(cell);
            }
        } else if (e.code == 'Tab') {
            let cell = document.querySelector('.jexcel_dropdown.highlight-selected');
            let instances = document.querySelectorAll('.jexcel_container');
            let currentInstance = Array.from(instances).find(c => c.style.display != 'none');
            let dropdownHasFocus = document.querySelector('.jdropdown-focus');
            if (cell && currentInstance && dropdownHasFocus) {
                currentInstance.jexcel.closeEditor(cell);
                // prevent lost focus
                currentInstance.focus();
            }
        }
    };

JSFiddle

alfr3dosv avatar Aug 20 '20 16:08 alfr3dosv

I was able to save the data, calling close on the dropdown instead of closeEditor on the cell

    document.getElementById('spreadsheet').onkeydown = e => {
        if (e.code == 'ArrowDown') {
            let cell = document.querySelector('.jexcel_dropdown.highlight-selected');
            let instances = document.querySelectorAll('.jexcel_container');
            let currentInstance = Array.from(instances).find(c => c.style.display != 'none');
            let dropdownHasFocus = document.querySelector('.jdropdown-focus');
            if (cell && currentInstance && !dropdownHasFocus) {
                currentInstance.jexcel.openEditor(cell);
            }
        } else if (e.code == 'Tab') {
            let cell = document.querySelector('.jexcel_dropdown.highlight-selected');
            let instances = document.querySelectorAll('.jexcel_container');
            let currentInstance = Array.from(instances).find(c => c.style.display != 'none');
            let currentDropdown = document.querySelector('.jdropdown-focus');
            if (cell && currentInstance && currentDropdown) {
                currentDropdown.dropdown.close();
                // prevent lost focus
                currentInstance.focus();
            }
        }
    };

JSFiddle

alfr3dosv avatar Aug 20 '20 16:08 alfr3dosv

Alternatively, you can create a button and call openEditor to start the edition of one specific cell.

pphod avatar Aug 20 '20 20:08 pphod

I have idea.

onselection: function (instance, x1, y1, x2, y2, origin) {
            if (x1 == 2) {
                var columnName = jexcel.getColumnNameFromId([x1, y1]);
                var cell = instance.jexcel.getCell(columnName);
                instance.jexcel.openEditor(cell);
                cell.children[0].dropdown.close(true);
            }
        },

User can open dropdown by single clicking.

whyJoe avatar Jan 07 '21 02:01 whyJoe