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?

Answers

  • colincolin Posts: 1,193Questions: 0Answers: 226

    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: 2,784Questions: 19Answers: 618

    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: 1,193Questions: 0Answers: 226
    edited June 14

    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

Sign In or Register to comment.