【问题标题】:Kendo Grid Foreign Key template剑道网格外键模板
【发布时间】:2013-08-22 23:17:27
【问题描述】:

我的剑道网格中有一个外键,我为这个外键创建了一个编辑器。保存工作正常,但问题是当网格显示数据时,外键值未定义。我知道我必须更改模板以显示正确的值。我添加了函数intemplate 来显示正确的值,但它对我不起作用。

你能帮帮我吗?这是我的代码:

var collection;

$("#mygrid").kendoGrid({
    dataSource: dataSource,
    pageable: true,  
    toolbar: [{ name: 'create', text: 'My create' }],
    columns: [
        { field: "ForeignKeyColumn", editor: categoryDropDownEditor, template: "#=getTextByValue(data)#" },
        { field: "NOTES", title: "Notes" },
        { command: ["edit", "destroy"], title: " ", width: "160px" }],
    editable: "popup",                    
});

//update model when choose value from dropdown list
var grid = $("#mygrid").data("kendoGrid");
grid.bind("save", grid_save);
function grid_save(e) {

    if (e.model.ForeignKey) {

        //change the model value
        e.model.ForeignKeyColumn = 0;
        //get the currently selected value from the DDL
        var currentlySelectedValue = $(e.container.find("#typeCombo")[0]).data().kendoDropDownList.value();
        //set the value to the model
        e.model.set('ForeignKeyColumn', currentlySelectedValue);            
    }
}

//Editor template
function categoryDropDownEditor(container, options) {
    $('<input id="typeCombo" required data-text-field="text" data-value-field="value" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: true,
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        url: '@Url.Action("SomeAction", "SomeController")',
                        type: "POST",
                        contentType: "application/json",
                    }
                }
            }
        });
}

//Show template
function getTextByValue(data) {
    //if the collection is empty - get it from the grid
    if (!collection) {
        grid = $("#GridName").data("kendoGrid");
         valuesCollection = grid.options.columns[1].values;//Set the correct FKColumn index
        collection = {};           
        for (var value in valuesCollection) {
            collection[valuesCollection[value].value] = valuesCollection[value].text;
        }
    }
    return collection[data.ForeignKeyColumn];
}

注意:valuesCollection 跟踪时未定义值。

【问题讨论】:

  • 您是否尝试在 EditorTemplates 中创建“GridForeignKey”?
  • 不,有什么有用的链接吗?

标签: asp.net-mvc-4 kendo-grid


【解决方案1】:

试试这个,

我认为在网格中绑定外键列是必要的。请不要更改 .chtml 视图名称和 Forlder 名称。

存放在以下位置。 位置:- YourViews -> Shred -> EditorTemplates -> GridForeignKey.cshtml

GridForeignKey.cshtml
@(
 Html.Kendo().DropDownListFor(m => m)
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
             .OptionLabel("--Not Category--")      
)

【讨论】:

  • 如果您已将选项标签传递到外键定义中,则此处不需要它。谢谢你。我几乎整天都在为此苦苦挣扎。
  • 如果我想要一个非通用的选项标签怎么办?如何将它传递给 GridForeignKey?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多