Dynamic columns from knockout observable array not working
Dynamic columns from knockout observable array not working
I have some dynamic columns which are coming from a dynamic Knockout observableArray.
The object definition is:
// dynamically-generated list of all the valid permission names
var allPermissions = ["Superuser","Client","User"];
ViewModel = function() {
var self = this;
self.members = ko.observableArray([]); // array of Member objects to populate datatable
}
Member = function() {
var self = this;
self.name = ko.observable();
self.email = ko.observable();
self.permissions = ko.observableArray([]); // array of Permission objects
}
Permission = function() {
var self = this;
self.name = ko.observable();
self.isChecked = ko.observable();
}
The data is generated and then the DataTable is initialised:
//set up the basic columns for the member
var columns = [
{ data: 'name()', title: "Name" },
{ data: 'email()', title: "Email" }
];
for(var i=0; i< allPermissions.length; i++)
{
columns.push({ data: 'permissions()' + i + '.isChecked()', title: allPermissions[i]});
}
var dt = $('#my-table').DataTable({
columns: columns
});
If I comment-out the dynamic columns, it works fine, but when I include it, I get an error being thrown from the datatable js at line 1209 because a[i] is "permissions()[0]" and it can't resolve this as a property of data.
else if ( funcNotation )
{
// Function call
a[i] = a[i].replace(__reFn, '');
data = data a[i] ;
continue;
}
How would I go about achieving this?
So, to summarise, the list of permissions comes from the database. The Member has an entry in self.permissions for each of these, with a flag of isChecked.