DataTable - Default Date

Hello

DataTable Date column seems to have a default date of Today. I have a field i.e. "Start" that is set by the user, if this is set i would like this date to be the default date in the DataTable is it possible?

Hello @Chris_Grist,

Please paste the following code in JavaScript Editor to set the Date in DataTable with the value from 'Start' field.

Please replace 'DataTable0' and 'Start" with the internal names of control and field. Column0 - is the column title.

fd.control('DataTable0').$on('change',
    function(e) {
        var startDate = fd.field('Start').value;
        fd.control('DataTable0').value[0].Column0 = startDate;
    });

You also may have a look at the DataTable documentation for more information.

Hey,

Thank you i have this working, but it does not seem to override the default date, just sets the first rows date, is there a simple way to set subsequent rows as i cannot guess how many rows they would have and do not want to overwrite rows after the user has made selections.

@Chris_Grist,

The above code is for the DataTable which is set up to add new lines on top.

image

Please see below the code example for when the lines added in the bottom.

    fd.control('DataTable0').$on('change',
	
    function(e) {
    	var rec = parseInt(fd.control('DataTable0').value.length) - 1;
        var startDate = fd.field('Start').value;
        fd.control('DataTable0').value[rec].Column0 = startDate;
    });

Hi! I've the same problem, but the suggest not work well.
On change event not allow to enter a new date in datatable.

how put a default date correctly ?

Thanks

@ycervoni,

You can check if the column is blank or not, and populate field with the date only if it is blank:

    fd.control('DataTable1').$on('change',
	
    function(e) {
    	var rec = parseInt(e.value.length) - 1;
        var startDate = fd.field('Date').value;
        if(!e.value[rec].Column1){
            fd.control('DataTable1').value[rec].Column1 = startDate;
        }
    });

Thanks, but in the first row fills the date of today and the other row fills blank rows

fd.control('DataTable1').$on('change', function() {
        fd.control('DataTable1').widget.bind("dataBound",function(){
            var data = fd.control('DataTable1').widget._data;
            var startDate = '';
            if(data){
                console.log(data[0]);
                for (var i = 0; i < data.length; i++){
                    console.log(i);
                    console.log(data[i]);
                    data[i].Dal = startDate;
                    data[i].Al = startDate;
                    console.log(data[i].Dal);   
                    console.log(data[i].Al);   
                }
            }
        });   
    });

@ycervoni,

The startDate variable is blank, and it clears all records.

Why don't you use the code I've suggested in the previous post? Do you need to changes the date in all rows at once?

the previous code don't work. I've an error: value undefined

@ycervoni,

Please try out this code:

    var dt = fd.control('DataTable1');
    var isNew = false;

    dt.widget.wrapper.find('.k-grid-add').on('click', e => {
        isNew = true;
    });

    dt.widget.bind('edit', function(e) {
        if (isNew) {
            isNew = false;
            //set values for Column1 and Column2
            e.model.set('Column1', fd.field('Date').value);
        }
    });

Make sure you are using valid internal control and field names.

Great! it works! Thanks!

1 Like

Hi,

I am trying to adjust the code so that the default value of a number field in datatable is 1 instead of 0. Is it possible to do that?

Thanks,

Regards
Aseem

Hello @aseem,

Please use this code to populate column value for a new row:

var dt = fd.control('Control1');

dt.widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('ColumnName', 1);
    }
});

Thank you @mnikitina it sets the default value to 1 but it also makes the row readonly. I have to click on column header/title and then other datatable fields can be edited. Any suggestions?

@aseem,

You can use this code if you want to keep the column in edit mode:

var dt = fd.control('Control1');

dt.widget.bind('edit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Column1', 1);
    }
});

This worked! thank you.