2016-11-30 36 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