View Single Post
04-02-21, 10:59 AM   #5
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
Very often, displaying information in table form generally means creating a frame/button for each row where each row is broken up into sections (columns), and then positioning each row one below the next so they form a table. Then you loop through the rows and update the columns.

It's very common to do this with a scrollframe too, which allows your table to be scrollable.

The code that follows adds a /showme slash command to summon a scrollable table of random data. (You can repeat /showme to generate new random data.)

When the number of rows has the potential to far exceed the displayable area, it's common to use a HybridScrollFrameTemplate or FauxScrollFrameTemplate. Then you only have as many rows that can be displayed and you update the content based on the scrollbar offset. In the example below I've doing a basic scrollframe where every row is defined so you don't need to worry about offsets. (This is not suggested for long lists, but probably easier to pick apart.)

Lua Code:
  1. local CELL_WIDTH = 100
  2. local CELL_HEIGHT = 20
  3. local NUM_CELLS = 3 -- cells per row
  4.  
  5. -- we'll use an ordered table of arbitrary data: {{fruit,number,alphabet},{fruit,number,alphabet},etc}
  6. local fruits = {"apple","banana","orange","pear","tomato","pineapple"}
  7. local numbers = {"one","two","three","four","five","six"}
  8. local alphabet = {"abc","def","ghij","klmn","op","qrstuv","wx","yz"}
  9. local data = {}
  10.  
  11. -- just generates a random bunch of data in the data table
  12. local function updateData()
  13.     wipe(data)
  14.     for i=1,10+random(40) do
  15.         tinsert(data,{fruits[random(#fruits)], numbers[random(#numbers)], alphabet[random(#alphabet)]})
  16.     end
  17. end
  18.  
  19. -- creating a basic dialog frame (if you name this frame and define basic bits during the load (not after PLAYER_LOGIN), then
  20. -- the client will restore the window position on login to wherever it was last moved by the user)
  21. local f = CreateFrame("Frame","SimpleScrollFrameTableDemo",UIParent,"BasicFrameTemplateWithInset")
  22. f:SetSize(CELL_WIDTH*NUM_CELLS+40,300)
  23. f:SetPoint("CENTER")
  24. f:Hide()
  25. f:SetMovable(true)
  26. f:SetScript("OnMouseDown",f.StartMoving)
  27. f:SetScript("OnMouseUp",f.StopMovingOrSizing)
  28.  
  29. -- adding a scrollframe (includes basic scrollbar thumb/buttons and functionality)
  30. f.scrollFrame = CreateFrame("ScrollFrame",nil,f,"UIPanelScrollFrameTemplate")
  31. f.scrollFrame:SetPoint("TOPLEFT",12,-32)
  32. f.scrollFrame:SetPoint("BOTTOMRIGHT",-34,8)
  33.  
  34. -- creating a scrollChild to contain the content
  35. f.scrollFrame.scrollChild = CreateFrame("Frame",nil,f.scrollFrame)
  36. f.scrollFrame.scrollChild:SetSize(100,100)
  37. f.scrollFrame.scrollChild:SetPoint("TOPLEFT",5,-5)
  38. f.scrollFrame:SetScrollChild(f.scrollFrame.scrollChild)
  39.  
  40. -- adding content to the scrollChild
  41. local content = f.scrollFrame.scrollChild
  42. content.rows = {} -- each row of data is one wide button stored here
  43.  
  44. local function updateList()
  45.     for i=1,#data do
  46.         -- create a row if not created yet (buttons[i] is a whole row; buttons[i].columns[j] are columns)
  47.         if not content.rows[i] then
  48.             local button = CreateFrame("Button",nil,content)
  49.             button:SetSize(CELL_WIDTH*NUM_CELLS,CELL_HEIGHT)
  50.             button:SetPoint("TOPLEFT",0,-(i-1)*CELL_HEIGHT)
  51.             button.columns = {} -- creating columns for the row
  52.             for j=1,3 do
  53.                 button.columns[j] = button:CreateFontString(nil,"ARTWORK","GameFontHighlight")
  54.                 button.columns[j]:SetPoint("LEFT",(j-1)*CELL_WIDTH,0)
  55.             end
  56.             content.rows[i] = button
  57.         end
  58.         -- now actually update the contents of the row
  59.         for j=1,3 do
  60.             content.rows[i].columns[j]:SetText(data[i][j])
  61.         end
  62.         -- show the row that has data
  63.         content.rows[i]:Show()
  64.     end
  65.     -- hide all extra rows (if list shrunk, hiding leftover)
  66.     for i=#data+1,#content.rows do
  67.         content.rows[i]:Hide()
  68.     end
  69. end
  70.  
  71. -- create /showme slash command to make up data and summon window
  72. SLASH_SHOWME1 = "/showme"
  73. SlashCmdList["SHOWME"] = function(msg)
  74.     updateData()
  75.     updateList()
  76.     f:Show()
  77. end

Last edited by Gello : 04-02-21 at 11:02 AM.
  Reply With Quote