Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

deleteRow fails

Hello,

I got an for loop. With this loop i want to remove rows from my table. But when I start removing it looks like that the indexes are changing of my table. I got a rowCount of 9, when i removed more than 4 rows i goes wrong.

Am i right? And how to solve this problem?

My for loop:

var rows = window.table.data[0].rowCount;
window.table.remove();
for(var i = 0; i < rows; i++)
{
    try {
        window.table.deleteRow(i);
    } catch(e) {
        Ti.API.error(e);
    }
}

Thanks,
Jacob

— asked November 10th 2010 by Jacob van Dam
  • android
  • change
  • deleterow
  • index
0 Comments

2 Answers

  • Found a simple solution myself:

    var table = window.table;
    var rows = table.data[0].rowCount;
    
    while(rows)
    {
        window.table.deleteRow(0); //Always delete the first key
        rows = table.data[0].rowCount;
    }
    
    — answered November 10th 2010 by Jacob van Dam
    permalink
    1 Comment
    • Great solution! Also, if you want to delete rows starting from the bottom up:

      var rows = table.data[0].rowCount;
      while(rows) {
          table.deleteRow(table.data[0].rowCount-1);
          rows = table.data[0].rowCount;
      }
      

      This is useful if you want to delete certain indices in the table, say, that you have stored in an array and don't want to worry about those indices being offset by deleting a row before the given index.

      — commented April 25th 2011 by Joe iEntry
  • If you want to delete all the rows you can just set the data to nothing:

    window.table.setData(null);
    
    — answered November 10th 2010 by Roger Chapman
    permalink
    2 Comments
    • This doesn't works at al.

      — commented November 10th 2010 by Jacob van Dam
    • This works for me on android !

      — commented January 21st 2011 by Gwe Pru
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.