function row().data()

function row().data()

Karl53Karl53 Posts: 72Questions: 29Answers: 0

If I understand DataTables' design correctly, function row().data() seems to return a new instance of the object.

Is there a way to initialize a var to point to the data for direct manipulation? For my purpose, I don't think I need to maintain a copy of the data, update it and then call row().data( d ) to set the data.

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    function row().data() seems to return a new instance of the object.

    It returns a new API instance, but the data contained in it is the original data given to DataTables.

    Can you illustrate what you are trying to do with an example please?

    Thanks,
    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0
    edited October 2014

    Hi Allan,

    Sure. What I want is a reference to the data that DataTables displays.

    This does not do what I expect, i.e. change the displayed value:

    var tableData = table.rows().data();
    
    tableData[0].idx = 100; // value was 1
    
    table.draw();
    
    // display value does not change
    

    This changes displayed value:

    var tableData = table.rows().data();
    
    tableData[0].idx = 100; // value was 1
    
    table.row(0).data( tableData[ 0 ] ).draw();
    

    From the above, I surmise that tableData points to a copy of the data managed by DataTables and is not simply a reference to it. Otherwise, why is the call to data() in below form, necessary if tableData is a reference?

    function row().data( d )

    Thanks for your assistance in understanding this.

    (Sorry if the above code does not display on separate lines. I used Markdown but in Chrome, it displays in preview on one long line with horizontal scrollbar.)

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The first method does not work since DataTables doesn't know that the data has changed - there is no listener on the data. With the advent of ECMAScript 1.7's Object.observe it will be possible to do that natively in browsers, but as far as I know, only Chrome supports it at the moment.

    The correct way to update data is to use the row().data() or cell().data() methods (unless you were to use a proxy for Object.observe, which is possible, but not something I've tried yet).

    Allan

This discussion has been closed.