VGUI - DCategoryList

Hey guys, i have a problem with “DCategoryList” element on my journey learning gmod lua.

I m wondering how i could fill this element with content…
We have THIS in the gmod wiki, but it does not work for me, my parented panels are always displayed below the category bars and not inside as they should.

My code looks like this:



        .....
	for k, v in SortedPairs( catarr ) do
	    cat = vgui.Create( "DCategoryList", self )
            cat:SetTall(450)
            cat:SetWide(cat:GetWide())
            cat:Dock(FILL)
            cat:Add( k )
            local IconList = vgui.Create( "DPanel", cat )
            IconList:Dock(FILL)
            cat:AddItem( IconList )

            for key, ent in SortedPairsByMemberValue( v, "order" ) do


                AddSpawnIcon(ent.name, ent.limit, ent.lvlrq, ent.vip, ent.price, ent.hint, ent.model, icon_size*ix, icon_size*iy, IconList)
         .....


The main frame is a “DScrollPanel” and the AddSpawnIcon function creates a “DModelPanel” with some draw functions for nice buttons.

This works well in other panels but not in my DCategoryList…

Can anyone tell me please what am i missing here?

THX in advance
.delay

You should only need one DCategoryList and then to add new tabs to it use CategoryList:Add(name) which will return the new tab. Then with that tab you must use AddItem(Panel).

So your code should look more like this:
[lua]

local catList = vgui.Create( “DCategoryList”, self )
catList:SetTall(450)
catList:SetWide(catList:GetWide())
catList:Dock(FILL)

for k, v in SortedPairs( catarr ) do
local cat = catList:Add( k )
local IconList = vgui.Create( “DPanel”, cat )
IconList:Dock(FILL)
cat:AddItem( IconList )

for key, ent in SortedPairsByMemberValue( v, "order" ) do

    AddSpawnIcon(ent.name, ent.limit, ent.lvlrq, ent.vip, ent.price, ent.hint, ent.model, icon_size*ix, icon_size*iy, IconList)


[/lua]

Oh yeah, thanks. That was a stupid mistake.
But it is still not working like it should. Btw the old “DCollapsibleCategory” works just great, but we should not use it because it’s depreciated??

Anyway, my new code looks like this and still gives me a nil error:



local PANEL = {}
function PANEL:Init()  
    
	function self:Paint()
		draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 192,192,192 ) )
	end
    
    -----
	local catarr = {}
	local SpawnableEntities = weapon
	if ( SpawnableEntities ) then
		for k, v in pairs( SpawnableEntities ) do
			v.cat = v.cat or "Other"
			catarr[ v.cat ] = catarr[ v.cat ] or {}
			table.insert( catarr[ v.cat ], v )
		end
	end
    -----
    
	local catList = vgui.Create( "DCategoryList", self )
    
       catList:SetTall(450)
       catList:SetWide(catList:GetWide())
       catList:Dock(FILL)
    
	for k, v in SortedPairs( catarr ) do
    
                local cat = catList:Add( k )
		local IconList = vgui.Create( "DPanel", cat )

                IconList:Dock(FILL)
		cat:AddItem( IconList )

                local icon_size = 92
                local ix = 0
                local iy = 0
		for key, ent in SortedPairsByMemberValue( v, "order" ) do
                        if(wide_max < (ix*icon_size)+icon_size) then
				ix = 0
				iy = iy + 1
			end
                AddSpawnIcon(ent.name, ent.limit, ent.lvlrq, ent.vip, ent.price, ent.hint, ent.model, ent.cmd, ent.ent, icon_size*ix, icon_size*iy, IconList)
                ix = ix + 1
		end
	end 
end
vgui.Register( "SpawnMenu_weapon", PANEL, "Panel" )


For some reason “AddItem” always returns nil: "attempt to call method “AddItem” - a nil value

Any suggestions? :smiley:

Instead of cat:AddItem( IconList) try cat:SetContents( IconList ).

Ok tried this, but now the the categories have no entrys, but without any errors -.-

I guess i will just use the DCollapsibleCategory, works fine.