0

I have 3 checkbox columns with corresponding date column. So what I am trying to achieve is when user clicks on checkbox during inline-edit and hits enter, today's date will be sent set as the value for another cell in a specified column. How to achieve? I recieved prior direction from OLEG on doing this with on form-edit but I would like to do this during inline edit instead. I have both enabled but only checkmarks will be editable during inline edit. I have all other fields disabled but the checkboxes columns. Any ideas would be greatly appreciated.

UPDATE Okay so I solved with the below code. Thanks Oleg for the help.

Current Date Function:

var date = new Date(Date.now());
console.log(todayDate(date));
function todayDate(date){
return (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
    } 

Check Column Code:

        { name: "devc", index: "devc",width: 25, align: "right", formatter: "checkbox", 
    editable: true, edittype: "checkbox",editoptions: {value: "Yes:No",defaultValue: "No",
dataEvents: [{type: "change", fn: function (e) {var $this = $(e.target), columnName = "devdate", rowid, cellId;
                                if ($this.hasClass("FormElement")) {
                                    // form editing
                                    cellId = columnName;
                                } else if ($this.closest("td").hasClass("edit-cell")) {
                                    // cell editing
                                    return; // no other editing field exist - nothing to do
                                } else {
                                    // inline editing
                                    rowid = $this.closest("tr.jqgrow").attr("id");
                                    cellId = rowid + "_" + columnName;
                                }
                                $("#" + $.jgrid.jqID(cellId)).val($this.is(":checked") ?
                                    (todayDate(date)) : "");} }]}}
NewHistoricForm
  • 121
  • 1
  • 8
  • 26

1 Answers1

1

To be able to address another editing field inside of some event handler defined in dataEvents you need uses the corresponding name conversion for ids used by different editing mode.

Form editing creates editing elements with id attribute the same as the name property in colModel. Inline editing allows to edit multiple rows at the same time. So it uses another rule for building id values: the rowid value will be appended by _ and then appended with the value of name property in colModel. During cell editing jqGrid allows to edit one cell only. So accessing of another editing field inside of some event handler defined in dataEvents have no sense.

The answer provide an example of detection of editing mode inside of event handler of dataEvents.

Let us you have column { name: "appc", formatter: "checkbox", editable: true, edittype: "checkbox", ...} and you need to define change event handler for the column. Let us you have another text column { name: "appdate", editable: true, ...} and you want change the value of the the edit field of appdate column on checking/unchecking of the checkbox in appc column. In the case you can do the following:

{ name: "appdate", editable: true, ...},
{ name: "appc", formatter: "checkbox", editable: true, edittype: "checkbox",
    editoptions: {
        dataEvents: [{
            type: "change",
            fn: function (e) {
                var $this = $(e.target), columnName = "appdate", rowid, cellId;
                if ($this.hasClass("FormElement")) {
                    // form editing
                    cellId = columnName;
                } else if ($this.closest("td").hasClass("edit-cell")) {
                    // cell editing
                    return; // no other editing field exist - nothing to do
                } else {
                    // inline editing
                    rowid = $this.closest("tr.jqgrow").attr("id");
                    cellId = rowid + "_" + columnName;
                }

                // set the value of another edit field
                // we use below $.jgrid.jqID(cellId) instead of cellId
                // only to be sure that the next line work correctly
                // even in case of columnName contains special meta-charackters
                // like space or !"#$%&'()*+,./:;<=>?@[\]^`{|}~
                $("#" + $.jgrid.jqID(cellId)).val($this.is(":checked") ?
                    "CheckedText": "UncheckedText");
            } 
        }]
    }
}

UPDATED: One can modify the code from your question to the following:

var date = new Date(Date.now()),
    todayDate = function (date) {
        return (date.getMonth() + 1) + "/" + date.getDate() +
            "/" + date.getFullYear();
    },
    checkBoxChange = function (e) {
        var $this = $(e.target), columnName = e.data.dateColumn,
            rowid, cellId;

        if ($this.hasClass("FormElement")) {
            // form editing
            cellId = columnName;
        } else if ($this.closest("td").hasClass("edit-cell")) {
            // cell editing
            return; // no other editing field exist - nothing to do
        } else {
            // inline editing
            rowid = $this.closest("tr.jqgrow").attr("id");
            cellId = rowid + "_" + columnName;
        }
        $("#" + $.jgrid.jqID(cellId)).val($this.is(":checked") ?
            (todayDate(date)) : "");
    };

...
...
{ name: "appdate", editable: true, ...},
{ name: "appc", formatter: "checkbox", editable: true,
    edittype: "checkbox",
    editoptions: {
        dataEvents: [{
            type: "change",
            data:  { dateColumn: "appdate" },
            fn: checkBoxChange
        }]
    }},
{ name: "devdate", editable: true, ...},
{ name: "devc", formatter: "checkbox", editable: true,
    edittype: "checkbox",
    editoptions: {
        dataEvents: [{
            type: "change",
            data:  { dateColumn: "devdate" },
            fn: checkBoxChange
        }]
    }}

One can see that we use data: { dateColumn: "devdate" } or data: { dateColumn: "appdate" } as additional property of dataEvents items. The event handler checkBoxChange used in fn: checkBoxChange can access the data by usage e.data.dateColumn. In the way one can easy share the same event handler for multiple columns.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    @NewHistoricForm: You don't wrote clear enough what you want to write in the field. *You can write the value which you need.* In the text of the question you wrote that you what to set current date only if some field is empty. Probably you need to test `$("#" + $.jgrid.jqID(cellId)).val()` and if it's non-empty and the checkbox are set then you can set the current date. The rule is that the question have some value if it's not very specific. I don't believe that some other person will need to set date based on some checkbox, but I believe that one could access (get/set) *another* editing field – Oleg Sep 22 '14 at 19:16
  • I'll figure out how to adjust the value. Thanks for you guidance. – NewHistoricForm Sep 22 '14 at 19:48
  • Minor question: How to address changing multiple columns? – NewHistoricForm Mar 19 '15 at 23:17
  • @NewHistoricForm: Sorry, but I don't understand your question. Do you want to use the same `fn` in multiple columns? Then you need just define the event handler function not as anonymous function. – Oleg Mar 20 '15 at 08:16
  • I mean that the checkmark event changes the value in 3 columns not just 1. Could you help? – NewHistoricForm Mar 20 '15 at 15:06
  • 1
    @NewHistoricForm: If I correctly understand your problem you can follow **UPDATED** part of my answer. – Oleg Mar 20 '15 at 17:21
  • I see. So if I woud use `data: { dateColumn: "appdate", "otherColumn","yetotherColumn"},` would this change all these columns together?Because this is what I seek. – NewHistoricForm Mar 20 '15 at 17:51
  • 1
    @NewHistoricForm: Why you not just describe **exactly** what you want? The usage of `data: { dateColumn: "appdate", "otherColumn","yetotherColumn"},` is not possible because it's syntax error. I understood you so that you have 6 columns: 3 dates and 3 checkboxs. I posted the code with 2+2 columns instead of 3+3. **Could you describe exactly your problem?** Which columns of which type you have, why you can't use the existing solution, what you really need to implement? – Oleg Mar 20 '15 at 18:48
  • Say that `checkBoxChange` changes values of 2+ other columns at the same time, to address change value of multiple column . I want `dateColumn` to contain data for multiple columns so that when checkbox is checked multiple columns are changed at once. Possible? – NewHistoricForm Mar 20 '15 at 20:12
  • 1
    @NewHistoricForm: You need just uses the selector `$("#" + $.jgrid.jqID(cellId1) + ",#" + $.jgrid.jqID(cellId2) + ",#" + $.jgrid.jqID(cellId3))` instead of `$("#" + $.jgrid.jqID(cellId))` in the last line which set `val`. – Oleg Mar 20 '15 at 20:13
  • You have been really helpful. Would this be the same for `edittype:'select'`? I've tried with select and it does not fire. – NewHistoricForm Mar 20 '15 at 22:23