How to create Parent-Child collections

Hi,

I’m very new to Cockpit and currently doing a test drive to check if it’s a good fit for my projects.

I would to know if its possible to create collections with parent-child relationships or nested collections. Something like the following example:

[
	{
		name: 'Example A',
		description: 'Example A description'
		children: [
			{
				name: 'Example A_1',
				description: 'Example A_1 description'
				children: [
					{
						name: 'Example A_1_1',
						description: 'Example A_1_1 description'
						children: []
					}
				]
			},
			{
				name: 'Example A_2',
				description: 'Example A_2 description'
				children: [
					{
						name: 'Example A_2_1',
						description: 'Example A_2_1 description'
						children: []
					}
				]
			}
		]
	},
	{
		name: 'Example B',
		description: 'Example B description'
		children: [
			{
				name: 'Example B_1',
				description: 'Example B_1 description'
				children: []
			}
		]
	},
	...
]

Is a structure like the above example possible? If so, how can I accomplish it?

Thanks in advance!

use TREE in content

Hi @creativeplusplus,

Thanks for the reply.

I’ve actually tried the Tree model, but was unable to link the “children” to another collection. When I select a collection model, i will have to manually select each item of that collection manually. How can I make a link to every item in a collection (current and future items)?

I dont understand what you try but for for json above just drag and drop like image below

Hi again @creativeplusplus,

Sorry for all the misunderstanding, but I’m really a zero at Cockpit :sweat_smile:

I started by creating a new Tree model based on your structure (as shown in the next image), but I don’t know how to make the “children” field an array of other fields:

Can you further explain the steps I have to do to manage the same structure that you are showing?

Once again thanks for the help.

I think I’m mistake the example I give you the pro addon menu it is a custom build by arthur,

The tree content JSON like this example below :

[
{
“name”: “one”,
“_pid”: “d2f1693c373935b7a0000181”,
“_o”: 0,
“_modified”: 1678260698,
“_mby”: “d2e87e453436640f2400027f”,
“_created”: 1659929250,
“_state”: 1,
“_cby”: “d2e87e453436640f2400027f”,
“_id”: “d2eee55e336362535100015d”
},
{
“name”: “two”,
“_pid”: null,
“_o”: 0,
“_modified”: 1678260679,
“_mby”: “d2e87e453436640f2400027f”,
“_created”: 1659929258,
“_state”: 1,
“_cby”: “d2e87e453436640f2400027f”,
“_id”: “d2f033d03363335d060001b9”
},
{
“name”: “three”,
“_pid”: “d2f033d03363335d060001b9”,
“_o”: 0,
“_modified”: 1678260686,
“_mby”: “d2e87e453436640f2400027f”,
“_created”: 1659929266,
“_state”: 1,
“_cby”: “d2e87e453436640f2400027f”,
“_id”: “d2f1693c373935b7a0000181”
},
{
“name”: “four”,
“_pid”: “d2eee55e336362535100015d”,
“_o”: 0,
“_modified”: 1678260708,
“_mby”: “d2e87e453436640f2400027f”,
“_created”: 1659929277,
“_state”: 1,
“_cby”: “d2e87e453436640f2400027f”,
“_id”: “d2f303e73337362d7a0000bd”
},
{
“name”: “five”,
“_pid”: null,
“_o”: 0,
“_state”: 1,
“_modified”: 1678260670,
“_mby”: “d2e87e453436640f2400027f”,
“_created”: 1678260670,
“_cby”: “d2e87e453436640f2400027f”,
“_id”: “814f91ea363931ddcf000043”
}
]

using _pid as parent id

you must convert JSON example below:

If you want you can try v1.getcockpit.com cockpit v1
the collection custom sortable entries output JSON children like you want

Use the Tree content model type.

Note: /api/content/tree/{model} api endpoint will be available in >=v2.4.0 (already available in the latest dev release)

3 Likes

Hey guys, thanks for the feedback!

@artur Thanks for the example video. I did try that, but it’s not possible to do what I was trying to accomplish: to have a link to all of the items in an existing Collection.

@creativeplusplus Thanks for the clarification. Using a legacy version won’t be an option for me :frowning:

Any other suggestion/workaround that would allow me to have nested collections?

Once again thanks for the help.

What exactly do you mean by this?

It’s a little hard to say if your problem can be solved using Cockpit’s systems with the only context being the structure you want your data to either be served as or end up as.

What’s the use case for the structure you provided in your first post?

Hi @rocamocha,

The use case that I’m trying to test out is how to store and retrieve a list of items with sub-categories. For example, I need to be able to manage several products with different category levels:

--- 1. Hair
----- 1.1. Shampoos & Masks
------- 1.1.2. Hidration & Nutrition
----- 1.2. Color & Discoloration
--- 2. Makup

The data model for each product is the same, and I would like to get all of the products from category ´Hair´ and its child category products in the same request.

allProducts = 
[
	{ 
		id: '1', 
		title: 'Product A', 
		category: 'Hair',
		childProducts: 
		[
			{
				id: '11',
				title: 'Product A-1',
				category: 'Shampoos & Masks',
				childProducts:
				[
					{
						id: '112',
						title: 'Product A-1-2',
						category: 'Hidration & Nutrition',
						childProducts: []
					}
				]
			},
			{
				id: '12',
				title: 'Product A-1',
				category: 'Color & Discoloration',
				childProducts: []
			}
		]
	},
	{
		id: '2',
		title: 'Product A-1-2',
		category: 'Hidration & Nutrition',
		childProducts: []
	}
];

Using just the Tree type isn’t very useful because there can be dozens of products in each category.

How can I accomplish such a structure in Cockpit?

Thanks in advance.