update and redraw only a single raw (not whole table)

update and redraw only a single raw (not whole table)

normadizenormadize Posts: 31Questions: 6Answers: 1
edited December 2015 in Free community support

Is it possible? Specifically, I'd like to change the data for a single row, then update table caches but redraw only that row in the table, **not* the whole table.

Looking at the API for the .row().data() it says I need to call .draw() after using .row().data() as a setter. I'd like a way to update caches and redraw only that row ...

Here's what I'm trying to do, maybe there is another solution:

I coded my own Click to edit feature on some columns, where clicking replaces the cell content with a <input> which has focus. If I use .drawI() then the focus is taken away from the <input>. I can focus it back after .draw(), but if the user is typing then there may be a brief moment when some keys are not captured ...

EDIT: I realize that changing data in a row and not updating caches can cause issues if, for example, the table is sorted by a column which was changed by my method ... I actually don't want the table to resort itself when changing a cell in a row. Is calling .row(0).data({...}) sufficient in that case?

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited December 2015

    I actually don't want the table to resort itself when changing a cell in a row

    So just pass false to the draw()

  • normadizenormadize Posts: 31Questions: 6Answers: 1
    edited December 2015

    I was just about to write a post about that as I may have hit a possible bug in DT where .row().invalidate() complains about Requested unknown parameter attempted.1 ... the thing is that I'm not changing the data for that cell (which does contain full data), and I don't get any errors when loading the table with the same row data and drawing it. Only .row().invalidate() and .row().data({...}) yields the error.

    I've been trying to replicate it on a simpler example on the fiddle but still couldn't. My data is loaded from an array of objects as

    data = [
      {foo:'bar', attempted:['',''], ... }
      {...},
      ...
    ]
    

    and column definitions are

    columns = [ 
      {data:'attempted.0', title:'A1'},
      {data:'attempted.1', title:'A2'}
    ]
    

    Table draws and displays without error but if I do data[0].foo='chan' (so I'm not changing the attempted property which remains ['','']) and then do .row(0).invalidate() yields the error ... same goes for .row(0).data({foo:'bar', attempted:['',''], ... })

    Still trying to replicate it on a simpler example (my code is huge and also sensitive so I can't share).

    Any clues at this point without a fiddle?

  • normadizenormadize Posts: 31Questions: 6Answers: 1
    edited December 2015

    Got the bastard! My data was being manipulated in another function just before calling .row().invalidate() and was ending up as .attempted=[''] instead of .attempted=['',''] ... fixed that and the alert is gone.

    However!

    My initial data is

    data = [
      {foo:'bar', attempted:[''], ... }
      {...},
      ...
    ]
    

    and columns are

    columns = [
      {data:'attempted.0', title:'A1'},
      {data:'attempted.1', title:'A2'}
    ]
    

    (note the call to attempted.1 which doesn't exist in the data)

    ... then DataTables does not complain in the initial init and draw, but calling .row().invalidate() does complain. That's one thing.

    Furthermore, and this is more peculiar, I did this fiddle to try and capture it: http://live.datatables.net/yanifumu/3/edit

    Note the [''] in the data and then calling attempted.1 in the columns, which doesn't exist and causes an alert in my case, but here it's working fine!

    I'm a little puzzled. I'd like to undertstand it.

This discussion has been closed.