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

DataTable: Allow Enter and Esc to be used in editing to confirm/cancel editing #433

Closed
mauromol opened this issue Jun 12, 2015 · 23 comments
Closed
Assignees
Labels
accessibility WCAG or ARIA related issues enhancement Additional functionality to current component
Milestone

Comments

@mauromol
Copy link

Having a <p:datatable> with a <p:rowEditor>, it would be quite natural to be able to use Enter to confirm editing and Esc to cancel it. Instead, these keys are currently ignored and captured by other elements in the page.

@mauromol
Copy link
Author

Currently, since there's no client-side API for doing this, it's a bit hard to make this work:
http://stackoverflow.com/questions/13287220/using-return-enter-key-to-save-edited-cell-in-primefaces-3-4-in-cell-editable-da

@cagataycivici
Copy link
Member

CellEditor supports these (editMode="cell"), by design we don't support it for row edit. If more users require it, I'll reopen.

@mauromol
Copy link
Author

OK Catagay, but please reconsider. That's the first thing I tried to do as soon as I started typing something in the fields of the edited row. I think it's also a matter of accessibility: right now, you are forced to take the mouse, move its pointer and click on the "check" icon to confirm.

I can't think of any good reason for which they shouldn't be implemented "by design".

@cagataycivici
Copy link
Member

Ok, I'll reopen to see if more users will demand it. I have some performance changes upcoming in 5.3 to row editor, maybe will do it along the way.

@cagataycivici cagataycivici reopened this Jun 15, 2015
@bsanders1979
Copy link

I've actually implemented this very functionality (not the escape key) and it was pretty easy to do. However, I added the escape piece because it's pretty simple. I can see why it's not implemented OOTB because it might be difficult to cover all use cases without overstepping boundaries.

<p:dataTable widgetVar="datatable" rowIndexVar="rowIndex">
    <p:column>
        <p:cellEditor>
            <f:facet name="input">
                <p:inputText
                    onkeydown="PF('datatable').onKeyDown(event)"
                    onkeyup="PF('datatable').onKeyUp(event, #{rowIndex})"
                />
            </f:facet>
        </p:cellEditor>
    </p:column>
</p:dataTable>

<script type="text/javascript">
//<![CDATA[
    $(function() {
        $.extend(PF("datatable"), {
            onKeyDown : function(e) {
                var key = e.which,
                    keyCode = $.ui.keyCode;

                if((key === keyCode.ENTER||key === keyCode.NUMPAD_ENTER)) {
                    e.preventDefault();
                }
            },

            onKeyUp : function(e, rowIndex) {
                var key = e.which,
                    keyCode = $.ui.keyCode;

                if((key === keyCode.ENTER||key === keyCode.NUMPAD_ENTER)) {
                    this.tbody
                        .find('.ui-row-editor .ui-icon-check')
                        .eq(rowIndex)
                        .click();
                }

                if (key === keyCode.ESCAPE) {
                    this.tbody
                        .find('.ui-row-editor .ui-icon-close')
                        .eq(rowIndex)
                        .click();
                }
            }
        });
    });
//]]/>
</script>

@mauromol
Copy link
Author

Actually, I'm currently using the code provided by BalusC on Stackoverflow with no issues so far. It's quite brief. I'm reporting it here how the Enter key is handled:

$(document).on("keydown", ".ui-cell-editor-input input", function(event) {
    if (event.keyCode == 13) {
        $(this).closest("tr").find(".ui-row-editor .ui-icon-check").click();
    }
});

@kannextic
Copy link

I'd like this function, or at least the option to enable it for RowEditor.

@victorcarvalhosp
Copy link

We need this option too... All of our customers click ENTER and think something is wrong...

@yannickb85
Copy link

We need this option too.

@bgmoon
Copy link

bgmoon commented Mar 2, 2017

Need this option for "in place" as well :)

@cagataycivici
Copy link
Member

Please create a separate ticket for inplace and we'll discuss.

@okworx
Copy link

okworx commented Mar 22, 2017

I'd like this function as well OOTB.

@ademcaliskan
Copy link

