2

jqgrid edit and add forms coontain textareas which can edited and enter adds new line to them.

How to allow Ctrl+S to save edit and add forms, just like clicking to save button in those forms ?

For inline edit I was able to add Save button to jqgrid toolbar and use

    case 83: $("#grid_savebutton").click(); break;

In body onkeydown event to simulate pressing to this button by Ctrl+S

How to allow also to save in edit and add forms by Ctrl+S ?

Update

I added code

        case 83: saveb = $("#TblGrid_" + "grid"+ "_2 #sData");
            if (saveb.length > 0 ) {
                   evt.stopPropagation();
                   return;
                  }
            $("#grid_savebutton").click(); 
            break;

to body onkeydown handler. After first edit form open this code finds always that sData button exists and goes not invoke grid_savebutton anymore. It looks like sData button exists even if edit/add form is closed. How to fix it so that if edit/add form is not open, $("#grid_savebutton").click() is executed ?

Update2

keydown bindings used:

            jQuery.extend(jQuery.jgrid.edit, {
               savekey: [true, 13],
               recreateForm: true,
               closeOnEscape: true,
               reloadAfterSubmit: false,
               beforeShowForm: function ($form) {
                 var gridIdEncoded = $.jgrid.jqID($form[0].id.substring(8));
                 $("#editmod" + gridIdEncoded).bind( 'keydown', beforeShowFormHandler);
                  },

               onClose: function () {
                 var gridIdEncoded = 'grid'; // $.jgrid.jqID($form[0].id.substring(8));
                 $("#editmod" + gridIdEncoded).unbind( 'keydown', beforeShowFormHandler);
                  }
            });


    var beforeShowFormHandler = function (e) {
        var gridIdEncoded = 'grid';
        if (e.ctrlKey && e.which === 83) { // 83 - 's'
          $("#TblGrid_" + gridIdEncoded + "_2 #sData").trigger("click");
          return false;
          }
    };

global keydown binding:

$(function () {
    $("html").keydown(body_onkeydown);
});


function body_onkeydown(evt) {
    var saveb;
    if (evt.ctrlKey) {
        switch (evt.keyCode) {
            case 83: 
              saveb = $("#TblGrid_" + "grid" + "_2 #sData");
                // todo: saveb.length > 0 is always true after form is opened first time: 
                if (saveb.length > 0) return;
                $("#grid_savebutton").click(); break;
               }
        cancel(evt);
        return false;
    }
}

function cancel(evt) {
    evt.returnValue = false;
    evt.keyCode = 0;
    evt.cancelBubble = true;
    evt.preventDefault();
    evt.stopPropagation();
}

Update3

TinyMCE html editor is attached to textarea elemnts in form in afterShowForm event using

    $('.htmleditor', formSelector).attr('cols', '50').attr('rows', '15').tinymce({
        theme: "advanced",
        language: "et",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter," +
"justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,|,cut ,copy,paste,undo,redo" +
"link,unlink,image,cleanup,code,hr,|,removeformat,formatselect,|,fontselect,fontsizeselect," +
"sub,sup,|,forecolor,backcolor,forecolorpicker,backcolorpicker,charmap,visualaid," +
"anchor,blockquote"
    });
}

Ctrl+S in textarea causes IE9 standard save dialog to appear in this case. How to allow Ctrl+S to save form in tinyMCE also ?

Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

2

You can register an additional keydown event handler inside of beforeShowForm. The code can be like the following:

beforeShowForm: function ($form) {
    // $form[0].id is like "FrmGrid_list"
    var gridIdEncoded = $.jgrid.jqID($form[0].id.substring(8));
    $("#editmod" + gridIdEncoded).keydown(function (e) {
        if (e.ctrlKey && e.which === 83) { // 83 - 's'
            $("#TblGrid_" + gridIdEncoded + "_2 #sData").trigger("click");
            return false; // stop propagation
        }
    });
}

You can see live demo here. Because I use local editing without setting any editurl you see an error message on pressing Ctrl + S in the edit form. It means that submiting will be fire, but not successful.

UPDATED: Look at the demo. On pressing Ctrl + S it displays alert "Ctrl-S in body" if the edit form is close and try to submit the form if the edit form is opened. Is it not what you need? In the code (see below) I used keydown from the formEvent namespace (just a free name)

