DataTable - Auto-populate Today's Date

Hello, in the past, if selecting a DataTable column type as "Date" it would auto populate with today's date. It doesn't seem to do that now. How do I get Today's date (MM/DD/YYYY) populating when a new record is added to the table?

The code that I've been using for my table is this:

image

Thanks.

Hi @ParAvion,

You can do this as described here:

fd.rendered(() => {
    fd.control('DataTable1').widget.bind('beforeEdit', function(e) {
        var model = e.model;
        if (model.isNew()) {
            model.set('DateColumn', new Date());
        }
    });
});

@IliaLazarevskii thank you. Can you show me how to incorporate that into my existing code?

fd.spRendered(function() {
fd.control('DataTable2').$on('change',
function(e) {
var username = _spPageContextInfo.userDisplayName;
fd.control('DataTable2').value[0].Column3 = username;
});
});

Hi @ParAvion,

Try this:

fd.spRendered(function() {
    fd.control('DataTable2').$on('change', function(e) {
        var username = _spPageContextInfo.userDisplayName;
        fd.control('DataTable2').value[0].Column3 = username;
    });

    fd.control('DataTable2').widget.bind('beforeEdit', function(e) {
        var model = e.model;
        if (model.isNew()) {
            model.set('DateColumn', new Date());
        }
    });
});
1 Like

@IliaLazarevskii your code got the date column to auto populate again, but now I cannot access the dropdown for the 'Assessment' column. The screen must be refreshed to get it working. Video attached ...thoughts?

Column code:

fd.spRendered(function() {
fd.control('DataTable4').$on('change', function(e) {
var username = _spPageContextInfo.userDisplayName;
fd.control('DataTable4').value[0].Column3 = username;
});

fd.control('DataTable4').widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Column2', new Date());
    }
});

});

@IliaLazarevskii can you please help with my last post?

Hi @ParAvion,

So far it seems like the problem is that changing the control value in the beforeEdit event triggers the change event, which unfocuses the control. Double-clicking should help as a temporary workaround.

I'll let you know as soon as I have a fix.

@IliaLazarevskii yeah double click doesn't work unfortunately...only browser refresh. Please keep me posted...thanks.

Hi @ParAvion,

Could you try this?

fd.spRendered(function() {
    fd.control('DataTable4').$on('edit', function(e) {
        var username = _spPageContextInfo.userDisplayName;
        var model = e.model;
        
        model.set('Column3', username);
    });

    fd.control('DataTable4').widget.bind('beforeEdit', function(e) {
        var model = e.model;
        
        if (model.isNew() && !model.get('Column2')) {
             model.set('Column2', new Date());
        }
    });
});