2016-11-30 33 views
0

我需要在剑道网格的标题栏中有一个按钮。该按钮需要能够调用网格对象中的函数(GetFoo)。Kendo网格带标题访问数据的按钮

UPDATE:对不起任何混乱,但我只想对表头行与文本“名”,“姓”一个键等等......所以我们不得不


[th] |名字|姓氏|标题|按钮(调用getFoo())


[td] | Joe | Schmo | None |

[td] | Joe | Bob | None |

[结束更新]

这里是剑道UI一些修改后的代码

$(document).ready(function() { 
    var grid = $("#grid").kendoGrid({ 
    dataSource: { 
     pageSize: 20, 
     data: createRandomData(50) 
    }, 
    getFoo:function(){ 
     alert('bar'); 
    }, 
    pageable: true, 
    height: 550, 
    columns: [ 
     { field: "FirstName", title: "First Name", width: "140px" }, 
     { field: "LastName", title: "Last Name", width: "140px" }, 
     { field: "Title" }, 
     { field: '',title: "<button onClick='getFoo()' value='foo'>sdf</button>" }] 
    }).data("kendoGrid"); 
}); 

任何帮助表示赞赏!

+0

我想你应该使用网格的dataBound事件将按钮插入到标题中。 –

+0

没有真正使用过dataBound。如果你能提供一个关于如何解决这个问题的例子,我会看看。 – user3473534

回答

0

您可以使用数据绑定事件并像这样插入按钮:

$(document).ready(function() { 
var grid = $("#grid").kendoGrid({ 
dataSource: { 
    pageSize: 20, 
    data: createRandomData(50) 
}, 
getFoo:function(){ 
    alert('bar'); 
}, 
pageable: true, 
height: 550, 
dataBound: grid_dataBound, 
columns: [ 
    { field: "FirstName", title: "First Name", width: "140px" }, 
    { field: "LastName", title: "Last Name", width: "140px" }, 
    { field: "Title" }, 
    { field: '',title: "<button onClick='getFoo()' value='foo'>sdf</button>" }] 
}).data("kendoGrid"); 
}); 

function grid_dataBound(e) { 
var grid = this; 

var lastHeaderCell = grid.thead.find("th").last(); 
var button = $("<button value='foo'>sdf</button>"); 
lastHeaderCell.html(button); 
$(button).click(function(){ 
    grid.options.getFoo(); 
}); 
} 
+0

尝试使用你的代码在这里:https://jsfiddle.net/ffa1fqo8/ 它给错误grid.getFoo不是一个函数 – user3473534

+0

对不起。我已经更新了我的答案。 –

+0

工作,谢谢! – user3473534

0

有几种方法可以在Kendo网格中添加自定义按钮。一个是将其添加为工具栏项目。你可以阅读更多关于这里执行Kendo custom command button in toolbars

.ToolBar(toolBar => toolBar.Template("<a class='k-button k-button-icontext' onclick='customCommand()' href='#'></span>Cutom Command</a>")) 

接下来将有每行一个行内。你可以阅读如何实现你要注意的是一个在这里Kendo grid custom commands

但代码:

$(document).ready(function() { 
       var grid = $("#grid").kendoGrid({ 
        dataSource: { 
         pageSize: 20, 
         data: createRandomData(50) 
        }, 
        pageable: true, 
        height: 550, 
        columns: [ 
         { field: "FirstName", title: "First Name", width: "140px" }, 
         { field: "LastName", title: "Last Name", width: "140px" }, 
         { field: "Title" }, 
         { command: { text: "View Details", click: showDetails }, title: " ", width: "180px" }] 
       }).data("kendoGrid"); 

       wnd = $("#details") 
        .kendoWindow({ 
         title: "Customer Details", 
         modal: true, 
         visible: false, 
         resizable: false, 
         width: 300 
        }).data("kendoWindow"); 

       detailsTemplate = kendo.template($("#template").html()); 
      }); 

更何况这条线:

{ command: { text: "View Details", click: showDetails } 

您也可以使用模板定制剑道网格。看看这个链接阅读更多:Toolbar using templates

希望这会有所帮助,快乐的编码!

+0

感谢您的回复,但我只希望在第一个表格标题行中有1个按钮,而不是在每个单独的行中。 – user3473534

+0

上述解决方案的第一部分是网格上的单个按钮,您可以通过单击链接[DEMO](http://demos.telerik.com/aspnet-mvc/grid/toolbar-模板)。在那个例子中,他们正在使用下拉菜单,但是您可以使用按钮。 –

0

我通常使用Kendo模板来实现这一点。

在JavaScript

改变该行:

{ field: " " title: " ", template: kendo.template($("#button-template").html()) } 

而在你的HTML标记添加:

<script id="button-template" type="text/x-kendo-template"> 
    <button type="button" onClick='getFoo()' value='foo'> 
     Button Action 
    </button> 
</script> 

或者,这里是你如何可以添加一个按钮将网格的标头:

创建剑道模板:

<script type="text/x-kendo-template" id="GridToolbar"> 
    <div class="toolbar"> 
     <button type="button" onClick='getFoo()' value='foo'>Button Action</button> 
    </div> 
</script> 

在JS设置该属性上的剑道格:

toolbar: kendo.template($("#GridToolbar").html()), 
+0

感谢您的回复,但我只希望在第一个表格标题行中有1个按钮,而不是在每个单独的行中。 – user3473534

+0

更新了,我的回答。如果我正确理解你,我的帖子中的第二个选项应该会对你有所帮助,只要你试图给网格标题添加一个按钮(一个工具栏)即可。如果你想在网格中的第一个实际行中有一个按钮,而不是其他人,那么这个我仍然不适合你。 – Kyle

+0

工具栏选项关闭。我希望它在标题中,但我希望它在与“名”,“姓”,“标题”相同的行上 当我使用工具栏选项时,它将按钮放在“名字”之上等。 .. – user3473534

0

我认为你可以使用HeaderTemplate中这个 检查该工作示例

dojo example

$("#grid").kendoGrid({ 
columns: [{ 
    field: "name", 
}, { 
    field: "FirstName", 
    title: "First Name", 
    width: "140px" 
}, { 
    field: "LastName", 
    title: "Last Name", 
    width: "140px" 
}, { 
    field: "Title", 
    headerTemplate: 'Title <button id="foo" onclick="foo()">foo</button>' 
}], 
dataSource: [{ 
    name: "Jane Doe" 
}, { 
    name: "John Doe" 
}]}); 
+0

我知道我可以将它添加为html,但然后函数“foo()”必须在网格外。可以做到的是,我希望将它全部保留在网格中,因为这是更大的应用程序的一部分。 – user3473534