Leading zero problem

Leading zero problem

Alex_anderAlex_ander Posts: 11Questions: 4Answers: 0

Hello!
I have in database column (type varchar), where I store four digits.
When I display them using Datatables there ore two options:
- if the first digit is not a zero, no problems
- if the first digit is zero, datatables do not display the leading zero.

In case I update this field via datatables and in my update request I include the leading zero, it is correcty updated in database with leading zero, but never displayed in Datatables.

To generate data I use this:

Field::inst( 'publish_data.passport_number' ),
Field::inst( 'publish_data.passport_issue_date' )

To render data I use this:

{ data: null, render: function ( data, type, row ) {
return data.publish_data.passport_series+' '+data.publish_data.passport_number;
}
},

I have already spent two days reading similar cases, but was not able to find a solution, sorry about that.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    Why is it a varchar field if it's always four digits?
    And if you never want to display leading zeros, why not have the database return the required format?

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    It's definitely worth considering tangerine's comments and consider changing the database schema.

    • if the first digit is zero, datatables do not display the leading zero.

    You would need to do that check in columns.render, and remove it there.

    If still no joy, could you update this test case to demonstrate the issue, please.

    Colin

  • Alex_anderAlex_ander Posts: 11Questions: 4Answers: 0

    Thanks a lot for reply!
    tangerine, the problem is that for digits are not digits, but a string (they are part of passport number and it is important to keep the leading zero. For example 0123 is not the same as 123 from passport number point of view).

    colin, the task is not to remove the leading zero, but to show the string "as is", without removing zeroes

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394
    edited November 2020

    Oh, okay.
    I don't know why DataTables would not be representing your string correctly. I suggest you provide a test case so we see your code running.
    https://datatables.net/manual/tech-notes/10

  • Alex_anderAlex_ander Posts: 11Questions: 4Answers: 0

    My case is similar to this example:
    https://editor.datatables.net/examples/simple/fieldDefaults.html
    How is possible to save 0123 and, then, to represen it in column Ext of this example?

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    I just looked at the example, and did a couple of edits, and I don't get why it won't display leading zeroes.
    Also, the "New" button isn't working.
    We need one of the development team in here.

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Yep, it's on Allan's list, so he'll be replying at some point today.

    Colin

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    Answer ✓

    It is being caused by this change. Basically when JSON_NUMERIC_CHECK is used, it will return any numeric strings as an actual JSON number - which can sometimes be desirable (1, 2).

    But not in all cases!

    The immediate solution is to use:

    $data = Editor::inst( $db, 'datatables_demo' )
        ->fields(
                 // ....
        )
        ->process( $_POST )
        ->data(); // note the use of `data()` rather than `json()` here
    
    echo json_encode($data);
    

    I think we should probably remove that option from the default code since it was only added recently. We'll make it optional. That will be done for 1.9.6.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4
    edited November 2020

    hi @tangerine

    for add button, perhaps in sample use of def value for extn ...

                    label: "Extension:",
                    name: "extn",
                    def: "0000"
    
  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    Thank you, Allan!

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Fix committed here. As I say, it will be in 1.9.6.

    The new way to pass json_encode options is a second optional parameter for the Editor->json() method - e.g.:

    ->json(true, JSON_PRETTY_PRINT);
    

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    @allan is the ? best ?

  • Alex_anderAlex_ander Posts: 11Questions: 4Answers: 0

    Allan, thank you so much!! It works now as I need!

This discussion has been closed.