动态表列 [英] Dynamic table columns
问题描述
当我想从仅包含字符串(例如,csv中的数据)的列表列表中生成表时,应该如何进行.列的名称无关紧要.从提供的所有示例中,我仅看到将表项绑定到特定模型(由于我不知道列的数目和名称,因此不适合该模型).
How should I proceed when I want to generate table from list of lists which contains only strings(ex. data from csv). Names of columns don't matter. From all examples provided I saw only binding table items to specific model(which doesn't fit there as I have unknown number and names of columns).
推荐答案
如果您已经知道列名和数据类型,我建议对其进行硬编码.如果您对格式一无所知,而只想创建具有完全动态列的TableView,则可以将csv数据中的索引用作提取器来为数据创建StringProperty
值:
If you already know the column names and data type, I would suggest to hard code that. If you know nothing about the format and simply want to create a TableView with completely dynamic columns, you can use the index in the csv data as an extractor to create StringProperty
values for your data:
class MyView : View() {
val data = FXCollections.observableArrayList<List<String>>()
val csvController: CsvController by inject()
init {
runAsync {
csvController.loadData()
} ui { entries ->
// Generate columns based on the first row
entries.first().forEachIndexed { colIndex, name ->
root.column(name, String::class) {
value { row ->
SimpleStringProperty(row.value[colIndex])
}
}
}
// Assign the extracted entries to our list, skip first row
data.setAll(entries.drop(1))
}
}
override val root = tableview(data)
}
class CsvController : Controller() {
// Load data from CSV file here, we'll use som static data
// where the first row is the headers
fun loadData() = listOf(
listOf("Name", "Age"),
listOf("John", "42"),
listOf("Jane", "24")
)
}
此方法仅适用于可视化CSV文件中的数据.如果您需要编辑或操作数据,则对数据类型的了解可以简化IMO的应用程序:)
This approach would only be good for visualizing the data in a CSV file. If you need to edit or manipulate the data, knowledge of the data types up front would yield a less flimsy application IMO :)
这篇关于动态表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!