Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to capture an 'enter' key press when focus is on a DataTable #5423

Closed
reriley opened this issue Dec 22, 2024 · 4 comments
Closed

Ability to capture an 'enter' key press when focus is on a DataTable #5423

reriley opened this issue Dec 22, 2024 · 4 comments

Comments

@reriley
Copy link

reriley commented Dec 22, 2024

I currently seem unable to capture a press of the enter key when focused on the DataTable widget. This would allow for more intuitive selection.

Here's the code I had hoped would work:

    BINDINGS = [
        Binding('i', 'toggle_id', 'Show/hide ID'),
        Binding('enter', 'select', 'Select/Deselect', show=False)
    ]

    def compose(self):
        yield DataTable(id='people_table')

    def on_mount(self):
        table: DataTable = self.query_one("#people_table")
        table.cursor_type = 'row'
        table.add_columns(*COLS)
        for p in serve_people():
            table.add_row(p.name, p.address_line_1, p.address_line_2, p.first_tax_year, key=p.id, label=p.id)
    
    def on_show(self):
        table: DataTable = self.query_one("#people_table")
        table.show_row_labels = False

    def action_select(self):
        table: DataTable = self.query_one("#people_table")
        cursor_type = table.cursor_type
        match cursor_type:
            case 'row':
                table.cursor_type = 'cell'
            case 'cell':
                pass

    def action_toggle_id(self) -> None:
        table: DataTable = self.query_one("#people_table")
        table.show_row_labels = True if table.show_row_labels is False else False`
Copy link

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@reriley
Copy link
Author

reriley commented Dec 22, 2024

I searched for hours for some explicit explanation of what to do for this functionality and of course found it just after posting. https://textual.textualize.io/guide/input/#keyboard-input provides the answer in the form of Key Methods, namely a method called 'key_enter'.

@reriley reriley closed this as completed Dec 22, 2024
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@willmcgugan
Copy link
Collaborator

I would advise against using key_ methods. They should be considered convenient for quick experiments, but bindings are preferred.

I suspect your example doesn't work because the DataTable already binds 'enter'. Which will send a DataTable.CellSelected message. If you want to change that behavior, you can extend the DataTable class and add your bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants