Suppress 1970.01.01 in editor datepicker

Suppress 1970.01.01 in editor datepicker

dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1
edited April 2019 in Editor

After i solved the format problem with unix timeseconds in a oracle db in the datable editor (https://datatables.net/forums/discussion/comment/149816), i have the next problem :-(

The design of database writes always a 0 in the datafield, when were is no input. Don't ask me - i can not change it. This is the oracle database from my customer and not mine :-/

So, here's my problem:
If one of the date fields is not entered when the record is created, then a 0 is written into the database. After a couple of days, the customer knows the date and wishes to update the field. Now the editor shows '1970.01.01'. This is absolutly correct because the editor found a 0 in the database and this represents '1970.01.01'.
For the customer it's very toilsome to click in the datepicker from 1970 until 2019. Is there a way that i can change the shown date-value in the editor / date-picker when it's '1970.01.01' ?

Thanks a lot for any help

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Assuming the Oracle field accepts a date of NULL, use

    ->setFormatter( 'Format::nullEmpty' )
    

    on the Editor field.

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    @tangerine
    Thanks for your help, but your solution didn't work. As i explained: Into the Oracle database a 0 (Zero) is written and not null. So the setFormatter is not working because the field is not empty, it has a value and the value is 0.

    I think i will need some different on the javaScript side where i define the editor fields. But i found nothing to write into the inputfield. The only thing i found was "def". But "def" only works if nothing is found in the value of the field, i.e. null. But datatable will find a 0 and this is a value, so "def" won't work.

    Is there a something like "value" in HTML?
    Then i would do something like:

    If the value is 0, then show current date otherwise show value.

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    ….and what i forget to write:

    I'm still using the setFormatter in the server script to convert the unixseconds to a date Format with

    ->setFormatter( function ( $val, $data )
    { return strtotime($val); } )

    So i'm not shure if i can use 2 setFormatters….

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Into the Oracle database a 0 (Zero) is written and not null. So the setFormatter is not working because the field is not empty, it has a value and the value is 0.

    Yes, I understand that; but I stand by my answer. There is more detail in my posts in this thread:
    https://datatables.net/forums/discussion/comment/146871#Comment_146871

    I'm not sure which of "ifEmpty(null)" or "nullEmpty" is preferred - possibly one of them is a legacy method.

    A zero in PHP is considered empty.
    Why is there a zero in the field sent to the server in the first place?

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    @tangerine
    First of all: Thanks for your time and help.

    As i descriped before (perhaps not so clear), there are triggers in the Oracle database which fill every field with a 0 (zero) if the field is null (empty). I can't change this behavior, because it's the database of my client and i have no rights of changing the database. Therefore the setformatter makes no sense.

    I'm sorry but i can't confirm your statement that "A zero in PHP is considered empty." My client works with Php 5.6.25 and there a "0" is definitely not null. But that should not interest us now.

    In the database is a "0" (zero) and if it's so, i want that the editor shows the current date and not "1970.01.01".

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Nobody with an idea?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    there are triggers in the Oracle database which fill every field with a 0 (zero) if the field is null (empty).

    null is not the same as empty. Explicitly submitting null via Editor's formatter will write null to the db, assuming Oracle allows null date fields.

    I'm sorry but i can't confirm your statement that "A zero in PHP is considered empty." My client works with Php 5.6.25 and there a "0" is definitely not null.

    Look at the documentation for PHP's empty() function. A zero is considered empty.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    As i descriped before (perhaps not so clear), there are triggers in the Oracle database which fill every field with a 0 (zero)

    A 0 as a Unix timestamp is by its very definition 1970/01/01 00:00:00. The timestamp counts the number of seconds since that point. See here.

    So if you are putting in 0 as a default value, then yes, it would be 1st Jan 1970.

    If you didn't want that to show, then you could use a get formatter to detect that value and return an empty string. I would recommend against that though - what if someone actually wanted to put in 1970-01-01. Its a perfectly valid value.

    As @tangerine says, use null if there no value assigned to the field in the db. That's what it is there for.

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    I can't modify the trigger or the design of this database. I know that 0 is the 1st Jan 1970. I know that this is absolutly correct.

    Here i have to "manipulate" the editor. If the value in the Input line of the editor is 0 (equal to 1st Jan 1970), then i have to change the date in the datepicker of the editor to the current date. But i don't know how.

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    In pure php i would write

    if ($val == 0 or $val == null)
    { $val = time(); }
    

    How can i do something like this in the editor input datepicker field?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @dg_datatables ,

    This here is a way you could do it on the client end. It's doing two things: 1, showing an empty date instead of 0 on the view display, and 2. converts it to the current date on the edit.

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1
    edited April 2019

    @colin
    This is exactly what i want. Thanks a lot.

    Helpfull the part in the show-section:

          { data: 'start_date', render: function(data) {
            return data === '0'? '' : data;
          } },
    

    and this part:

      editor.on('open', function ( e, mode, action ) {
        if (editor.field('start_date').get() === '0') {
          editor.field('start_date').set(new Date())
        }
      });
    
This discussion has been closed.