How do I show numbers in a column as phone number format, i.e. (xxx) xxx-xxxx ?

How do I show numbers in a column as phone number format, i.e. (xxx) xxx-xxxx ?

hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

I'm using Datatables in a Laravel 5.5 app and store phone numbers as a 10-digit number in my database, but want to show them in my Datatable as a formatted phone number string. I can use the number rendering for currency, but can't figure out how to format phone numbers.

Here's my code:

@section('javascript') 
    <script>
        @can('about_you_delete')
            @if ( request('show_deleted') != 1 ) window.route_mass_crud_entries_destroy = '{{ route('admin.about_yous.mass_destroy') }}'; @endif
        @endcan
        $(document).ready(function () {
            window.dtDefaultOptions.ajax = '{!! route('admin.about_yous.index') !!}?show_deleted={{ request('show_deleted') }}';
            
            window.dtDefaultOptions.columns = [@can('about_you_delete')
            
                @if ( request('show_deleted') != 1 )
                    {data: 'massDelete', name: 'id', searchable: false, sortable: false},
                @endif
                @endcan{data: 'first_name', name: 'first_name'},
                {data: 'last_name', name: 'last_name'},
                {data: 'address_1', name: 'address_1'},
                {data: 'address_2', name: 'address_2'},
                {data: 'city', name: 'city'},
                {data: 'state', name: 'state'},
                {data: 'zip', name: 'zip'},
                {data: 'phone', name: 'phone'}, 
                {data: 'email', name: 'email'},
                {data: 'pay_per_period', name: 'pay_per_period', render: $.fn.dataTable.render.number( ',', '.', 0, '$' )},
                
                {data: 'actions', name: 'actions', searchable: false, sortable: false}
            ];
            
            processAjaxTables();
        });
    </script>
@endsection

I tried adding the phoneNumber.js resource but can't figure out how to use it as a renderer in my Datatable as I do with currency above. Here's the phoneNumber resources call:

<script src="https://cdn.datatables.net/plug-ins/1.10.19/filtering/type-based/phoneNumber.js"></script>

Any suggestions are most welcome

Answers

  • hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

    Bump....?

  • hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

    Nevermind, I found mutators that I can use in the Laravel model so that the returned value from the database is intercepted, reformatted to suit, and then displayed in the Datatable.

    My code in the model is:

            /**
         * Make sure that phone number is properly formatted when retrieved from the database
         * 
         * @param $value
         * @return string
         */
        public function getPhoneAttribute($value)
        {
            return '('.substr($value, 0, 3).') '.substr($value, 3, 3).'-'.substr($value,6); 
        }
    
This discussion has been closed.