Kendo UI Grid获取当前元素javascript的id

时间:2014-12-31 18:21:06

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

即时通讯使用Kendo UI apsnet,我在第3列中使用Gird with autocomptele作为模板,我希望使用javascript函数" onAutoCompleteX"发送数据,在此函数中我想获取要发送的活动自动完成的ID作为参数的动作文本" GetArticle"但我的问题是如何得到这个id,尝试了很多方法总是得到" undefined"或错误

@using KendoUIMvcApplication2.Areas.Gescom.Models.Achat


<style>
    .k-widget .templateCell
    {
        overflow: visible;
    }
</style>
 <script> 
    function initMenus(e) {
        $(".templateCell").each(function () {
            eval($(this).children("script").last().html());
        });
    }

    function onAutoCompleteSelectX(e) {
        var dataItem = this.dataItem(e.item.index());
        var url = '@Url.Action("GetArticle", "Fiche")';

        $.ajax({
            url: url,
            data: { code: dataItem.Code }, //parameters go here in object literal form
            type: 'GET',
            datatype: 'json',
            success: function (data) {
                if (data == null)
                    document.getElementById('labelx').innerHTML = "null";
                else
                    document.getElementById('labelx').innerHTML = data.Code;
            },
            error: function () {
                document.getElementById('labelx').innerHTML = "error";
            }
        });
    }


     function onAutoCompleteX() {
         var currentId = $(this).attr('id');
         //var currentId = $(this).id;
         //document.getElementById('labelx').innerHTML = $(this).className; //$obj.attr('id');
         return {
             text: document.getElementById(currentId).value
             //text: $("#id_of_another_autocomplete").val() works fine when i set static id manually
         };
     }
</script>

<div class="lines-tab-doc">
    @(Html.Kendo().Grid<LineAppelOffre>()
        .Name("grid-lines-doc")

        // Declare grid column
        .Columns(columns =>
        {
            // Cretae all the columns base on Model
            columns.Bound(l => l.Document);
            columns.Bound(l => l.LigneOriginale);
            columns.Template(l => { }).Title(@Resources.Resource.Article)
                .HtmlAttributes(new { @class = "templateCell" })
                .ClientTemplate(
                    Html.Kendo().AutoComplete()
                    .Name("Article")
                    .HtmlAttributes(new { id = "#=LigneOriginale#", style = "width:100%;" })
                    .DataTextField("Code")
                    .Filter(FilterType.Contains)
                    .DataSource(source =>
                        {
                            source.Read(read =>
                                {
                                    read.Action("GetArticles", "Fiche").Data("onAutoCompleteX");
                                })
                            .ServerFiltering(true);
                        })
                    .Events(e => { e.Select("onAutoCompleteSelectX"); }).ToClientTemplate().ToHtmlString()
            );
            columns.Bound(l => l.Fournisseur);
            columns.Bound(l => l.RefArtFrs);

            // Edit and Delete button column
            columns.Command(command =>
            {
                command.Edit();
                command.Destroy();
            }).Width(200);
        })
        .Events(ev => ev.DataBound("initMenus"))
        // Declare ajax datasource.
        // CRUD operation are wired back to ASP MVC Controller/Action e.g. HomeController, GetAll
        // Set the model Id
        .DataSource(datasoure => datasoure.Ajax()
                                    .Model(model => 
                                        {
                                            //model.Id(l => l.Document);
                                            model.Id(l => l.LigneOriginale);
                                        })
                                    .Read(read => read.Action("LinesAppelOffre_Read", "Achat"))
                                    .Create(create => create.Action("LinesAppelOffre_Add", "Achat"))
                                    .Update(update => update.Action("LinesAppelOffre_Update", "Achat"))
                                    .Destroy(delete => delete.Action("LinesAppelOffre_Delete", "Achat"))
                                    .PageSize(10)
        )

        // Add tool bar with Create button
        .ToolBar(toolbar => toolbar.Create())

        // Set grid editable.
        .Editable(editable => editable.Mode(GridEditMode.InCell))

        .Scrollable(scr=>scr.Height(327))
        .Sortable()
        .Selectable()
        .Navigatable()
        .Pageable(pageable =>
                      {
                          pageable.Refresh(true);
                          pageable.PageSizes(true);
                          pageable.Messages(msg => msg.Empty(null));
                      })
    )
</div>

2 个答案:

答案 0 :(得分:0)

您可以像这样获取自动完成ID:

 function onAutoCompleteX(e) {
     var currentId = e.sender.element.attr('id');
     ...
 }

但是我不确定你是否将明确的名称设置为“文章”.Name("Article")你不会总是将“Artilcle”作为id,即使你使用.HtmlAttributes属性设置它。

如果是这样,只要尝试使用不同的属性,然后使用id和get就可以了。

答案 1 :(得分:0)

我使用了document.activeElement

浏览器兼容性

Chrome 2

Firefox(Gecko)3.0

Internet Explorer 4

Opera 9.6

Safari 4.0