javascriptjquerydatatabledatatables

Pull Numeric characters and add spacing from DataTable JSON


I have a DataTable which is doing a GET and populates. The data returned only has one option returned called which is a tel number but the tel number returned has 'test-' in front of it (e.g. test-010101010101).

What I want is to remove the 'test-' and then add spaces after every 4 digits (e.g. 0101 0101 0101.

I have tried the below but can't get it working

"createdRow": function (row, data, dataIndex) {
    $('#dialPlanListDataTable').DataTable().rows().eq(0).each(function (index) {
         var row = $('#dialPlanListDataTable').DataTable().row(index);
         var data = row.data();
         console.log(data)
         var sd = data.text();  
         sd = parseInt(sd);
         console.log(sd)
        });
},

This results in

enter image description here


Solution

  • Went with the following:

    var strippedTelNo = '';
    
    "columns": [
        {   "data": null,
            "render": function(data) {
                    strippedTelNo = data.telNumber
                                        .replace(/[A-Z, a-z]|-/g, '')
                                        .replace(/\B(?=(\d{4})+(?!\d))/g, " ");
    
                return strippedTelNo
            }
        }...