0

I need to have an editype checkmark column where when checkmark is checked today's date automatically is added next to checkmark and after submit to cell and if unchecked the date is removed on editform and on submit sends "null" to leave cell empty. I know I can use use

{ name: 'MyCol', index: 'MyCol', editable:true, edittype:'checkbox', editoptions: { value:"True:False" }

But I have no idea how to display the date next to the checkbox(like pic)and then add that date to cell on submit. Help please. Date added to edit project and on Submit date sent to column My code as it currently as it exists UPDATED with answer:

`

<title>PWe</title>


<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.10.3.custom.min.css" />

<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/nhf.css" />

<script src="js/jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
 <script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
 <script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

<script type="text/javascript">




 $(function(){ 
      $("#list").jqGrid({
        url:'request.php',
        editurl: "jqGridCrud.php",
        datatype: 'xml',
        mtype: 'GET',
        height: 'AUTO',
        width: 900,
        scrollOffset:0,
        hidegrid: false,

        colNames:['id','Project', 'Assigned To','Dev','Approve','Due Date','Attachments','Notes','App','mydate2',""],
        colModel :[ 
          {name:'id', index:'id', width:28, align: 'center'}, 
          {name:'name', index:'name', width:170, align:'left',editable:true, editoptions:{
                size:60} }, 
          {name:'id_continent', index:'id_continent', width:50, align:'right',editable:true,edittype:'select', 
          editoptions:{value: "Henry:Henry; Ramon:Ramon; Paul:Paul" },mtype:'POST'  }, 

          {name:'lastvisit', index:'lastvisit', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy',editable:true, edittype: 'text',mtype:'POST' ,       editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,


          {name:'cdate', index:'cdate', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy', edittype: 'text',editable:true ,mtype:'POST' ,editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,

          {name:'ddate', index:'ddate', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy',date:'true',editable:true, edittype: 'text',editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,


          {name:'email', index:'email', width:50,align:'center',sortable:false,mtype:'POST',formatter:function(cellvalue, options, rowObject) {
        if (cellvalue === undefined || cellvalue === null || cellvalue === 'NULL') {
            return ''; // or just ""
        }
        return '<a href="' + cellvalue + '"target="_blank"><img src="file.png"> </a>';
    }},

            {name:'notes', index:'notes', width:100, align:'left',sortable:false, editable:true,edittype:'textarea',formatter:'link', editoptions:{
                rows:10,cols:60} }, 



    { name: "hello", index:'hello',width: 17, align: "right", formatter: "checkbox",
            editable: true, 
            edittype: "checkbox",
            editoptions: {
                value: "Yes:No",
                defaultValue: "Yes",
                dataEvents: [
                    {
                        type: "change",
                        data: { uncheckedDate: "" },
                        fn: function (e) {
                            if ($(this).is(':checked')) {
                                var date = new Date(Date.now()),
                                    strDate = (date.getMonth() + 1) + "/" +
                                              date.getDate() + "/" +
                                              date.getFullYear();
                                $("#mydate").text(strDate);
                            } else {
                                $("#mydate").text(e.data.uncheckedDate);
       }
                        }
                    }
                ]
            }},


        {name:'mydate2', index:'mydate2', width:40, align:'left',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy',date:'true',editable:false} ,

            {name:'act',index:'act',width:30 ,align:'left', sortable:false,formatter: "actions",cellattr: function () { return ' title="Delete Project"'; },
    formatoptions: {
        keys: true,
         deltitle: 'delete',
        delbutton: true,
        editbutton:false,
        delOptions: {
            url: 'delete-perm.php',
            afterShowForm: function ($form) {
        $("#dData", $form.parent()).click();
    },
            msg: "Remove Selected Project?",
            bSubmit: "Remove",
            bCancel: "Cancel"
        }
    }}, 
        ],
        pager: '#pager',

        rowNum:30,
        rowList:[30,40,80],
        sortname: 'ddate',
        sortorder: 'asc',
        viewrecords: true,
        gridview: true,
        caption: 'Current Assignments',


        ondblClickRow: function(rowid) {

        $(this).jqGrid('editGridRow', rowid,
                            {width:550,Height:550,recreateForm:true,closeAfterEdit:true,
                             closeOnEscape:true,reloadAfterSubmit:true, modal:true,mtype:'post',top:350,left: 30});}


                });


     jQuery.extend(jQuery.jgrid.nav, {
            deltitle: '',
            delcaption: 'Project Complete'


        },{delicon: "ui-icon-circle-check",deltext: "Project Complete"});   

        $("#list").jqGrid("navGrid", "#pager", { add: false, search: false, refresh:false,edit:false }).navButtonAdd('#pager',{


                                    caption:"Export to Excel", 
                                    buttonicon:"ui-icon-save", 
                                    onClickButton: function () {
            jQuery("#list").jqGrid('excelExport', { url: 'ExportExcel.php' });
    }, 
                                    position:"last",

                                });
    jQuery.extend(jQuery.jgrid.edit, {
    recreateForm: true,
    beforeShowForm: function ($form) {
    $("<span id='mydate'></span>").insertAfter("#hello");
    $("#hello").trigger("change"); // to set date
    },
    onclickSubmit: function () {
    return {
    mydate2: $("#mydate").text()
    }
    },
    afterclickPgButtons: function () {
    $("#hello").trigger("change"); // to set date
    } });
        // setup grid print capability. Add print button to navigation bar and bind to click.
        setPrintGrid('list','pager','Current Assignments');

    });
NewHistoricForm
  • 121
  • 1
  • 8
  • 26

1 Answers1

2

There are many ways to implement your requirements. The first one is the usage of edittype: "custom". It allows you to create any editing element in edit form. The same approach work in inline editing or in Searching dialog. The demo from the answer and this old one describes all more detailed.

Another way is more easy. One can just add one more control, for example <span> after the checkbox. One can fill the span and change it based on changes of the chackbox. The demo demonstrates the approach. The most important part of the code is the following:

The grid have the editable column "closed" with edittype: "checkbox". I use form editing to edit the grid. Form editing creates editing controls which ids are the same as the value of name property. So the checkbox for editing the column "closed" in the edit form will have the id="closed". Inside of beforeShowForm callback I insert additional <span> element with id="mydate" directly after the checkbox "closed":

beforeShowForm: function () {
    $("<span id='mydate'></span>").insertAfter("#closed");

    // we trigger change event on the chechbox only to execute once
    // the custom event handler which fills the span #mydate
    $("#closed").trigger("change");
}

The definition of the column "closed" of the grid contains editoptions.dataEvents which defines the handler of "change" event. The event handler fills the span with id="mydate" based on the state of the checkbox:

colModel: [
    ...
    { name: "closed", width: 70, align: "center", formatter: "checkbox",
        editable: true, 
        edittype: "checkbox",
        editoptions: {
            value: "Yes:No",
            defaultValue: "Yes",
            dataEvents: [
                {
                    type: "change",
                    data: { uncheckedDate: "9/11/1964" },
                    fn: function (e) {
                        if ($(this).is(':checked')) {
                            var date = new Date(Date.now()),
                                strDate = (date.getMonth() + 1) + "/" +
                                          date.getDate() + "/" +
                                          date.getFullYear();
                            $("#mydate").text(strDate);
                        } else {
                            $("#mydate").text(e.data.uncheckedDate);
                        }
                    }
                }
            ]
        },
        stype: "select",
        searchoptions: {
            sopt: ["eq", "ne"],
            value: ":Any;true:Yes;false:No"
        }
    },

As the result the form looks like on the picture below

enter image description here

Additional editing callback

afterclickPgButtons: function () {
    $("#closed").trigger("change"); // to set date
}

refreshes the date new the checkbox after the user clicks next/previous record button.

To send additional data with the data from madate <span> I used onclickSubmit callback

onclickSubmit: function () {
    return {
        myDate: $("#mydate").text()
    }
}

which send the date as myDate parameter during submitting of editing form.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • The second option seems the easiest. But I want the date to also be displayed in the column on submit. If I use this second route it will only display a checkbox in the cell. Is their way to display in the cell, only the date when I check it in the edit form and if unchecked it leaves the cell empty? – NewHistoricForm Sep 08 '14 at 20:05
  • @NewHistoricForm: Sorry, but I don't understand your questions. You wrote "I want the date to also be displayed in the column on submit". In which column you want to display the date? Later you wrote "If I use this second route it will only display a checkbox in the cell". Which cell you mean? Do you speak about editing form or about the main grid? Sorry, but I can't follow you. – Oleg Sep 08 '14 at 20:17
  • I mean the main grid. Your "Closed" column shows only cells with checkmarks. I need the cells to show the current date ex."9/8/2014" when checking the checkbox or empty if unchecked(The checkbox is displayed in the editform only).The way you have the edit form is perfect. – NewHistoricForm Sep 08 '14 at 20:29
  • @NewHistoricForm: If you have the column in the main grid with the date which you displays new to the checkbox then where you see a problem? You use default `reloadAfterSubmit:true` and the column value will be filled based on the data returned from the server. – Oleg Sep 08 '14 at 20:47
  • True. Everything is starting to come together. I ran into a problem. I want the unchecked state to dispay empty so I left like this `{ uncheckedDate: "" }` But when one goes back to edit previously checked rowcell and unchecks it, when submitted it displays unwanted incorrect date on the assigned date column. How to adjust to display empty on editform when unchecked but sends null to cell if submitted?That way I can adjust the cell to display empty if null with `if (cellvalue === undefined || cellvalue === null || cellvalue === 'NULL') { return ''; ` – NewHistoricForm Sep 08 '14 at 23:01
  • @NewHistoricForm: If you need just **send** `myDate: null` in case of empty `$("#mydate").text()` you can modify `onclickSubmit` from my answer. `onclickSubmit` can be used to either extend posted data with new parameter or to overwrite someone. To display empty input data of Date as ` `, ` `, `"n/a"` or other you can use custom formatter which calls original date formatter. See [the answer](http://stackoverflow.com/a/25684748/315935) for the code example. – Oleg Sep 09 '14 at 07:53
  • Also I checked today and when I open the editform for a previously checked cell it shows todays date not the actual date that it was checked. Ex. Checked date was 9/8/2014 but in edit form it shows today's date, 9/9/2014 – NewHistoricForm Sep 09 '14 at 15:43
  • @NewHistoricForm: Sorry, but I can't do all your job. I used the names `myDate` because you wrote multiple times that you have the column in the grid, but didn't wrote **the name of the column** (`lastvisit`, `cdate`, `ddate` or other). Because of the same reason I didn't know whether the column is editable or not. The code depends from it. In general you need probably just replace the name `myDate` inside of `onclickSubmit` to the column name of the date. – Oleg Sep 09 '14 at 16:26
  • I understand. I was a bit confused.The problem is `beforeShowForm: function () {$("").insertAfter("#closed");` This shows the current date when checked but I want to show the date saved in the column I named "mydate2" when checkbox wa previously checked. How to do that? I thank you for your help and patience, @Oleg – NewHistoricForm Sep 09 '14 at 17:21
  • To be clear I need that if it detects that checkbox is checked then should display saved date from previously submitted checkmark change(saved in column). Thank you – NewHistoricForm Sep 09 '14 at 18:22
  • @NewHistoricForm: Do you filled the `mydate2` on the server side? Do you get the modified `mydate2` value on your code of `"jqGridCrud.php"` (`editurl`) and save it? If "Yes" then the current code should already work. By the way, there are no `srcformat:'yyyy-mm-dd',newformat: 'm/d/yy'` properties in `colModel`. You should place it inside of `formatoptions: {...}` – Oleg Sep 09 '14 at 18:23
  • Thanks for all your help. I always learn allot from your answer. – NewHistoricForm Sep 09 '14 at 19:26