HTML:
<div data-bind="koGrid: gridOptions" style="height:600px;border:solid 1px #ccc;"></div>
JS:
列定义:
{ field: 'orderCatalogUpdateID', cellTemplate: '<button data-bind="click: $userViewModel.removeItem">X</button>', displayName: ' ', width: '2%' }`
ViewModel 上的 removeItem 函数:
self.removeItem = function (item) {
self.list.remove(item);
}
传递给 removeItem
函数的 item
不是绑定(bind)到行的数据项,而是 KoGrid 列。如何获取绑定(bind)到该行的数据项,以便将其传递给可观察数组上的 remove
函数?
我曾尝试使用 jQuery 和各种单元格模板连接点击事件,试图传递绑定(bind)到行的数据项,但没有成功。
最佳答案
默认情况下,当前数据上下文被传递给 click
处理程序,它是 documentation 中描述的当前列对象。 :
$data: kg.Column: //the column entity
您需要传入的是$parent.entity://your data model
,即当前行实体。
所以你需要改变你的绑定(bind):
{
field: 'orderCatalogUpdateID',
cellTemplate: '<button data-bind="click: ' +
' function() { $userViewModel.removeItem($parent.entity); }">X</button>',
displayName: ' ',
width: '2%'
}
演示 JSFiddle .
关于javascript - 如何将绑定(bind)到 KoGrid 单元格的项目传递给 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18367929/