Leading zeros truncated for varchar column
Leading zeros truncated for varchar column
peterbrowne
Posts: 314Questions: 54Answers: 0
Leading zeros are being truncated for a varchar column user.user_id
. How can I retain the leading zeros? E.g. 00123456 is selected, inserted and updated as 123456. I need to keep the leading zeros as they are staff IDs...
columns: [ {
data: "user.user_id"
},...
and
ar editor = new $.fn.dataTable.Editor( {
ajax: "user_data.php",
table: "#user_table",
template: '#user_form',
fields: [ {
label: "User ID:",
name: "user.user_id"
},
and
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
Editor::inst( $db, 'user', 'user_pk' )
->fields(
Field::inst( 'user.user_id' ),
Field::inst( 'user.first_name' )
->get( $_SESSION['cm_user']['permission'] ),
Field::inst( 'user.last_name' ),
Field::inst( 'user.email' ),
Field::inst( 'user.user_type' ),
Field::inst( 'user.modified' ),
Field::inst( 'user.modified_by' )->setValue( $user )
)
->join(
Mjoin::inst( 'program' )
->link( 'user.user_pk', 'user_program_lookup.user_fk' )
->link( 'program.program_pk', 'user_program_lookup.program_fk' )
->order( 'program.program_name asc' )
->fields(
Field::inst( 'program_pk' )
->options( Options::inst()
->table( 'program' )
->value( 'program_pk' )
->label( 'program_name' )
),
Field::inst( 'program_name' )
)
)
->process( $_POST )->json();
This question has an accepted answers - jump to answer
Answers
This is the table:
It's probably because Editor thinks they're numeric so is being 'helpful'. You can force the field to be a string with
Colin
Thanks Colin. That might be OK for the Editor, but the retrieval from the DB is still truncating the zeros.
So that when the editor updates or inserts, the field is still missing the zeros, unless the user adds the zeros manually each time. I think the type needs to be added in other places, e.g. the data script above?
In that case it's not really a DT issue. You need to fix it at the db level. Google has a lot to say about varchar leading zeros.
I don't believe the issue is with the DB column. I can manually add the zeros directly via PhpMyAdmin and they are retained.
Do you mean the display in the Datatables table truncates the leading zeros?
Have you used the browser's network inspector to validate what is returned when Datatables fetches the table data and the request and response when editing a row?
Basically we need to understand where the leading zeros are truncated. Can you post a link to your page or a test case showing the issue?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
This is what is shown in datatables. The leading zeros are missing.
In the DB table:
OK, so what is in the JSON response? Use the browser's network inspector to look at the response of Datatables XHR request.
If the leading zeros or in the JSON response then try setting
columns.type
tostring
. If the leading zeros aren't in the response then the problem is with the server script and will need to be debugged there.Kevin
The JSON contains no leading zeros. You can see my ID in the JSON is 82563. In the database it is 00082563, which is correct.
The server script is included above in my original question.
There is the same issue in this thread.
Just updated from Editor-PHP-1.9.5 to Editor-PHP-1.9.6. Leading zeros now retained fine. Fixed.
Thanks for the update - good to hear that the update fixed it!
Allan