EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: bbones on April 06, 2015, 07:00:32 AM



Title: How to manage datebox with Date as long integer?
Post by: bbones on April 06, 2015, 07:00:32 AM
Hi, again.
Help me pls :)
Server sends me a Date value as long integer. I can format it to show with formatter like
Code:
        
dateFormatter : function(value) {
        var d = new Date(value);
        return d.toLocaleDateString();
}
and edit it with datebox with same formatter and parser like
Code:
dateParser : function(s){
    console.log(s);
        if (!isNaN(s))
  return new Date(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();
        }
},

but since this moment field value is Date (or String) and I can't find how to put it in edited row as Long again before sending it back to server.
What is rigth way to write formatter and parser? Or what is a right strategy at all?

Thanks for any help


Title: Re: How to manage datebox with Date as long integer?
Post by: yamilbracho on April 06, 2015, 06:44:47 PM
I think you could use getTime() method of javascript Date


Title: Re: How to manage datebox with Date as long integer?
Post by: stworthy on April 06, 2015, 07:13:09 PM
A better way to solve this issue is to extend a new editor that can convert between long numeric and date value.
Code:
<script>
$.extend($.fn.datagrid.defaults.editors, {
datebox2: {
init: function(container, options){
var input = $('<input>').appendTo(container);
input.datebox(options);
return input;
},
destroy: function(target){
$(target).datebox('destroy');
},
getValue: function(target){
var opts = $(target).datebox('options');
var v = $(target).datebox('getValue');
return opts.parser.call(target,v).getTime();
},
setValue: function(target, value){
var opts = $(target).datebox('options');
var d = new Date(parseInt(value));
var s = opts.formatter.call(target, d);
$(target).datebox('setValue', s);
},
resize: function(target, width){
$(target).datebox('resize', width);
}
}
});
</script>

Please notice that the 'formatter' and 'editor' should be defined correctly on the datagrid column.
Code:
<th data-options="field:'datefield',width:100,
formatter:function(value,row){
if (value){
return new Date(parseInt(value)).toLocaleDateString();
}
},
editor:{
type:'datebox2',
options:{

}
}"> datefield </th>


Title: Re: How to manage datebox with Date as long integer?
Post by: bbones on April 07, 2015, 06:02:55 AM
It works!

Really flexible thing.

formatter/parser was isolated to JS module - my sintax from there

Tnx, indeed!


Title: Re: How to manage datebox with Date as long integer?
Post by: bbones on May 08, 2015, 05:47:12 AM
One step more...

How to use datebox2 with input field in form?
I need to extend another editor?

BR
BB