Where you don’t want a header (or footer), simply omit from the declaration of the table.
For List based variants, the header and footer are inside the scrolling region. For Stack and Grid based variants, they are outside. (This may be configurable at some point once any scaling/performance issues are resolved.)
Column Sorting
Column sorting is available through the tablerSort view function.
The examples below show how the header items can support sort.
.columnTitle() is a convenience function that displays header name along with an indicator showing the current sort state, if any. Alternatively, build your own header and call the .indicator() method to get the active indicator image.
Caret images are used by default for indicators, but are configurable (see Configuration section below).
Note that for Core Data, the user’s changes will need to be saved to the Managed Object Context. See the TablerCoreDemo code for an example of how this might be done.
Row Background
You have the option to specify a row background, such as to impart information, or as a selection indicator.
Row Background, as the name suggests, sits BEHIND the row.
macOS
iOS
An example of using row background to impart information, as shown above:
sortIndicatorNeutral: AnyView - “chevron.up” image, with opacity of 0
List
List configuration is optional.
TablerListConfig<Element>.init parameters:
canMove: CanMove<Element> - with a default of { _ in true }, allowing any row to move (if onMove defined)
canDelete: CanDelete<Element> - with a default of { _ in true }, allowing any row to be deleted (if onDelete defined), currently only via swipe menu on iOS
onMove: OnMove<Element>? - with a default of nil, prohibiting any move
onDelete: OnDelete<Element>? - with a default of nil, prohibiting any delete, currently only via swipe menu on iOS
filter: Filter? - with a default of nil, indicating no filtering
onHover: (Element.ID, Bool) -> Void - defaults to { _,_ in }
tablePadding: EdgeInsets - per Base defaults
sortIndicatorForward: AnyView - per Base defaults
sortIndicatorReverse: AnyView - per Base defaults
sortIndicatorNeutral: AnyView - per Base defaults
Stack
Stack configuration is optional.
TablerStackConfig<Element>.init parameters:
rowPadding: EdgeInsets - Stack-specific defaults; varies by platform
headerSpacing: CGFloat - default varies by platform
footerSpacing: CGFloat - default varies by platform
rowSpacing: CGFloat - default of 0
filter: Filter? - with a default of nil, indicating no filtering
onHover: (Element.ID, Bool) -> Void - defaults to { _,_ in }
tablePadding: EdgeInsets - default varies by platform
sortIndicatorForward: AnyView - per Base defaults
sortIndicatorReverse: AnyView - per Base defaults
sortIndicatorNeutral: AnyView - per Base defaults
Grid
Grid configuration is required, where you supply a GridItem array.
TablerGridConfig<Element>.init parameters:
gridItems: [GridItem] - required
alignment: HorizontalAlignment - LazyVGrid alignment, with a default of .leading
itemPadding: EdgeInsets - Grid-specific defaults, varies by platform
headerSpacing: CGFloat - default varies by platform
footerSpacing: CGFloat - default varies by platform
rowSpacing: CGFloat - default of 0
filter: Filter? - with a default of nil, indicating no filtering
onHover: (Element.ID, Bool) -> Void - defaults to { _,_ in }
tablePadding: EdgeInsets - default varies by platform
sortIndicatorForward: AnyView - per Base defaults
sortIndicatorReverse: AnyView - per Base defaults
sortIndicatorNeutral: AnyView - per Base defaults
Horizontal Scrolling
On compact displays you may wish to scroll the table horizontally.
You can wrap in your own ScrollView, or alternatively import the SwiftSideways package:
import Tabler
import Sideways
var body: some View {
TablerList(header: header,
row: row,
results: fruits)
.sideways(minWidth: 400)
}
AutoInit Code Generation
This applies only to those forking and customizing the Tabler code.
Many additional init() functions for each table variant are generated via the code template Templates/AutoInit.stencil.
To regenerate and re-format, run the Sourcery command from the project directory.
This library is a member of the OpenAlloc Project.
OpenAlloc - product website for all the OpenAlloc apps and libraries
OpenAlloc Project - Github site for the development project, including full source code
License
Copyright 2021, 2022 OpenAlloc LLC
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributing
Contributions are welcome. You are encouraged to submit pull requests to fix bugs, improve documentation, or offer new features.
The pull request need not be a production-ready feature or fix. It can be a draft of proposed changes, or simply a test to show that expected behavior is buggy. Discussion on the pull request can proceed from there.
SwiftTabler
A multi-platform SwiftUI component for tabular data.
Available as an open source library to be incorporated in SwiftUI apps.
SwiftTabular is part of the OpenAlloc family of open source Swift software tools.
Features
RandomAccessCollection
data sourcesAnyView
), which can impact scalability and performance**Three table types are supported, as determined by the mechanism by which their header and rows are rendered.
List
List
Stack
ScrollView
/LazyVStack
Grid
ScrollView
/LazyVGrid
* Other platforms like macCatalyst, iPad on Mac, watchOS, tvOS, etc. are poorly supported, if at all. Please contribute to improve support!
** AnyView only used to specify sort images in configuration, which shouldn’t impact scalability.
Tabler Example
The example below shows the display of tabular data from an array of values using
TablerList
, a simple variant based onList
.While
LazyVGrid
is used here to wrap the header and row items, you could alternatively wrap them withHStack
or similar mechanism.Tabler Views
Tabler offers twenty-seven (27) variants of table views from which you can choose. They break down along the following lines:
List
ScrollView
/LazyVStack
ScrollView
/LazyVGrid
TextField
, etc.) to mutate modelconfig.filter
is supported (see caveat below)TablerList
TablerListB
TablerListC
TablerList1
TablerList1B
TablerList1C
TablerListM
TablerListMB
TablerListMC
TablerStack
TablerStackB
TablerStackC
TablerStack1
TablerStack1B
TablerStack1C
TablerStackM
TablerStackMB
TablerStackMC
TablerGrid
TablerGridB
TablerGridC
TablerGrid1
TablerGrid1B
TablerGrid1C
TablerGridM
TablerGridMB
TablerGridMC
* filtering with bound values likely not scalable as implemented. If you can find a better way to implement, please submit a pull request!
Header/Footer
Optionally attach a header (or footer) to your table:
Where you don’t want a header (or footer), simply omit from the declaration of the table.
For List based variants, the header and footer are inside the scrolling region. For Stack and Grid based variants, they are outside. (This may be configurable at some point once any scaling/performance issues are resolved.)
Column Sorting
Column sorting is available through the
tablerSort
view function.The examples below show how the header items can support sort.
.columnTitle()
is a convenience function that displays header name along with an indicator showing the current sort state, if any. Alternatively, build your own header and call the.indicator()
method to get the active indicator image.Caret images are used by default for indicators, but are configurable (see Configuration section below).
Random Access Collection
From the TablerDemo app:
Core Data
The sort method used with Core Data differs. From the TablerCoreDemo app:
Sorting on a computed column
Where there is no key path available to store in the sort context, such as for a computed value, create a place holder key path.
Bound data
When used with ‘bound’ views (e.g.,
TablerListB
orTablerListC
), the data can be modified directly, mutating your data source. From the demo:For value sources,
BoundValue
is a binding:For reference sources, including Core Data,
BoundValue
is an object wrapper (aka ‘ProjectedValue’):Note that for Core Data, the user’s changes will need to be saved to the Managed Object Context. See the TablerCoreDemo code for an example of how this might be done.
Row Background
You have the option to specify a row background, such as to impart information, or as a selection indicator.
Row Background, as the name suggests, sits BEHIND the row.
An example of using row background to impart information, as shown above:
An example of a selection indicator using row background, such as for Stack based tables which do not have a native selection indicator:
Row Overlay
Similar to a row background, an overlay can be used to impart information, or to use as a selection indicator.
Row overlay, as the name suggests, sits ATOP the row.
An example of a selection indicator using row overlay:
Hover Events
For macOS only, you can capture hover events, typically to highlight the row under the mouse cursor.
To coordinate hover with other backgrounds, such as for selection on Stack tables, see the demo apps.
Moving Rows
Row moving via drag and drop is available for the List based variants.
An example for use with Random Access Collections, as seen in TablerDemo:
TODO need Core Data example, if it’s possible to do so.
Configuration
Configuration options will vary by table type.
Defaults can vary by platform (macOS, iOS, etc.). See the code for specifics.
Spacing defaults are driven by the goal of achieving uniform appearance among table types, with the List type serving as the standard.
Base Defaults
Base defaults are defined in the
TablerConfig
module.tablePadding: EdgeInsets
- no paddingsortIndicatorForward: AnyView
- “chevron.up” imagesortIndicatorReverse: AnyView
- “chevron.down” imagesortIndicatorNeutral: AnyView
- “chevron.up” image, with opacity of 0List
List configuration is optional.
TablerListConfig<Element>.init
parameters:canMove: CanMove<Element>
- with a default of{ _ in true }
, allowing any row to move (ifonMove
defined)canDelete: CanDelete<Element>
- with a default of{ _ in true }
, allowing any row to be deleted (ifonDelete
defined), currently only via swipe menu on iOSonMove: OnMove<Element>?
- with a default ofnil
, prohibiting any moveonDelete: OnDelete<Element>?
- with a default ofnil
, prohibiting any delete, currently only via swipe menu on iOSfilter: Filter?
- with a default ofnil
, indicating no filteringonHover: (Element.ID, Bool) -> Void
- defaults to{ _,_ in }
tablePadding: EdgeInsets
- per Base defaultssortIndicatorForward: AnyView
- per Base defaultssortIndicatorReverse: AnyView
- per Base defaultssortIndicatorNeutral: AnyView
- per Base defaultsStack
Stack configuration is optional.
TablerStackConfig<Element>.init
parameters:rowPadding: EdgeInsets
- Stack-specific defaults; varies by platformheaderSpacing: CGFloat
- default varies by platformfooterSpacing: CGFloat
- default varies by platformrowSpacing: CGFloat
- default of 0filter: Filter?
- with a default ofnil
, indicating no filteringonHover: (Element.ID, Bool) -> Void
- defaults to{ _,_ in }
tablePadding: EdgeInsets
- default varies by platformsortIndicatorForward: AnyView
- per Base defaultssortIndicatorReverse: AnyView
- per Base defaultssortIndicatorNeutral: AnyView
- per Base defaultsGrid
Grid configuration is required, where you supply a
GridItem
array.TablerGridConfig<Element>.init
parameters:gridItems: [GridItem]
- requiredalignment: HorizontalAlignment
-LazyVGrid
alignment, with a default of.leading
itemPadding: EdgeInsets
- Grid-specific defaults, varies by platformheaderSpacing: CGFloat
- default varies by platformfooterSpacing: CGFloat
- default varies by platformrowSpacing: CGFloat
- default of 0filter: Filter?
- with a default ofnil
, indicating no filteringonHover: (Element.ID, Bool) -> Void
- defaults to{ _,_ in }
tablePadding: EdgeInsets
- default varies by platformsortIndicatorForward: AnyView
- per Base defaultssortIndicatorReverse: AnyView
- per Base defaultssortIndicatorNeutral: AnyView
- per Base defaultsHorizontal Scrolling
On compact displays you may wish to scroll the table horizontally.
You can wrap in your own
ScrollView
, or alternatively import the SwiftSideways package:AutoInit Code Generation
This applies only to those forking and customizing the Tabler code.
Many additional
init()
functions for each table variant are generated via the code templateTemplates/AutoInit.stencil
.To regenerate and re-format, run the Sourcery command from the project directory.
The generated code will be found in the
Sources/Generated
directory.See Also
Apps demonstrating Tabler:
This library is a member of the OpenAlloc Project.
License
Copyright 2021, 2022 OpenAlloc LLC
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributing
Contributions are welcome. You are encouraged to submit pull requests to fix bugs, improve documentation, or offer new features.
The pull request need not be a production-ready feature or fix. It can be a draft of proposed changes, or simply a test to show that expected behavior is buggy. Discussion on the pull request can proceed from there.