有没有人有使用Meteor和Handsontable创建响应式表格以读取和更新数据的工作示例?
非常感谢您的帮助。
有没有人有使用Meteor和Handsontable创建响应式表格以读取和更新数据的工作示例?
非常感谢您的帮助。
Open handsontable.full.js and change the following line:
// Remove "var" from Handsontable declaration to add to global scope
var Handsontable = function (rootElement, userSettings) {
...
// New code
Handsontable = function (rootElement, userSettings) {
...
You may need to uninstall any existing Handsontable smartpackages
<template name="handsontable">
<div class="handsontable" id="hot"></div>
</template>
最后,在你的js文件中:
Meteor.isClient {
Template.handsontable.rendered = function () {
var myData = []; // Need this to create instance
var container = document.getElementById("hot");
var hot = new Handsontable(container,{ // Create Handsontable instance
data: myData,
startRows: 5,
startCols: 5,
colHeaders: true,
minSpareRows: 1,
contextMenu: true,
afterChange: function (change, source) { // "change" is an array of arrays.
if (source !== "loadData") { // Don't need to run this when data is loaded
for (i = 0; i < change.length; i++) { // For each change, get the change info and update the record
var rowNum = change[i][0]; // Which row it appears on Handsontable
var row = myData[rowNum]; // Now we have the whole row of data, including _id
var key = change[i][1]; // Handsontable docs calls this "prop"
var oldVal = change[i][2];
var newVal = change[i][3];
var setModifier = {$set: {}}; // Need to build $set object
setModifier.$set[key] = newVal; // So that we can assign 'key' dynamically using bracket notation of JavaScript object
MyCollection.update(row._id,setModifier);
}
}
}
});
Tracker.autorun( function () { // Tracker function for reactivity
myData = MyCollection.find().fetch(); // Tie in our data
hot.loadData(myData);
});
};
}
在这里可以找到有关afterChange API的文档:https://github.com/handsontable/handsontable/wiki/Events
以下代码将自动将您的集合字段呈现为列,包括一个_id列。为了使用Handsontable定义Meteor中的列,这里是样本集合Books中的示例文档:
{_id: 'Hneb7LxFRptXCiL49',
title: 'The Name of the Wind',
author: 'Patrick Rothfuss',
copies: 3 }
// replace at "colHeaders":
colHeaders: ['Title','Author','Copies'],
columns: [
{data: 'title'},
{data: 'author:},
{data: 'copies'}
],
// ...
columns:[{data:1},{data:2},{data:3}]
,但是整个表格都没有呈现。当我使用属性单元格时,出现相同的问题。有什么建议。提前致谢。 - Lobosan