Thread: Trying to Learn
View Single Post
06-30-19, 06:29 PM   #33
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Probably easier to use a filtered "display" table to store information from the main table.

Lua Code:
  1. local upper = string.upper
  2. local classes = { "DH", "DK", "Druid", "Hunter", "Mage", "Monk", "Paladin", "Priest", "Rogue", "Shaman", "Warlock", "Warrior" } -- Sorted to use keys[i] insted of full class name in the table
  3. local width, height, numrows = 200, 20, 6
  4. local TableData = {}
  5. local FilteredData = {}
  6.  
  7. for i=1, 30 do
  8.     tinsert(TableData, { Player=i, Class=random(1, #classes), DKP=random(0, 10000) })
  9. end
  10.  
  11. local function OnClick(self)
  12.     print(self.Strings[1]:GetText(), self.Strings[2]:GetText(), self.Strings[3]:GetText())
  13. end
  14.  
  15. function CreateRow(parent, id) -- Create 3 buttons for each row in the list
  16.     local f = CreateFrame("Button", "$parentLine"..id, parent)
  17.     f.Strings = {}
  18.     f:SetSize(width, height)
  19.     f:SetHighlightTexture("Interface\\BUTTONS\\UI-Listbox-Highlight2.blp");
  20.     f:SetScript("OnClick", OnClick)
  21.     for i=1, 3 do
  22.         f.Strings[i] = f:CreateFontString(nil, "OVERLAY");
  23.         f.Strings[i]:SetFontObject("GameFontHighlight");
  24.         f.Strings[i]:SetTextColor(1, 1, 1, 1);
  25.     end
  26.     f.Strings[1]:SetPoint("LEFT")
  27.     f.Strings[2]:SetPoint("CENTER")
  28.     f.Strings[3]:SetPoint("RIGHT", -30, 0)
  29.     return f
  30. end
  31.  
  32. local function ScrollFrame_Update(self)
  33.     local numOptions = #FilteredData
  34.     local index, row
  35.     local offset = FauxScrollFrame_GetOffset(self) or 0
  36.     for i=1, numrows do
  37.         row = self.Rows[i]
  38.         index = offset + i
  39.         if FilteredData[index] then
  40.             row:Show()
  41.             row.index = index
  42.             row.Strings[1]:SetText("Player"..FilteredData[index].Player)
  43.             row.Strings[2]:SetText(classes[FilteredData[index].Class])
  44.             row.Strings[3]:SetText(FilteredData[index].DKP)
  45.         else
  46.             row:Hide()
  47.         end
  48.     end
  49.     FauxScrollFrame_Update(self, numOptions, numrows, height, nil, nil, nil, nil, nil, nil, true) -- alwaysShowScrollBar= true to stop frame from hiding
  50. end
  51.  
  52. local f = CreateFrame("ScrollFrame", "FizzleScrollFrame", UIParent, "FauxScrollFrameTemplate")
  53. f:SetSize(width, height*numrows)
  54. f:SetPoint("LEFT", 20, 0)
  55. f.ScrollBar = FauxScrollFrame_GetChildFrames(f)
  56. f.Rows = {}
  57. for i=1, numrows do
  58.     f.Rows[i] = CreateRow(f, i)
  59.     f.Rows[i]:SetPoint("TOPLEFT", f.Rows[i-1] or f, f.Rows[i-1] and "BOTTOMLEFT" or "TOPLEFT")
  60. end
  61. f:SetScript("OnVerticalScroll", function(self, offset)
  62.     FauxScrollFrame_OnVerticalScroll(self, offset, height, ScrollFrame_Update)
  63. end)
  64.  
  65. ------------ Add Sorting and Header Buttons -------------------------
  66. local SortButtons = {}
  67.  
  68. local function SortTable(id, reset)
  69.     local button = SortButtons[id]
  70.     if reset then
  71.         button.Ascend = true
  72.     else
  73.         button.Ascend = not button.Ascend
  74.     end
  75.     local Suffix = button.Ascend and " ^" or " v"
  76.     button:SetText(button.Id .. Suffix)
  77.     for k, v in pairs(SortButtons) do
  78.         if v ~= button then
  79.             v.Ascend = nil
  80.             v:SetText(v.Id)
  81.         end
  82.     end
  83.     table.sort(FilteredData, function(a, b)
  84.         if button.Ascend then
  85.                 return a[button.Id] < b[button.Id]
  86.         else
  87.             return a[button.Id] > b[button.Id]
  88.         end
  89.         end)
  90.         ScrollFrame_Update(f)
  91. end
  92.  
  93. -- add the buttons
  94. SortButtons.Player = CreateFrame("Button", "FizzleSortButtonPlayer", f, "UIPanelButtonTemplate")
  95. SortButtons.Class = CreateFrame("Button", "FizzleSortButtonClass", f, "UIPanelButtonTemplate")
  96. SortButtons.DKP = CreateFrame("Button", "FizzleSortButtonDkp", f, "UIPanelButtonTemplate")
  97.  
  98. SortButtons.Class:SetPoint("BOTTOM", f, "TOP", 0, 5)
  99. SortButtons.Player:SetPoint("RIGHT", SortButtons.Class, "LEFT")
  100. SortButtons.DKP:SetPoint("LEFT", SortButtons.Class, "RIGHT")
  101.  
  102. for k, v in pairs(SortButtons) do
  103.     v.Id = k
  104.     v:SetText(k)
  105.     v:SetSize(width/3, height)
  106.     v:SetScript("OnClick", function(self) SortTable(self.Id) end)
  107. end
  108.  
  109. ------------ Add Filtering on Class and Edit Box -------------------------
  110. local function FilterTable(filter, sort)
  111.     table.wipe(FilteredData)
  112.     for k, v in pairs(TableData) do
  113.         if filter == "" or upper(classes[v.Class]) == upper(filter) then
  114.             tinsert(FilteredData, v)
  115.         end
  116.     end
  117.     SortTable(sort)
  118. end
  119.  
  120. -- add the editbox and label
  121. local Filter = CreateFrame("EditBox", "FizzleFilter", FizzleScrollFrame, "InputBoxTemplate")
  122. Filter:SetSize(75, 20)
  123. Filter:SetAutoFocus(false)
  124. Filter:SetPoint("BOTTOMLEFT", SortButtons.Player, "TOPLEFT", 0, 2)
  125. Filter:SetScript("OnEnterPressed", function(self)
  126.     FilterTable(self:GetText(), "Player")
  127. end)
  128. Filter.Label = Filter:CreateFontString()
  129. Filter.Label:SetFont("Fonts\\FRIZQT__.TTF", 12)
  130. Filter.Label:SetPoint("LEFT", Filter, "RIGHT", 2, 0)
  131. Filter.Label:SetText("Filter Class [press Enter]")
  132.  
  133. ------------ Let's load the list to start -------------------------
  134. FilterTable("", "Player")
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-30-19 at 11:54 PM.
  Reply With Quote