As far as I understand your requirements, you want to expand all items at level 0. Then on second click to expand their children (items on level 1) and etc.
When you loop the items, you can check how deep are their in the hierarchy based on the first cell. It contains so many span elements as the counter of it's parent. Check this example that demonstrates how to expand all items on given level and then to collapse them:
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI