简体   繁体   中英

jqGrid Disable Date Picker Value for Inline Edit

I'm using jqGrid 4.4.4. I have a date picker for a column and it returns today's date. I only need it to default to today's date for the add modal pop-up. However, when someone is editing a record inline, I need the date not to change to today's date when the user clicks into a record to edit inline.

This is causing data integrity issues, because when a user edits an existing record, the date changes to today's date.

How can I keep it the way it is for adding a new record through the modal, while preventing it from defaulting when inline editing?

Here is the code with the datepicker in the edit options:

 { key: false, name: 'CHK_DT_RCVD', width: '130px', index: 'CHK_DT_RCVD',
            editable: true, formatter: 'date',
            formatoptions: { newformat: 'm-d-Y' },
            formoptions: {},
            editrules: { custom: true, custom_func: validDateCheck },

            editoptions:
            {

                dataInit: function (element)
                {
                    $(element).datepicker({

                        id: 'entryDate_Datepicker',
                        dateFormat: 'mm-dd-yy',
                        maxDate: new Date(2020, 0, 1),
                        showOn: 'focus'
                    }).val(moment(new Date()).format('MM/DD/YYYY'));
                }
             }
        },

First of all you should initialize datepicker inside of setTimeout . The problem: the dataInit could be (will be) called before element be placed on the HTML page ( element is disconnected DOM element).

Inside of setTimeout you can travel over the parents of element and to try to find out whether the element is inside of tr.jqgrow or not. If $(element).closest("tr.jqgrow").length > 0 then the datepicker is inside of inline editing (or cell editing). You should not use .val(moment(new Date()).format('MM/DD/YYYY')); in the case.

Additionally you can consider to use defaultValue property of editoptions . It will be used in Add dialog of form editing. Probably you will be not need to use any .val(...) after the specifing of defaultValue .

By the way, I'd recommend you to remove unneeded key: false and index: 'CHK_DT_RCVD' properties and to fix width: '130px' to width: 130 because the value of width have to be integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM