EasyUI Forum
May 14, 2024, 09:59:05 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to manage datebox with Date as long integer?  (Read 9080 times)
bbones
Newbie
*
Posts: 11


View Profile Email
« on: April 06, 2015, 07:00:32 AM »

Hi, again.
Help me pls Smiley
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
Logged
yamilbracho
Jr. Member
**
Posts: 64


View Profile Email
« Reply #1 on: April 06, 2015, 06:44:47 PM »

I think you could use getTime() method of javascript Date
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #2 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>
Logged
bbones
Newbie
*
Posts: 11


View Profile Email
« Reply #3 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!
Logged
bbones
Newbie
*
Posts: 11


View Profile Email
« Reply #4 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
« Last Edit: May 08, 2015, 05:48:49 AM by bbones » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!