Reloading Datatables after signalR response

Reloading Datatables after signalR response

dezeazdezeaz Posts: 7Questions: 5Answers: 0

Hi, I have a datatable and i highlight rows by colouring them using the "createdRow" option based on a condition.

         var tasks = JSON.parse(localStorage.getItem('syncedTasks'));

          var table = $('#table_buttons').DataTable({
              "ajax": {
                  "url": "/api/v1/button/" + module,
                  "dataSrc": "",
                  'beforeSend': function (request) {
                      request.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem('accessGSMToken'));
                  },
                  "statusCode": {
                      401: function (xhr, error, thrown) {
                          window.location.href = "Index.html"
                          return false
                      }
                  }
              },
              "language": {
                  "emptyTable": "No buttons found, please add a button.",
                  "search": "_INPUT_",
                  "searchPlaceholder": " Search..."
              },
              "lengthChange": true,
              "pageLength": 25,
              "lengthMenu": [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"]],
              "order": [[1, "asc"]],
              "createdRow": function (row, data, dataIndex) {
                  console.log(data);
                  if ((tasks[site].module[module].Buttons).includes(data.uniqueId)) {
                      $(row).css('background-color', "#a3d3c2");
                  }                   
              },
              "rowId": 'id',
              "dom": "<'row'<'col-sm-12 col-md-6 AddBtn'><'col-sm-12 col-md-2'><'col-sm-12 col-md-2'l><'col-sm-12 col-md-2'f>>" +
                  "<'row'<'col-sm-12'tr>>" +
                  "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
              "paging": true,
              "order": [[0, "asc"]],
              "columns": [
                  { data: 'uniqueId' },
                  { data: 'apt' },
                  { data: 'tel' },
                  { data: 'div1' },
                  { data: 'div2' },
                  { data: 'div3' },
                  { data: 'name' },
                  { data: 'code' },
                  { data: 'dto' },
                  { data: 'timeBand' },

             ]                
          });

I'm using a SignalR rouytine to update the localstorage and to reload the table:

localStorage.setItem('syncedTasks', JSON.stringify(obj));

$('#table_buttons').DataTable().ajax.reload();

Is there any way i can refresh the variable tasks with the updated localstorage info so it is available in the "createdRow" option.

Thanks

Aaron

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953

    Maybe something like this will work:

    localStorage.setItem('syncedTasks', JSON.stringify(obj));
    tasks = JSON.parse(localStorage.getItem('syncedTasks'));
    
    $('#table_buttons').DataTable().ajax.reload();
    

    Kevin

Sign In or Register to comment.