Jump to content
direktor05

TTreeview to JSON VCL?

Recommended Posts

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

Hi...:classic_cool:

 

It is not a good idea to keep data storage in a visual control. :classic_blink:
The JSON should be generated from the data source (class, csv etc.).

Edited by haentschman

Share this post


Link to post

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 by direktor05

Share this post


Link to post
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

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 by direktor05

Share this post


Link to post
1 hour ago, haentschman said:

It is not a good idea to keep data storage in a visual control. :classic_blink:

 

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
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
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
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
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)

  • Like 1

Share this post


Link to post
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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×