在JQuery或JavaScript中的Grid标头上生成onClick事件

时间:2014-03-11 09:56:01

标签: javascript jquery kendo-ui kendo-grid kendo-asp.net-mvc

我有这个KendoUI网格,我想在其标题行中添加一个click事件,并获取所单击单元格的文本值:

@(Html.Kendo()
      .Grid<Timeshet.Web.Models.UserWeekModel>()
      .Name("grid")
      .Sortable(sortable => sortable.AllowUnsort(false))
      .Columns(columns =>
      {
              columns.Bound(t => t.Name);//.ClientTemplate("#=Name#" + " " + "<div class='jobDescriptionInfo-img' onclick='showDescription()' title='Job Description' >&nbsp;</div>");
              columns.Bound(t => t.JobDescription).Visible(false);
              columns.Bound(t => t.LastFri).ClientTemplate("#=LastFri#").HtmlAttributes(new { @class = "last-week" }).Hidden().Sortable(false);
              columns.Bound(t => t.Monday).ClientTemplate("#if (Monday.HasNotes) {#" + "#=Monday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Monday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Monday.Style#" });
              columns.Bound(t => t.Tuesday).ClientTemplate("#if (Tuesday.HasNotes) {#" + "#=Tuesday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Tuesday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Tuesday.Style#" });
              columns.Bound(t => t.Wednesday).ClientTemplate("#if (Wednesday.HasNotes) {#" + "#=Wednesday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Wednesday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Wednesday.Style#" });
              columns.Bound(t => t.Thursday).ClientTemplate("#if (Thursday.HasNotes) {#" + "#=Thursday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Thursday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Thursday.Style#" });
              columns.Bound(t => t.Friday).ClientTemplate("#if (Friday.HasNotes) {#" + "#=Friday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Friday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Friday.Style#" });
              columns.Bound(t => t.Saturday).ClientTemplate("#if (Saturday.HasNotes) {#" + "#=Saturday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Saturday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Saturday.Style#" });
              columns.Bound(t => t.Sunday).ClientTemplate("#if (Sunday.HasNotes) {#" + "#=Sunday.Name#" + " " + "<div class='project-note-img' title='Project Notes' >&nbsp;</div>" + "#}else{#" + "#=Sunday.Name#" + "#}#").Sortable(false).HtmlAttributes(new { @Class = "#=Sunday.Style#" });
              columns.Command(command => command.Custom("Job Description").Click("showDescription"));
      })
      .Selectable(s => s.Enabled(true).Mode(GridSelectionMode.Single).Type(GridSelectionType.Cell))
                 .DataSource(source => source.Ajax()
                 .Batch(true)
                 .Model(model => model.Id("UserId"))
                 .Read(read => read.Action("GetData", "Home").Data("additionalData"))
                 .Update(update => update.Action("UpdateData", "Home").Data("additionalData")
                 ).Events(events => events.RequestEnd("onRequestEnd"))
                 ))

我曾尝试在document.ready上执行以下操作,但似乎无法正常工作:

var titles = $("#grid thead tr th");

for (var i = 0, len = titles.length; i < len; i++)
{
    titles[i].addEventListener("click", function() {
        alert(this.innerHTML);
    }, false);
}

我也找到了这个解决方案,这正是我想做的,但我无法真正重构它以供我自己使用,因为我不会放弃所有这些:jsFiddle

我想知道如何在网格标题上触发点击事件。

以上网格在浏览器中呈现为HTML格式:

enter image description here

2 个答案:

答案 0 :(得分:7)

试试这个jquery代码:

$(document).on("click", "th[role='columnheader']", function () {
    alert($(this).text());
});

答案 1 :(得分:2)

这是表头的CSS选择器,如果将其替换为Vinod的解决方案,它可以工作。所有kendo控件都使用某种形式的.k-whatever

.k-grid table th
{  
    text-align: center !important;      
}    


$(document).on("click", ".k-grid table th", function () {
    alert($(this).text());
});