LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

TableHeaderElement

Description
Table header element to use within tables. Children which serve as sorting icons are to be marked in the screen XML configuration by setting the name attribute to "iconAscending" or "iconDescending" depending on which sorting state they are intended to represent. Headers must be defined outside of tables, because anything within a table is considered a table item.
Parent
ButtonElement
XML Configuration Parameters
GuiElement#targetTableIdstring Configured ID of the decorated table
GuiElement#columnNamestring Name of the table column which will be sorted when this header is activated
GuiElement#allowSortingAscbool [optional, default=false] If set to true, allows sorting the associated table column in ascending order.
GuiElement#allowSortingDescbool [optional, default=false] If set to true, allows sorting the associated table column in descending order.

Functions

addElement

Description
Add a child element to this GUI element. This element searches for marked children to use as sorting icons.
Definition
addElement(element Element)
Arguments
elementElementto add.
Code
100function TableHeaderElement:addElement(element)
101 TableHeaderElement:superClass().addElement(self, element)
102
103 -- find header icons by configuration attribute
104 if element.name == TableHeaderElement.NAME_ASC_ICON then
105 self.sortingIcons[TableHeaderElement.SORTING_ASC] = element
106 end
107 if element.name == TableHeaderElement.NAME_DESC_ICON then
108 self.sortingIcons[TableHeaderElement.SORTING_DESC] = element
109 end
110end

copyAttributes

Description
Definition
copyAttributes()
Code
88function TableHeaderElement:copyAttributes(src)
89 TableHeaderElement:superClass().copyAttributes(self, src)
90
91 self.targetTableId = src.targetTableId
92 self.columnName = src.columnName
93 self.allowedSortingStates = {unpack(src.allowedSortingStates)}
94end

disableSorting

Description
Disable sorting on this header by setting its sorting state to OFF.
Definition
disableSorting()
Code
132function TableHeaderElement:disableSorting()
133 self.sortingOrder = TableHeaderElement.SORTING_OFF
134 self:updateSortingDisplay()
135end

loadFromXML

Description
Definition
loadFromXML()
Code
64function TableHeaderElement:loadFromXML(xmlFile, key)
65 TableHeaderElement:superClass().loadFromXML(self, xmlFile, key)
66
67 self.targetTableId = Utils.getNoNil(getXMLString(xmlFile, key.."#targetTableId"), self.targetTableId)
68 self.columnName = Utils.getNoNil(getXMLString(xmlFile, key.."#columnName"), self.columnName)
69
70 local allowAscendingSort = Utils.getNoNil(getXMLBool(xmlFile, key.."#allowSortingAsc"), self.allowedSortingStates[TableHeaderElement.SORTING_ASC])
71 local allowDescendingSort = Utils.getNoNil(getXMLBool(xmlFile, key.."#allowSortingDesc"), self.allowedSortingStates[TableHeaderElement.SORTING_DESC])
72 self.allowedSortingStates[TableHeaderElement.SORTING_ASC] = allowAscendingSort
73 self.allowedSortingStates[TableHeaderElement.SORTING_DESC] = allowDescendingSort
74end

loadProfile

Description
Definition
loadProfile()
Code
78function TableHeaderElement:loadProfile(profile, applyProfile)
79 TableHeaderElement:superClass().loadProfile(self, profile, applyProfile)
80
81 self.columnName = profile:getValue("columnName", self.columnName)
82 self.allowedSortingStates[TableHeaderElement.SORTING_ASC] = profile:getBool("allowSortingAsc", self.allowedSortingStates[TableHeaderElement.SORTING_ASC])
83 self.allowedSortingStates[TableHeaderElement.SORTING_DESC] = profile:getBool("allowSortingDesc", self.allowedSortingStates[TableHeaderElement.SORTING_DESC])
84end

new

Description
Definition
new()
Code
40function TableHeaderElement.new(target, custom_mt)
41 local self = ButtonElement.new(target, custom_mt or TableHeaderElement_mt)
42
43 self.allowedSortingStates = {
44 [TableHeaderElement.SORTING_OFF] = true, -- no reason to ever change this, only here for processing
45 [TableHeaderElement.SORTING_ASC] = false,
46 [TableHeaderElement.SORTING_DESC] = false,
47 }
48 self.sortingOrder = TableHeaderElement.SORTING_OFF -- state index in allowed sorting states
49
50 self.sortingIcons = {
51 [TableHeaderElement.SORTING_OFF] = nil, -- off state icon element
52 [TableHeaderElement.SORTING_ASC] = nil, -- ascending sort state icon element
53 [TableHeaderElement.SORTING_DESC] = nil -- descending sort state icon element
54 }
55
56 self.targetTableId = "" -- ID of decorated table
57 self.columnName = "" -- name of table column to sort
58
59 return self
60end

toggleSorting

Description
Toggle this header's sorting display state, if allowed.
Definition
toggleSorting()
Return Values
Thenewsorting order
Code
115function TableHeaderElement:toggleSorting()
116 -- cycle to the next allowed sorting state (will stop at off setting at the latest):
117 local prevOrderIndex = self.sortingOrder
118
119 repeat
120 self.sortingOrder = ((self.sortingOrder) % #self.allowedSortingStates) + 1
121 until self.allowedSortingStates[self.sortingOrder]
122
123 if not (prevOrderIndex == self.sortingOrder) then
124 self:updateSortingDisplay()
125 end
126
127 return self.sortingOrder
128end

updateSortingDisplay

Description
Update the header's display with its new state.
Definition
updateSortingDisplay()
Code
139function TableHeaderElement:updateSortingDisplay()
140 -- enable current state, disable others
141 for sortOrderIndex, icon in pairs(self.sortingIcons) do
142 if sortOrderIndex == self.sortingOrder then
143 icon:setVisible(true)
144 else
145 icon:setVisible(false)
146 end
147 end
148end