Checking for Ctrl key onReturn

Checking for Ctrl key onReturn

Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

Probably a basic JS or JQuery question, but I'd like to modify the formOptions onReturn() function to check if the control key is also pressed. If it is then I'd like the editor to submit the current data and immediately reopen to add another record (for a rapid ability to add entries without clicking the "Create" button in between records).

onReturn: function () {
  if (/* control key is pressed*/) {
    this.submit( () => {
      this.create();
    } );
  } else {
    this.submit();
  } 
}

However, I can't figure out how to easily check if the control key is pressed without some elaborate onkeydown setup outside of the onReturn function.

Any ideas on how to do this?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    e.ctrl will tell you if control was pressed.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited October 2018

    That's what I thought, but I get it as undefined.

    Here's where I look for it in the console log:

                                        editor_sa_conferences = new $.fn.dataTable.Editor({
                                            "table": "#sa_conferences",
                                            "formOptions": {
                                                "main": {
                                                    "onReturn": function() {
                                                        console.log(e.ctrl);
                                                    }
                                                },
                                                "inline": {
                                                    "submit": "allIfChanged",
                                                    "drawType": "none"
                                                }
                                            },
                                            "ajax": {
                                                "url": "/wiki/extensions/GHSFHA/models/m_sa_conferences.php",
                                                "type": "POST",
                                                "data": {
                                                    "currentUser": "Loren Maxwell"
                                                }
                                            },
                                            "fields": [{
                                                "name": "id",
                                                "def": null,
                                                "label": "Id",
                                                "type": "display"
                                            }, {
                                                "name": "season_name",
                                                "def": null,
                                                "label": "Season"
                                            }, {
                                                "name": "organization",
                                                "def": null,
                                                "label": "Organization"
                                            }, {
                                                "name": "class",
                                                "def": null,
                                                "label": "Class"
                                            }, {
                                                "name": "conference_name",
                                                "def": null,
                                                "label": "Conference name"
                                            }, {
                                                "name": "abbrv",
                                                "def": null,
                                                "label": "Abbreviation"
                                            }, {
                                                "name": "cross_count",
                                                "def": null,
                                                "label": "Cross count",
                                                "type": "checkbox",
                                                "separator": "|",
                                                "options": [{
                                                    "label": "",
                                                    "value": 1
                                                }],
                                                "unselectedValue": 0
                                            }]
                                        }).on('open', function(e, mode, action) {
                                            if (mode === 'main') {
                                                sa_conferences.keys.disable();
                                            }
                                        }).on('close', function() {
                                            sa_conferences.keys.enable();
                                        });
    

    Am I overlooking something?

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    According to the docs, onReturn pass one parameter when used as a function:

    A single parameter is passed in, the Editor instance.

    It sounds like you need to add the parameter, something like this:

                "onReturn": function( e ) {
                    console.log(e.ctrl);
                }
    

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited October 2018

    Thanks, Kevin.

    I had tried that also, but e in this case is an instance of Editor, which doesn't have a ctrl property and so is undefined.

    I'm not a jQuery expert by any stretch, but the example of e.ctrl that I've seen so far refers to e as an event object.

    Here's a SO question that highlgiths what I'm referring to:
    https://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery

    For the onReturn function, I'm not sure how to access an event object or if there even is one.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited October 2018

    The docs actually need to be updated here (done - will be deployed shortly) - it does pass the original event in that you can use, but as the second parameter:

    "onReturn": function( editor, ev ) {
        console.log(ev.ctrl);
    }
    

    should do it.

    Sorry for the confusion. I had thought you were modifying the Editor code internally.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    Thanks, @allan. Unfortunately I'm still getting undefined in the console, event when I just do console.log(ev).

    Just to make sure there's no syntax errors, here's the new code for the instance of editor, which is the same as above except for the onReturn function you've specified above:

    editor_sa_conferences = new $.fn.dataTable.Editor({
        "table": "#sa_conferences",
        "formOptions": {
            "main": {
                "onReturn": function(editor, ev) {
                    console.log(ev.ctrl);
                }
            },
            "inline": {
                "submit": "allIfChanged",
                "drawType": "none"
            }
        },
        "ajax": {
            "url": "/wiki/extensions/GHSFHA/models/m_sa_conferences.php",
            "type": "POST",
            "data": {
                "currentUser": "Loren Maxwell"
            }
        },
        "fields": [{
            "name": "id",
            "def": null,
            "label": "Id",
            "type": "display"
        }, {
            "name": "season_name",
            "def": null,
            "label": "Season"
        }, {
            "name": "organization",
            "def": null,
            "label": "Organization"
        }, {
            "name": "class",
            "def": null,
            "label": "Class"
        }, {
            "name": "conference_name",
            "def": null,
            "label": "Conference name"
        }, {
            "name": "abbrv",
            "def": null,
            "label": "Abbreviation"
        }, {
            "name": "cross_count",
            "def": null,
            "label": "Cross count",
            "type": "checkbox",
            "separator": "|",
            "options": [{
                "label": "",
                "value": 1
            }],
            "unselectedValue": 0
        }]
    }).on('open', function(e, mode, action) {
        if (mode === 'main') {
            sa_conferences.keys.disable();
        }
    }).on('close', function() {
        sa_conferences.keys.enable();
    });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Oops - sorry. That's something that is coming in 1.8. I just looked at the source and forgot that it was a fairly recent addition! Just working on wrapping up the documentation for 1.8 and the packaging at the moment.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    Ah, ok.

    Looking forward to it for several reasons!

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    @allan, I upgraded to Editor 1.8.0 today and revisited this and I'm still not getting the original event passed to the OnReturn event.

    Again for reference here's my instance of Editor:

        editor_sa_conferences = new $.fn.dataTable.Editor({
            "table": "#sa_conferences",
            "formOptions": {
                "main": {
                    "onReturn": function (editor, ev) {
                        console.log(ev);
                    }
                },
                "inline": {
                    "submit": "allIfChanged",
                    "drawType": "none"
                }
            },
            "ajax": {
                "url": "/wiki/extensions/GHSFHA/models/database/m_sa_conferences.php",
                "type": "POST",
                "data": {
                    "currentUser": "Loren Maxwell"
                }
            },
            "fields": [{
                "name": "id",
                "def": null,
                "label": "Id",
                "type": "display"
            }, {
                "name": "season_name",
                "def": null,
                "label": "Season"
            }, {
                "name": "organization",
                "def": null,
                "label": "Organization"
            }, {
                "name": "class",
                "def": null,
                "label": "Class"
            }, {
                "name": "conference_name",
                "def": null,
                "label": "Conference name"
            }, {
                "name": "abbrv",
                "def": null,
                "label": "Abbreviation"
            }, {
                "name": "cross_count",
                "def": null,
                "label": "Cross count",
                "type": "checkbox",
                "separator": "|",
                "options": [{
                    "label": "",
                    "value": 1
                }],
                "unselectedValue": 0
            }]
        }).on('open', function (e, mode, action) {
            if (mode === 'main') {
                sa_conferences.keys.disable();
            }
        }).on('close', function () {
            sa_conferences.keys.enable();
        });
    
  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    Just to add some clarification with 1.8.0, it is executing my custom OnReturn function, but the event object doesn't seem to be passed along. I've found the spot in the Editor code that passes it (or at least I think I have at line 5290 in the un-minified version), but for some reason it doesn't come through.

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    OK, another piece of information -- I can get it to work in the un-minified version, but not the minified version.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Doh - sorry. Its ctrlKey!

    "onReturn": function( editor, ev ) {
        console.log(ev.ctrlKey);
    }
    

    If you load this example and then in the console use:

    $('#example').on('click', 'td', function () {
      editor.inline( this, {
        onReturn: function( editor, ev ) {
          console.log(arguments, ev.ctrlKey);
        }
      } );
    } );
    

    it will show the state of the control key (note the first column in that specific table won't work for inline editing).

    If you have the debug version working but not the minified one, it suggests that the minifed one might be cached as the old version.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited October 2018

    It was the cache . . . ITWASTHECACHE!!! AHHHHHHH!!!!!!!!!

    I cleared it for the debug version to troubleshoot (and saw that it was ctrlKey) but never thought to do so for the minified one!

    Anyway, works perfect now. Thanks, Allan!

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited October 2018

    Just following up to post my final solution on this one.

    Now when a user presses return, the form submits and closes. If they press control+return, it submits and reopens for another entry.

    There's also a button on the create form that does the same thing if they press that.

    var editor_sa_conferences;
    $(function () {
        editor_sa_conferences = new $.fn.dataTable.Editor({
            "table": "#sa_conferences",
            "formOptions": {
                "main": {
                    "onReturn": function (editor, ev) {
                        if (ev.ctrlKey) {
                            editor.submit(() => {
                                editor.create();
                            });
                        } else {
                            editor.submit();
                        }
                    }
                },
                "inline": {
                    "submit": "allIfChanged",
                    "drawType": "none"
                }
            },
            "ajax": {
                "url": "/wiki/extensions/GHSFHA/models/database/m_sa_conferences.php",
                "type": "POST",
                "data": {
                    "currentUser": "Loren Maxwell"
                }
            },
            "fields": [{
                "name": "id",
                "def": null,
                "label": "Id",
                "type": "display"
            }, {
                "name": "season_name",
                "def": null,
                "label": "Season"
            }, {
                "name": "organization",
                "def": null,
                "label": "Organization"
            }, {
                "name": "class",
                "def": null,
                "label": "Class"
            }, {
                "name": "conference_name",
                "def": null,
                "label": "Conference name"
            }, {
                "name": "abbrv",
                "def": null,
                "label": "Abbreviation"
            }, {
                "name": "cross_count",
                "def": null,
                "label": "Cross count",
                "type": "checkbox",
                "separator": "|",
                "options": [{
                    "label": "",
                    "value": 1
                }],
                "unselectedValue": 0
            }]
        }).on('open', function (e, mode, action) {
            if (mode === 'main') {
                var focused = false;
                var i = 0;
                do {
                    var fieldname = editor_sa_conferences.fields()[i];
                    if (editor_sa_conferences.field(fieldname).canReturnSubmit != null && editor_sa_conferences.field(fieldname).def() == null) {
                        editor_sa_conferences.field(fieldname).focus();
                        focused = true;
                    } else {
                        ++i
                    }
                } while (!focused && i < editor_sa_conferences.fields().length);
            }
        }).on('open', function (e, mode, action) {
            if (mode === 'main') {
                sa_conferences.keys.disable();
            }
        }).on('close', function () {
            sa_conferences.keys.enable();
        });
        var sa_conferences = $('#sa_conferences').DataTable({
            "paging": true,
            "autoWidth": true,
            "buttons": [{
                "extend": "create",
                "editor": editor_sa_conferences,
                "text": "<i class=\"fa fa-plus fa-fw\" aria-hidden=\"true\" title=\"Create\"></i>",
                "className": "btn-sm",
                "formButtons": [{
                    "text": "<i class=\"fa fa-floppy-o fa-fw\" aria-hidden=\"true\" title=\"Save\"></i> <i class=\"fa fa-plus fa-fw\" aria-hidden=\"true\" title=\"Add another\"></i>",
                    "action": function () {
                        this.submit(() => {
                            this.create();
                        });
                    }
                }, "<i class=\"fa fa-floppy-o fa-fw\" aria-hidden=\"true\" title=\"Save\"></i>", {
                    "text": "<i class=\"fa fa-times fa-fw\" aria-hidden=\"true\" title=\"Cancel\"></i>",
                    "action": function () {
                        this.close();
                    }
                }]
            }, {
                "extend": "edit",
                "editor": editor_sa_conferences,
                "text": "<i class=\"fa fa-edit fa-fw\" aria-hidden=\"true\" title=\"Edit\"></i>",
                "className": "btn-sm",
                "formButtons": ["<i class=\"fa fa-floppy-o fa-fw\" aria-hidden=\"true\" title=\"Save\"></i>", {
                    "text": "<i class=\"fa fa-times fa-fw\" aria-hidden=\"true\" title=\"Cancel\"></i>",
                    "action": function () {
                        this.close();
                    }
                }]
            }, {
                "extend": "remove",
                "editor": editor_sa_conferences,
                "text": "<i class=\"fa fa-trash fa-fw\" aria-hidden=\"true\" title=\"Delete\"></i>",
                "className": "btn-sm",
                "formButtons": ["<i class=\"fa fa-trash fa-fw\" aria-hidden=\"true\" title=\"Delete\"></i>", {
                    "text": "<i class=\"fa fa-times fa-fw\" aria-hidden=\"true\" title=\"Cancel\"></i>",
                    "action": function () {
                        this.close();
                    }
                }]
            }, {
                "extend": "csv",
                "text": "<i class=\"fa fa-file-excel-o fa-fw\" aria-hidden=\"true\" title=\"Export\"></i>",
                "className": "btn-sm"
            }],
            "serverSide": true,
            "ajax": {
                "url": "/wiki/extensions/GHSFHA/models/database/m_sa_conferences.php",
                "type": "POST",
                "data": []
            },
            "order": [1, "asc"],
            "select": {
                "style": "os",
                "selector": ".row-selector"
            },
            "columns": [{
                "defaultContent": "<i class=\"fa fa-chevron-right fa-fw\"></i>",
                "className": "row-selector",
                "orderable": false
            }, {
                "data": "id",
                "name": "id"
            }, {
                "data": "season_name",
                "name": "season_name",
                "className": " inline"
            }, {
                "data": "organization",
                "name": "organization",
                "className": " inline"
            }, {
                "data": "class",
                "name": "class",
                "className": " inline"
            }, {
                "data": "conference_name",
                "name": "conference_name",
                "className": " inline"
            }, {
                "data": "abbrv",
                "name": "abbrv",
                "className": " inline"
            }, {
                "data": "cross_count",
                "name": "cross_count",
                "className": "text-center",
                "render": function (data, type, row) {
                    if (type === 'display') {
                        return '<input type=\"checkbox\" class=\"editor-cross_count\">';
                    }
                    return data;
                },
                "orderSequence": ["desc", "asc"]
            }],
            "keys": {
                "editor": editor_sa_conferences,
                "columns": ".inline"
            },
            "scroller": {
                "loadingIndicator": true
            },
            "deferRender": true,
            "scrollX": true,
            "scrollY": "80vh",
            "scrollCollapse": true,
            "dom": "Brti",
            "rowCallback": function (row, data) {
                $('input.editor-cross_count', row).prop('checked', data.cross_count == 1);
            }
        }).on('click', 'td.row-selector', function () {
            sa_conferences.cell.blur();
        }).on('click', 'tbody td.inline', function () {
            sa_conferences.rows().deselect();
            editor_sa_conferences.inline(this);
        })
    });
    
This discussion has been closed.