+1
That's the usual way I think. Click enter to accept changes and esc to cancel editing.

@tandraschko tandraschko changed the title Allow Enter and Esc to be used in datatable editing to confirm/cancel editing DataTable: Allow Enter and Esc to be used in editing to confirm/cancel editing Jan 5, 2019
@tandraschko tandraschko added the enhancement Additional functionality to current component label Jan 5, 2019
@ghost
Copy link

ghost commented Feb 1, 2019

+1

@melloware melloware added the accessibility WCAG or ARIA related issues label Sep 10, 2019
@elab
Copy link

elab commented Oct 7, 2019

Slightly modified version of the BalusC code cited by @mauromol above:

  • prevent adding new row to the table on "Enter" click
  • cancel editing on "Escape" click
$(document).on("keydown", ".ui-cell-editor-input input", function(event) {
    if (event.keyCode == 13) { // Enter
        $(this).closest("tr").find(".ui-row-editor .ui-icon-check").click();
        return false; // prevents executing other event handlers (adding new row to the table)
    }
    if (event.keyCode == 27) { // Escape
        $(this).closest("tr").find(".ui-row-editor .ui-icon-close").click();
        return false;
    }
});

@melloware
Copy link
Member

melloware commented Oct 7, 2019

@elab. I am using the showcase and your fix for Cell editor is already natively in the latest Showcase Cell Edit event: https://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml This ticket was about ROW editing right?

Nevermind: I see you are attaching to the Cell Editor to close the row editor.

@elab
Copy link

elab commented Oct 7, 2019

@melloware yes, the ticket is about the row editing.
I'm (actually, BalusC) attaching the handler to the cell editor as the key press is happening in an input element which is located under div with the "ui-cell-editor-input" class (while we are on row editing):

<div class="ui-cell-editor-input">
    <span id="form:table:12:j_idt61" class="ui-inputnumber ui-widget"
	    <input id="form:table:12:j_idt61_input" name="form:table:12:j_idt61_input" type="text" ...

(just checked that)

@melloware melloware self-assigned this Oct 7, 2019
@melloware melloware added this to the 7.1 milestone Oct 7, 2019
melloware added a commit to melloware/primefaces that referenced this issue Oct 7, 2019
@melloware
Copy link
Member

OK I submitted a PR. I modified all answers slightly.

  1. Use :input instead of input so if you are are a select dropdown you can still press ENTER or ESC. Previously it would only work if you were in an input box.

  2. Modified the ROW finder to use the more specific styles .ui-row-editor-check and ..ui-row-editor-close instead of .ui-row-editor .ui-icon-close

  3. used Jquery constants for ENTER and ESC.

@elab
Copy link

elab commented Oct 7, 2019

@melloware: you made me thinking about, we could in fact catch key events for the row itself (to automatically cover any input elements), this also simplifies the code a bit:

$(document).on("keydown", "tr.ui-row-editing", function(event) {
	if (event.keyCode == 13) { // Enter
		$(this).find(".ui-row-editor .ui-icon-check").click();
		return false; // prevents executing other event handlers (adding new row to the table)
	}
	if (event.keyCode == 27) { // Escape
		$(this).find(".ui-row-editor .ui-icon-close").click();
		return false;
	}
});

@melloware
Copy link
Member

@elab I think the problem is the ui-row-editing style does NOT appear until you actually click and edit a row. So when the component loads no rows are marked with ui-row-editing yet right?

@elab
Copy link

elab commented Oct 7, 2019

In the initial state no rows are marked with that, right.
But it works. It looks like browser's event processing always takes the current state of elements into consideration when processing events.

@melloware
Copy link
Member

Cool let me test it locally and update my PR.

melloware added a commit to melloware/primefaces that referenced this issue Oct 7, 2019
@melloware
Copy link
Member

Tested and its working great. PR updated.

melloware added a commit to melloware/primefaces that referenced this issue Oct 7, 2019
melloware added a commit to melloware/primefaces that referenced this issue Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility WCAG or ARIA related issues enhancement Additional functionality to current component
Projects
None yet
Development

No branches or pull requests