本文介绍了使用DataTables(Meteor Tabular)绘制新行中的数组的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实现 />数据表。我正在尝试从Mongo集合创建一个表。该集合有一个文件的形式

I am using the Meteor Tabular package which implements DataTables. I am trying to create a table from a Mongo collection. The collection has one document of the form

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

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

I define the table in Meteor with the following code

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个元素都将在单个单元格中,所以我有一大堆。我想要每个元素在一个单独的行中创建,即

The problem is that when this is drawn, all 365 elements of each variable end up in a single cell, so I have one massive row. I want each element to be created in a separate row, i.e.

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]


推荐答案

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

You will need to transform your data and then put it into a local collection, since that package doesn't accept arrays (in contrary to what I thought earlier).

工作:

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
});

请注意,这可能不再有反应。但是我假设你在这些大阵列中的数据不会改变,否则你可能会改变你的模式以便与流星更加兼容。所以希望这不是问题。

Note that this may no longer be reactive. But I'm assuming that your data in those big arrays is not going to change either, or else you would probably change your schema to be more compatible with meteor to begin with. So hopefully that's not a problem.

这篇关于使用DataTables(Meteor Tabular)绘制新行中的数组的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:16