function VM() {
const self = this;
self.debug = ko.observable('');
self.catalog = [
{
name:'Section1',
items:[
{id:1,name:'Item1'},
{id:2,name:'Item2'},
]
},
{
name:'Section2',
items:[
{id:4,name:'Item4'},
]
}
];
self.orderClick = function(arg1,arg2,arg3,arg4) {
let dbgText = "this:<br> ";
dbgText += JSON.stringify(this);
dbgText += "<br>1st arg:<br> ";
dbgText += JSON.stringify(arg1);
dbgText += "<br>2nd arg:<br> ";
dbgText += JSON.stringify(arg2);
dbgText += "<br>3nd arg:<br> ";
dbgText += JSON.stringify(arg3);
dbgText += "<br>4th arg:<br> ";
dbgText += JSON.stringify(arg4);
self.debug(dbgText);
}
}
ko.applyBindings(new VM())
table {
border-collapse: collapse;width:100%;
}
td {
border:1px solid #CCC;
text-align:center;
font-family:sans-serif;
font-size:11px;
white-space:nowrap;
}
button {
display:block; margin:5px auto;
}
.dbglog {
padding:5px 10px;
border:1px solid #AAA;
background-color:#EEE;
font-family:"Lucida Console", Monaco, monospace;
font-size:12px;min-height:50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table data-bind="foreach: catalog">
<tr>
<td colspan=10><b data-bind="text:$data.name"></b></td>
<td>
<table data-bind="foreach:$data.items" class=nested>
<tr>
<td width=10%>
<i data-bind="text:$data.name"></i>
</td>
<td width=30%>
no .bind()
<button data-bind="click:$root.orderClick">click</button>
</td>
<td width=30%>
.bind($parent)
<button data-bind="click:$root.orderClick.bind($parent)">click</button>
</td>
<td width=30%>
.bind($parent,$data,'smthelse')
<button data-bind="click:$root.orderClick.bind($parent,$data,'text')">click</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div class=dbglog data-bind="html:$root.debug"></div>