Updating object from a publisher-subscriber platform

Updating object from a publisher-subscriber platform

sanchez_ingarteksanchez_ingartek Posts: 4Questions: 2Answers: 0

I'm developing a page in which I show a table whose data is provided by a publisher-subscriber platform (PubNub).

When a new message arrives, I find the associated object and update its new values. The object is already added to the datatable, but updates aren't shown: only the initial values are shown.

This is the realted code

$(document).ready(function(){

    // DATATABLE
    laTablaDatosVehiculos = $('#datos_vehiculos').DataTable({
            paging: false,
            searching: false,
            ordering:  true,
            columnDefs: [
            {"className": "text-center", "targets": "_all"}
            ],
            language: {
                    url: "[[@{/webjars/datatables-plugins/1.10.16/i18n/Spanish.json}]]"
            },
            columns: [
                {
                data: "vehiculo",
                title: "Vehiculo",
                render: function (data, type, row, meta) {
                    return data;
                }
                  },
                 ...
        }
    });

    for(var i=0; i<vehiculos.length; i++){
        var v = vehiculos[i];
                    
        var datoTablaVehiculo = {
            "vehiculo": ""
            "canalDatos": v.canalDatos,
            "canalDatosResultadoAlgoritmo": v.canalResultadosAlgoritmo,
            // more attributes
        };
        datosTablaVehiculos.push(datoTablaVehiculo);
        laTablaDatosVehiculos
            .row
            .add(datoTablaVehiculo)
            .draw();
                    
        ourChannels.push(v.canalDatos);
        ourChannels.push(v.canalResultadosAlgoritmo);   
    }
    
    // PUBNUB
    var pubnub = new PubNub({
        publishKey : 'pub-c-XXXX',
        subscribeKey : 'sub-c-XXX'
    });
    pubnub.addListener({    
        message: function(message) {    
            // find the object in datosTablaVehiculos and modify value
        }
    });

    pubnub.subscribe({
        channels: ourChannels
    });
});             

Am I missing something?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @sanchez_ingartek ,

    I can see where you add the row, with the row.add() method, but I can't see where you're updating the record. Am I missing something?

    Cheers,

    Colin

  • sanchez_ingarteksanchez_ingartek Posts: 4Questions: 2Answers: 0

    @colin I supposed that when I modify the object, automatically the datatable gets updated.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    The question is - how are you modifying the object?

    Is it modified in the client or the server?

    Is it modified using Datatables API's?

    Kevin

  • colincolin Posts: 15,118Questions: 1Answers: 2,583
    edited June 2018 Answer ✓

    Hi @sanchez_ingartek ,

    Ah, yep, I understand. As Kevin said, it depends on how you're modifying the object. If you're modifying the same object that was used when you added the row, then if you change it, you'll need to invalidate the row for DataTables to pick-up on that change with row().invalidate().

    Take a look at the example here - I'm changing a field in the second object after it's been added.

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.