2013-04-04 133 views
0

如何將自定義按鈕綁定到JavaScript函數?我看到網格中每一行上的按鈕,但單擊這些按鈕會使用當前URL附加的「/ 0」刷新頁面。它不調用JavaScript函數「RedirectCreateReport」。這裏是我的劍道:Kendo Grid自定義按鈕調用Javascript函數

   @(Html.Kendo().Grid(Model) 
        .Name("MyGrid") 
        .Columns(columns => 
           { 
            columns.Bound(p => p.Name).Title("Name"); 
            columns.Bound(p => p.Description).Title("Description"); 
            columns.Command(command => 
             { 
              command.Custom("CreateReport").Text("Submit Report").Click("RedirectCreateReport"); 
             }).Title("Actions"); 
           }) 
        ... 
        .ColumnMenu() 
        .Groupable()) 

@section scripts { 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $(".RedirectCreateReport").click(function() { 
       alert("a"); 
       var item = $("#MyGrid").data("kendoGrid").dataItem($(this).closest("tr")); 
       window.location.href = "/Report/Create?P=" + item; 
      }); 
     }); 
    </script> 
} 
+0

您的示例代碼令人困惑,因爲您沒有名爲'RedirectCreateReport'的JavaScript函數,並且針對類爲'.RedirectCreateReport'的元素執行jQuery選擇器,但是您顯示的代碼中沒有任何內容將該類分配給任何內容。 – CodingWithSpike 2013-04-04 18:06:36

+0

我已經將jQuery更改爲標準的JavaScript函數,但它仍然表現如下: function RedirectCreateReport()alert(「a」);數據項($(this).closest(「tr」)); var data = $(「#MyGrid」)。data(「kendoGrid」)。 window.location.href =「/ Report/Create?P =」+ item; } – 2013-04-04 22:40:34

+0

解決方案是否使用JavaScript或jQuery並不重要。我只需要點擊Kendo Grid行中的一個按鈕,並將其重定向到另一個頁面,將Item Id從引用頁面傳遞到目標頁面。 – 2013-04-05 19:10:43

回答

1

我找到了我的問題的答案:溝自定義按鈕。使用ActionLink的,風格作爲一個按鈕來代替:

columns.Template(
    p => @Html.ActionLink(
    "New Report", 
    "Create", 
    "Report", 
    new { ID = p.Id.ToString() }, 
    htmlAttributes: 
    new { @class = "k-button k-button-icontext k-grid-NewReport" } 
    ).ToHtmlString() 
    ); 

此代碼替換線columns.Command(命令=> ...)標題( 「操作」);。 沒有必要的JavaScript。