$("#list").jqGrid ('navGrid', '#pager',
    {add: false, del: false, refresh: true, view: false},
    {beforeShowForm: function ($form) {
        // $form[0].id is like "FrmGrid_list"
        var gridIdEncoded = $.jgrid.jqID($form[0].id.substring(8));
        $("#editmod" + gridIdEncoded).bind('keydown.formEvent', function (e) {
            if (e.ctrlKey && e.which === 83) { // 83 - 's'
                $("#TblGrid_" + gridIdEncoded + "_2 #sData").trigger("click");
                return false;
            }
        });
    },
    onClose: function (formselector) {
        $(formselector).unbind('keydown.formEvent');
    }}, {}, {}, {multipleSearch: true, overlay: false});

$("html").keydown(function (e) {
    if (e.ctrlKey && e.which === 83) { // 83 - 's'
        alert("Ctrl-S in body");
        return false;
    }
});
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much. There is also global ctrl+S handler on body onkeydown as shown in question which clicks in save button in jqgrid top toolbar. This handler should executed only if edit/add form is not active. However this global handler is executed also if edit/add form is active, it cancels event bubbling and thus prevents your handler execution. How to execute proper button click in both cases ? – Andrus Oct 23 '11 at 19:10
  • @Andrus: I am not sure that I understand you correct. You explain some complex construct which you use: "save button in jqgrid top toolbar", execution of some handler only if edit/add form is active. I just answered on your main question: "How to save in add and edit forms by pressing ctrl+s in jqgrid?". If the grid has id="list" you can simulate Ctrl-S with `$("#TblGrid_list_2 #sData").trigger("click");`. Additional logic which you describes depend on your implementation. I recommend you to use `beforeShowForm` and `onClose` to hold more simple logic. – Oleg Oct 23 '11 at 19:24
  • thank you very much. I updated question and described issue more presicely. – Andrus Oct 24 '11 at 15:44
  • @Andrus: I don't understand why you use `$("#grid_savebutton").click()`. The id of he button is 'sData' (see my answer) and to simulate the click you should use `$('#sData').trigger("click")`. – Oleg Oct 24 '11 at 16:08
  • grid_savebutton is jqgrdi top toolbar custom button id. If edit/add form is not active, Ctrl+S should activate this button. If edit/add form is active, ctrl+S should activate edit/add form save button instead. – Andrus Oct 24 '11 at 17:23
  • 1
    @Andrus: I don't full understand all your construction especially the reason of additional top toolbar, but in case of Edit/Add form you can just follows my suggestion. You can bind `keydown` in `beforeShowForm` and unbind it in `onClose`. So during the form is opened the Ctrl-S will be controlled by `keydown` for the form. If the form is closed another event handler which you implemented control the `keydown` event. – Oleg Oct 24 '11 at 18:49
  • thank you. I added bind/undbind. Problem persists. If edit/add form is opened in IE9, body keydown method processes Ctrl+S key and event handler defined in beforeShowForm is **not executed** . bosy onkeydown event contains other key handlers also so it cannot unbinded in edit form is open. How to execute handler defined in beforeShowForm ? – Andrus Oct 25 '11 at 11:13
  • @Andrus: To find out the reason of the problem which you describe you have to examine all `keydown` binding and unbinding which you use and which uses jqGrid. It could be helpful if you would use namespaces during binding/unbinding of the `keydown` event. – Oleg Oct 25 '11 at 11:56
  • thank you. I updated question and provided all keydown bindings used. How to allow Ctrl+S to execute sData button click if edit/add form is opened and to execute grid_savebutton click if edit/add form is closed ? Is it possible to destroy sData button in onClose, check for edit/add form opened in body_onkeydown, use global variable or other idea ? – Andrus Oct 25 '11 at 13:53
  • Thank you. Currently both ctrl+s and enter in add and edit form does not close form. How to change this so that enter closes form (or in textarea adds new line to textarea) but ctrl+s does not close form as currently, only saves ? – Andrus Oct 29 '11 at 19:02
  • @Andrus: The usage of different behavior with "Enter" and "Ctrl-S" key in combination with different behavior for the form closing and some additional aspects for textarea seems for me very far from your original question. – Oleg Oct 31 '11 at 11:41
  • tinyMCE html editor is added in afterShowForm to textarea elements. Ctrl+S in textarea causes IE9 browser standard save dialog to appear. How to enable Ctrl+S to work in this case also ? I updated question – Andrus Apr 06 '12 at 20:05
  • @Andrus: I don't use TinyMCE myself. Moreover I don't want to come back to the question which I answered a half of years before. – Oleg Apr 06 '12 at 20:16
  • I'm sorry for that. I posted new question in http://stackoverflow.com/questions/10048756/how-to-catch-ctrls-key-in-tinymce-in-jqgrid-edit-form . Can you recommend some other way to edit html visually so that ctrl+s works ? – Andrus Apr 06 '12 at 20:23