LUADOC - Farming Simulator 19

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
103function TableHeaderElement:addElement(element)
104 TableHeaderElement:superClass().addElement(self, element);
105
106 -- find header icons by configuration attribute
107 if element.name == TableHeaderElement.NAME_ASC_ICON then
108 self.sortingIcons[TableHeaderElement.SORTING_ASC] = element;
109 end;
110 if element.name == TableHeaderElement.NAME_DESC_ICON then
111 self.sortingIcons[TableHeaderElement.SORTING_DESC] = element;
112 end;
113end

copyAttributes

Description
Definition
copyAttributes()
Code
91function TableHeaderElement:copyAttributes(src)
92 TableHeaderElement:superClass().copyAttributes(self, src);
93
94 self.targetTableId = src.targetTableId;
95 self.columnName = src.columnName;
96 self.allowedSortingStates = {unpack(src.allowedSortingStates)};
97end

disableSorting

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

loadFromXML

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

loadProfile

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

new

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

toggleSorting

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

updateSortingDisplay

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