javascript 在剑道网格中获取点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19304683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting the click event in a kendo grid
提问by Crystal
I'm trying to get the click event for a Kendo Grid so I can bind stuff to shift and ctrl clicking. I can't use the inherent multiselect Kendo provides because it doesn't support drag and drop. When I create a function after the dataBound event, my function gets called on clicking, but it's not the typical click event.
我正在尝试获取 Kendo Grid 的点击事件,以便我可以将内容绑定到 shift 和 ctrl 点击。我无法使用 Kendo 提供的固有多选,因为它不支持拖放。当我在 dataBound 事件之后创建一个函数时,我的函数在点击时被调用,但这不是典型的点击事件。
var onDataBound = function () {
selectItem.apply(this);
}
grid.dataBound = onDataBound;
var selectItem.apply = function (e) {
console.log(e);
}
Any thoughts? Thanks in advance.
有什么想法吗?提前致谢。
回答by OnaBai
After initializing the Gridyou should bind a handler to the click event.
初始化之后,Grid您应该将处理程序绑定到单击事件。
Example:
例子:
$("#grid").on("click", "table", function(e) {
console.log("clicked", e.ctrlKey, e.altKey, e.shiftKey);
});
You can use:
您可以使用:
e.ctrlKeyfor detecting if ctrlis pressed.e.altKeyfor detecting if altis pressed.e.shiftKeyfor detecting if shiftis pressed.
e.ctrlKey用于检测是否ctrl被按下。e.altKey用于检测是否alt被按下。e.shiftKey用于检测是否shift被按下。
If you want to detect click only in the body of the table, you can replace "table"by "tbody"or even "td".
如果您只想检测表格正文中的点击,您可以替换"table"为"tbody"甚至"td"。
回答by Mahmoodvcs
Use dataBoundevent when declaring the grid:
dataBound声明网格时使用事件:
grid = $("#grid").kendoGrid({
...
,dataBound=onDataBound
});
var onDataBound = function(e)
{
$("#grid").find("tr").click(selectItem.apply);
};
var selectItem.apply = function (e) {
var dataItem = $("#grid").data("kendoGrid").dataItem(this);
if(e.ctrlKey)
alert('Ctrl + click on ' + dataItem.column1);
}
dataItemis your bound data item that you can pass around.
dataItem是您可以传递的绑定数据项。
回答by Jo?o Batista
I know this is quite old but I believe none of the solutions covered the fact that the event is being applied on the table headers as well. If you have table filters (order by ascending / descending) for example, this might cause problems. The correct way of adding the click event is targetting the table body, as so:
我知道这已经很老了,但我相信没有一个解决方案涵盖了该事件也被应用于表标题这一事实。例如,如果您有表格过滤器(按升序/降序排列),这可能会导致问题。添加点击事件的正确方法是针对表格主体,如下所示:
$("#grid tbody").find("tr").click(addYourFunctionHere);

