使用数据表(Meteor Tabular)在新行中绘制数组的每个元素

Draw each element of array in new row with DataTables (Meteor Tabular)

本文关键字:数组 绘制 元素 新行中 数据表 Meteor Tabular      更新时间:2023-09-26

我正在使用实现DataTables的Meteor Tabular包。我正在尝试从 Mongo 集合创建一个表。集合有一个表单的文档

{
  input: Array[365],
  output: Array[365],
  date: Array[365]
}

我使用以下代码定义 Meteor 中的表

TabularTables.MyTable = new Tabular.Table({
    name: "MyTable",
    collection: MyTable,
    columns: [
        {data: "input", title: "Input", searchable: false},
        {data: "output", title: "Output", searchable: false},
        {data: "date", title: "Date", searchable: false}
    ],
    order: [[1, "desc"]],
    pageLength: 10
});

问题是,当绘制它时,每个变量的所有 365 个元素最终都在一个单元格中,所以我有一大行。我希望每个元素都在单独的行中创建,即

Input      Output      Date
input[0]   output[0]   date[0]
input[1]   output[1]   date[1]

而目前是

Input            Output            Date
input[0...364]   output[0...364]   date[0...364]

您需要转换数据,然后将其放入本地集合中,因为该包不接受数组(与我之前的想法相反)。

这似乎有效:

TabularTables = {};
local =  new Meteor.Collection();
var data = MyTable.findOne();
if (data) {
    local.find().forEach(function(x) { local.remove(x._id) });
    for (var i in data.input) {
        local.insert({
            input: data.input[i],
            output: data.output[i],
            date: data.date[i]
        });
    }
}
TabularTables.MyTable = new Tabular.Table({
    name: "MyTable",
    collection: local,
    columns: [
        {data: "input", title: "Input", searchable: false},
        {data: "output", title: "Output", searchable: false},
        {data: "date", title: "Date", searchable: false}
    ],
    order: [[1, "desc"]],
    pageLength: 10
});

请注意,这可能不再是被动的。但我假设这些大数组中的数据也不会改变,否则你可能会改变你的模式,以便一开始就与 meteor 更兼容。所以希望这不是问题。

由于 TabularTables 不允许数组,您可以尝试使用 aslagle:reactive-table Meteor 包。软件包的 git 页面上的示例显示了如何在数组上使用 mongo 的语法。