setFormatter
setFormatter
I managed to format the date in the date field to dd-mm-yyyy, when the user has entered his date. But it is not saved because I don't understand how to use setFormatter. The examples are not very clear (to me that is :-) )
this is what I have:
editor = new $.fn.dataTable.Editor( {
//ajax: {url: "php/table.deelneming_v2.php"},
ajax: {url: "data/deeln.txt"},
table: "#deelnTable",
idSrc: 'id',
fields: [ {
label: "relatienr:",
name: "client"
}, {
label: "naam:",
name: "naam"
}, {
label: "publicatie :",
name: "publdatum",
type: 'datetime',
/*
def: function () { return new Date(); },
format: 'DD-MM-YYYY',
fieldInfo: 'Verbose date format',
*/
opts: {
format: 'DD-MM-YYYY',
showWeekNumber: true
}
}, {
label: "dividend 2016:",
name: "div_2016"
}, {
label: "opgeofferd bedrag:",
name: "offer"
}, {
label: "verkrijgingsprijs:",
name: "verkrijgingprijs"
}, {
label: "goodwill:",
name: "goodwill"
}
],
i18n: {
create: {
button: "nieuw",
title: "deelneming toevoegen",
submit: "bewaar"
},
edit: {
button: "wijzigen",
title: "deelneming wijzigen",
submit: "bijwerken"
},
remove: {
button: "verwijder",
title: "verwijder deelneming",
submit: "bevestig",
confirm: {
_: "Weet u zeker om %d te verwijderen?",
1: "Deze regel 1 verwijderen?"
}
},
datetime: {
previous: 'vorige',
next: 'volgende',
months: [ 'Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December' ],
weekdays: [ 'zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat' ]
}
}
} ); //editor
and serverside:
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'deelnemingen' )
->fields(
Field::inst( 'client' )->validator( 'Validate::notEmpty' ),
Field::inst( 'naam' )->validator( 'Validate::notEmpty' ),
Field::inst( 'locatie' ),
Field::inst( 'offer' )
->validator( 'Validate::numeric' )
->setFormatter( 'Format::ifEmpty', null ),
Field::inst( 'verkrijgingprijs' )
->validator( 'Validate::numeric' )
->setFormatter( 'Format::ifEmpty', null ),
Field::inst( 'goodwill' )
->validator( 'Validate::numeric' )
->setFormatter( 'Format::ifEmpty', null ),
Field::inst( 'div_2016' )
->validator( 'Validate::numeric' ),
Field::inst( 'publdatum' )
//->setFormatter( 'Format::ifEmpty', null )
->validator( 'Validate::dateFormat', array(
"format" => Format::DATE_ISO_8601,
"message" => "Please enter a date in the format dd-mm-yyyy"
) )
->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
)
->process( $_POST )
->json();
Demo is available on http:\ficos.nl\test\index.html
The setFormatter function is not working for me: I guess DATE_ISO_8601 accepts yyyy-mm-dd only.
What am I doing wrongly?
This question has an accepted answers - jump to answer
Answers
For a set formatter, what you need to do is tell it the format that you want to convert from.
date_format_to_sql
will always convert from a format, to ISO8601.date_sql_to_format
will always convert from ISO8601 to a format.So you might use:
You should update the get formatter and validator as well.
Allan
Great, thanks Alan.
I had to remove the rendering in the table as well, now it is working as expected.
Left is the datepicker (Editor's). I need to change the format there as well to 'd-m-Y'. Currently it returns 'Y-m-d'.
I tried:
but it doesn't work. Probably because that is meant for JQuery-ui datepicker?
You could use the
datetime
field type which is built into Editor, rather than thedate
field type.You will need to load MomentJS as well if you do want to use
datetime
with formatting.Allan
I had Moment.js already running...
Just needed:
and it works! Thanks
Anyone have an example of doing this in the .NET library instead of PHP. I can't figure out how to set the getformatter and setformatter in the proper area. Thanks!
Examples and documentation for how to use get and set formatters in the .NET libraries for Editor are available here.
Allan