Skip to content

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

Description

@alfr3dosv

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

Activity

  1. alfr3dosv commented on Aug 20, 2020

    @alfr3dosv
    Author

    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

  2. pphod commented on Aug 20, 2020

    @pphod
    Contributor

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

  3. whyJoe commented on Jan 7, 2021

    @whyJoe

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions