0

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 num 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 cant 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

murday1983
  • 3,806
  • 14
  • 54
  • 105
  • 1
    Why the double naming? `function (row, data` but then you do `var row =` and `var data =` – Carsten Løvbo Andersen Mar 14 '19 at 11:52
  • You can add the spaces every 4 characters by following this logic in [this answer](https://stackoverflow.com/a/2901298/519413) and changing `","` to `" "` and `{3}` to `{4}`. Removing the `test-` is a simple `replace()` operation – Rory McCrossan Mar 14 '19 at 11:53
  • @RoryMcCrossan Doesnt work with the `DataTable` or i am doing something wrong. Completely new to `DataTables` though – murday1983 Mar 14 '19 at 12:13
  • @CarstenLøvboAndersen - Your comment is not relevant to the question being asked. I understand what your saying but it makes no heads or tails – murday1983 Mar 14 '19 at 12:14
  • The logic to update the format works, https://jsfiddle.net/bo782vkc/, so you need to debug the Datatables logic. I would suggest using `render` on the cell individually instead: https://datatables.net/reference/option/columns.render – Rory McCrossan Mar 14 '19 at 12:16

1 Answers1

0

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
        }
    }...
murday1983
  • 3,806
  • 14
  • 54
  • 105
  • Doing regex replace twice against each target cell could be the issue from performance standpoint. Please, checkout my updated answer. –  Mar 14 '19 at 15:47