Home > Back-end >  A simple method of generate infinite layer Treeview
A simple method of generate infinite layer Treeview

Time:09-22

Recently to write a program that will data in the database to generate the TreeView, data structure, similar to the following:

 
1 all goods
10001 home appliances
100010001 TV
100010002 refrigerator

100010003 washing machine10002 kitchen appliances
100020001 the dishwasher
Microwave oven 100020002
10003 electronics
100030001 handheld device class
1000300010001 smartphones
1000300010002 handheld game


Data layer there is no limit, can be subdivided continues,

Think and search the solution online in general is to use recursive, traverse the TreeView tree, more troublesome,

Then thought of a simple solution:

 
The Tree - & gt; The Items - & gt; The Item [0] - & gt; Text=L "1";//the top categories: all goods

Int nrow=0, ncol=0;//query statement returns the number of rows in a result set, the number of columns
Char * * Result;//points to the returned data set

For (int k=0; K & lt; The Tree - & gt; The Items - & gt; The Count. K++) {//traversal Tree, a Tree, the Tree - & gt; The Items - & gt; The Count is invisible increment, the function of dynamic increase
UTF8String u=Tree - & gt; The Items - & gt; The Item [k] - & gt; The Text + L "____";
Q="select typecode from alltype where typecode like '" + u +"' order by typecode ";
Int r=sqlite3_get_table (db, q.c _str (), & amp; The Result, & amp; Nrow, & amp; Ncol, 0);

For (int j=1; J & lt;=nrow; J++) {//traversal search data list
UnicodeString w=UTF8String (Result) [j].
The Tree - & gt; The Items - & gt; AddChild (Tree - & gt; The Items - & gt; The Item [k], w);
}

Sqlite3_free_table (Result);
}


A simple piece of code implementation,

Code is the key to traverse the TreeView, the increase of the number of nodes is dynamic, have been chasing the traversal, direct node no longer increases (database content has been read), so that each node only traverse again, there is no repeated traversal and determine whether to deal with problems, efficiency is relatively high,

CodePudding user response:

Besides, collection first,

CodePudding user response:

Haven't scan, collection first,
  • Related