Created
April 28, 2011 16:03
-
-
Save rileydutton/946647 to your computer and use it in GitHub Desktop.
Collapsable/Expandable Table View Rows in Titanium Mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var container = Ti.UI.createView({backgroundColor: "white", layout: "vertical"}); | |
var layout = [ | |
{ | |
title: "Parent 1", | |
isparent: true, | |
opened: false, | |
sub: [ | |
{ | |
title: "Child 1" | |
}, | |
{ | |
title: "Child 2" | |
} | |
] | |
}, | |
{ | |
title: "Parent 2", | |
isparent: true, | |
opened: false, | |
sub: [ | |
{ | |
title: "Child 3" | |
}, | |
{ | |
title: "Child 4" | |
} | |
] | |
} | |
]; | |
var tableView = Ti.UI.createTableView({ | |
style:Titanium.UI.iPhone.TableViewStyle.GROUPED, | |
top: 0, | |
height: Ti.Platform.displayCaps.platformHeight, | |
data: layout | |
}); | |
tableView.addEventListener("click", function(e) { | |
//Is this a parent cell? | |
if(e.row.isparent) { | |
//Is it opened? | |
if(e.row.opened) { | |
for(var i=e.row.sub.length; i > 0; i = i - 1) { | |
tableView.deleteRow(e.index + i); | |
} | |
e.row.opened = false; | |
} | |
else { | |
//Add teh children. | |
var currentIndex = e.index; | |
for(var i=0; i < e.row.sub.length; i++) { | |
tableView.insertRowAfter(currentIndex, e.row.sub[i]); | |
currentIndex++; | |
} | |
e.row.opened = true; | |
} | |
} | |
}); | |
container.add(tableView); |
I believe so. In the click handler, just go through all of the rows in tableView (tableView.rows
) and then remove all children (same code that is used if(e.row.opened)
. That would "collapse" (remove all children) for all parent rows whenever any parent row is clicked.
it only show parent....child are not visible...can anybody help me.?
thanks this helps me alot
but
need to update row to rowData
and
update the open value is not real update the property value in the rowData
so it keep add rows all the time
instead you can check that the title of the next row is the title of the first child if true so the subtable is opened
Hi, When I am click on 0 index Row, I am getting error "No row at this index".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi rileydutton,
Is it possible to auto collapse when click another parent?