<table id="datatable" class="table table-striped table-bordered"></table>

{{ table_data | json_script:"table-data" }}

<script>
  $(document).ready(function () {
    console.log("Load RMS values from JSON..");
    let table_data = JSON.parse(document.getElementById('table-data').textContent);

    /** replace null in value with NaN
     * This is needed because we cannot pass NaN through JSON without
     * extra libraries, so it is passed as null (workaround) */
    table_data = table_data.map( x => {
         if (x['value'] === null) {
           console.log("replaced null");
           x['value'] = NaN;
         }
         return x
    });

    $('#datatable').DataTable({
      data: table_data,
      columnDefs: [
       // Indicate that first column contains HTML
       // to have HTML tags removed for sorting/filtering
       { targets: 0, type: 'html'}
      ],
      columns: [
        {
          title: 'Measurement',
          render: function (data, type, row) {
            let name = row.topography_name;
            return `<a target="_blank" title="${name}" href="${row.topography_url}">${name}</a>`;
          }
        },
        {data: 'quantity', title: 'Quantity'},
        {data: 'from', title: 'From'},
        {data: 'symbol', title: '<span title="Symbol according to ASME B46.1">Sym. 🛈</span>'},
        {data: 'direction', title: 'Direct.'},
        {data: 'value', title: 'Value', render: function (x) { return format_exponential(x, 5);}},
        {data: 'unit', title: 'Unit'},
      ],
      responsive: true,
      scrollX: true
    })
  })
</script>
