WoW:Using UIDropDownMenu: Difference between revisions

(Link new HOWTO that discusses how to minimize garbage of throw-away tables and have UI examples.)
Line 82: Line 82:


[[Category:HOWTOs|Use UIDropDownMenu]]
[[Category:HOWTOs|Use UIDropDownMenu]]
== HOWTO: nested submenu structures with UIDropDownMenu ==
If you want to split menus, this code examples may help you:
  -- creating test data structure
  local Test1_Data = {
    ["level1_test_1"] = {
      [1] = { ["name"] = "sublevel 1"; },
      [2] = { ["name"] = "sublevel 2"; },
    },
    ["level1_test_2"] = {
      [1] = { ["name"] = "sublevel A"; },
      [2] = { ["name"] = "sublevel B"; },
    }
  }
  -- menu create function
  function Test1_DropDown_Initialize(level)
    level = level or 1;
    if (level == 1) then
      for key, subarray in pairs(Test1_Data) do
        local info = UIDropDownMenu_CreateInfo();
        info.hasArrow = true; -- creates submenu
        info.notCheckable = true;
        info.text = key;
        info.value = {
          ["Level1_Key"] = key;
        };
        UIDropDownMenu_AddButton(info, level);
      end -- for key, subarray
    end -- if level 1
    if (level == 2) then
      -- getting values of first menu
      local Level1_Key = UIDROPDOWNMENU_MENU_VALUE["Level1_Key"];
      subarray = Test1_Data[Level1_Key];
      for key, subsubarray in pairs(subarray) do
        local info = UIDropDownMenu_CreateInfo();
        info.hasArrow = false; -- no submenues this time
        info.notCheckable = true;
        info.text = subsubarray["name"];
        -- use info.func to set a function to be called at "click"
        info.value = {
          ["Level1_Key"] = Level1_Key;
          ["Sublevel_Key"] = key;
        };
        UIDropDownMenu_AddButton(info, level);
      end -- for key,subsubarray
    end -- if level 2
  end -- function Test1_DropDown_Initialize
To use the Menu, you can do
  local dropdown = CreateFrame("Frame", "Test_DropDown", UIParent, "UIDropDownMenuTemplate");
  UIDropDownMenu_Initialize(dropdown, Test1_DropDown_Initialize, "MENU");
and you need something to active the menu, as with any menu
  ToggleDropDownMenu(1, nil, dropdown, self, -20, 0);
Anonymous user