You’ll need a:
<th>Item name</th>
after the <tr>
and before the first {{#each ...
And you’ll need:
<td>{{name}}</td>
{{#each checkboxes}}<td><input type="checkbox" checked={{this}}></td>{{/each}}
instead of:
<td>{{this}}</td>
<td><input type="checkbox" value={{this}}></td>
… if you’re trying to do what I think you’re trying to do.
Then, as long as listParams
and dataItems
are helpers that both return data from the same reactive data source, you should be okay.
The helpers would be something like:
Template.my_table.helpers({
listParams: function () {
var doc = ParamsForTables.findOne({_id: Session.get('currentTableId')});
return doc.listParams.sort(); // assuming `listParams` is an array of strings, which are the headings of your columns
},
dataItems: function () {
var doc = ParamsForTables.findOne({_id: Session.get('currentTableId')});
var columns = doc.listParams.sort(); // note that the columns for the data items are sorted the same way as the headings
var dataItems = DataItems.find({table_id: Session.get('currentTableId')});
return _.map(dataItems, function (dataItem) {
// create a temporary field called `checkboxes` for each row with a set of Booleans that match the checked state of the columns
// assuming here that for each row (data item document) there is a nested object called `checkedColumns` that maps the column name to a Boolean
var checkboxes = _.map(columns, function (column) {
return {
column: column,
checked: dataItem.checkedColumns[column],
dataItemId: dataItem._id // this just makes it easier when capturing the event and making changes
}
});
// extend the data item document with the temporary field and return it
return _.extend(dataItem, {checkboxes: checkboxes};
});
}
});
You’d then need to capture the change events on the checkboxes and modify the particular document from the dataItems collection.
Template.my_table.events({
'change input[type=checkbox]' : function () {
var modifier = {};
modifier['checkedColumns.' + this.column] = !this.checked;
DataItems.update({_id: this.dataItemId}, {$set: modifier});
}
});
This is an inelegant, inefficient solution, based on a whole lot of assumptions I’ve made about your data model, but it should work. (It’s all off the top of my head, so no guarantees.)
Just saw @herteby’s solution pop up. Much more elegant and understandable. I should have gone with something like that.