direktor05 2 Posted December 2, 2022 Hello, Is there a TTreeview component that can export items to JSON? Probably not or is very simple, because I also need to export data associated with a node which is in Pointer to Json. Any suggestion welcome. Share this post Link to post
direktor05 2 Posted December 2, 2022 Or give me some code snippet to parse TTreeView, then will make json manually Share this post Link to post
haentschman 92 Posted December 2, 2022 (edited) Hi... It is not a good idea to keep data storage in a visual control. The JSON should be generated from the data source (class, csv etc.). Edited December 2, 2022 by haentschman Share this post Link to post
direktor05 2 Posted December 2, 2022 (edited) No I want to make a tool to create HTML menus. So I want a treeview that will be just like a website menu then export to json and read with javascript. The menu must not only have name, but also link url, onclick event description and parent/child id, stored in tTreeview1.items.data pointer. Edited December 2, 2022 by direktor05 Share this post Link to post
aehimself 396 Posted December 2, 2022 8 minutes ago, direktor05 said: No I want to make a tool to create HTML menus. So I want a treeview that will be just like a website menu then export to json and read with javascript. The menu must not only have name, but also link url, onclick event description and parent/child id, stored in tTreeview1.items.data pointer. TTreeView is basically a wrapper for the old Windows TreeView component, which was created way before JSON. You MIGHT have some success finding JSON export in TVirtualTreeView but I wouldn't be so sure; most probably you'll have to extract that by yourself. As @haentschman mentioned storing data in a visual component is a bad practice. What I'd do is... Have a TJSONObject which stores your node information. By adding childnodes, you will already have the parent / children relationship and since each JSON object has a name, they only now have to store URL, OnClick javascript handler and description. For quick access to this information when building your TreeList based on the TJSONObject, you can assign the related object to the TTreeNode's .Data pointer. This way you didn't just separate UI / data, but simply can call TJSonObject(TreeView.Selected.Data).ToString to immediately get the selected node's JSON representation. To save the whole tree, call your main TJSONObject.ToString, to rebuild it TJSONObject(TJSONObject.ParseJSONValue). This is by using the Delphi provided System.JSON unit, but you can use any other library you prefer. The idea behind can stay the same. Share this post Link to post
direktor05 2 Posted December 2, 2022 (edited) DAMN, I just discovered there is also TJsonTreeView component. Delphi 10.4. A nono, its a component I installed. Made by some Polish guy at Embarcadero. Edited December 2, 2022 by direktor05 Share this post Link to post
Attila Kovacs 629 Posted December 2, 2022 1 hour ago, haentschman said: It is not a good idea to keep data storage in a visual control. 37 minutes ago, aehimself said: As @haentschman mentioned storing data in a visual component is a bad practice You both never worked with treeviews, do you? Share this post Link to post
aehimself 396 Posted December 2, 2022 6 minutes ago, direktor05 said: DAMN, I just discovered there is also TJsonTreeView component. Delphi 10.4. A nono, its a component I installed. Made by some Polish guy at Embarcadero. If you mean https://github.com/pglowack/DelphiJSONComponents/blob/master/JSONTreeView.pas this only visualizes the JSON as far as I can see from the code. You still have to manually append your extra data to each node, or search in each node's children to find your OnClik, URL and Description nodes. 3 minutes ago, Attila Kovacs said: You both never worked with treeviews, do you? I did, and first I used .Data to actually store data too, it was just too convenient. Lately I'm keeping my data in a separate store and .Data only points to the data in this store, if needed. This way I don't even need to free up memory when a node is deleted. Share this post Link to post
Attila Kovacs 629 Posted December 2, 2022 7 minutes ago, aehimself said: Lately I'm keeping my data in a separate store and .Data only points to the data in this store, if needed. This way I don't even need to free up memory when a node is deleted. As the component itself stores only a reference in both cases, I can't see any benefit. Share this post Link to post
aehimself 396 Posted December 2, 2022 1 minute ago, Attila Kovacs said: As the component itself stores only a reference in both cases, I can't see any benefit. The benefit is, that you are not actually storing and handling data in a visual component, e.g.: TTreeNode.Data := TMyClass.Create; While it does work it's considered bad practice as you are not separating UI and data / business logic. Anyway, let's not hijack the topic, shall we. OP might not be interested in this discussion. Share this post Link to post
Fr0sT.Brutal 900 Posted December 2, 2022 11 minutes ago, Attila Kovacs said: As the component itself stores only a reference in both cases, I can't see any benefit. The benefit appears when you want to auto-test the container itself or make another GUI (console, web, etc)... anyway that's just a good practice to divide flies from meatballs (local saying) 1 Share this post Link to post
Attila Kovacs 629 Posted December 2, 2022 2 minutes ago, Fr0sT.Brutal said: The benefit appears when you want to auto-test the container itself or make another GUI (console, web, etc)... anyway that's just a good practice to divide flies from meatballs (local saying) I know, but in this particular case I can't think of any situation where I would test any function for supporting a tree when there is no tree. Or building a console application and having stuff for a tree, which I do not need. In my view, in this case, you are mixing the GUI and the business logic. Share this post Link to post
SwiftExpat 65 Posted December 2, 2022 Someone had a similar question in the TMS forum recently and I thought the work around was a quick solution. Add columns (the work around was to hide the columns) to the tree view for each piece of data that is in your pointer. When you assign the pointer, copy the strings to the text of the columns. The TMSFNCTreeView in FNC UI Pack is JSON based, you can see if it fits your needs. Share this post Link to post