如何将绑定到KoGrid单元格的项目传递到ViewModel

How Can I Pass The Item Bound To A KoGrid Cell To The ViewModel

本文关键字:项目 ViewModel KoGrid 绑定 单元格      更新时间:2023-09-26

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不是绑定到行的数据项,而是KoGrid列。如何获取绑定到行的数据项,以便将其传递给可观察数组上的remove函数?

我曾尝试将点击事件与jQuery和各种单元格模板连接起来,试图传入绑定到行的数据项,但没有成功。

默认情况下,当前数据上下文将传递给click处理程序,该处理程序是文档中描述的当前列对象:

$data:kg.列://列实体

您需要传入的是$parent.entity: //your data model,它是当前行实体。

因此,您需要更改绑定:

{ 
    field: 'orderCatalogUpdateID', 
    cellTemplate: '<button data-bind="click: ' + 
       ' function() { $userViewModel.removeItem($parent.entity); }">X</button>', 
    displayName: ' ', 
    width: '2%' 
}

演示JSFiddle。