datagridjquery-easyuidatebox

Initialise jqueryeasyui datebox with current date


I am using jquery easyui edatagrid, where one column is of datebox type editor. I want to auto select today date so that data entry is faster.

Code :

<th field="date" width="50" editor="{type:'datebox',options:{formatter:myformatter,parser:myparser,required:true}}">Date</th>
 function myformatter(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            return (d<10?('0'+d):d)+'/'+(m<10?('0'+m):m)+'/'+ y;
        }
        function myparser(s){
            if (!s) return new Date();
            var ss = (s.split('/'));
            var d = parseInt(ss[0],10);
            var m = parseInt(ss[1],10);
            var y = parseInt(ss[2],10);
            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
                return new Date(y,m-1,d);
            } else {
                return new Date();
            }
        }

Solution

  • You can set the date as soon as the grid data is loaded. For example, something like

    $('.datebox input').each( function(){ $(this).val(formatDate(new Date())) });
    

    It is a matter of getting the input fields. For me the above works, you can adjust it according to your code. Then you need a function that does the date formatting, for example

    function formatDate(value) {
       return value.getMonth()+1 + "/" + value.getDate() + "/" + value.getYear();
    }