使用 Kendo 菜单打开多个 windows

Opening multiple windows with Kendo menu

我有一个 kendo 菜单,我希望每个菜单都打开一个新的 window。我怎样才能做到这一点?

这是我在 _layout 中的当前代码:

<div class="k-rtl">
@(Html.Kendo().Menu()
    .Name("menu")
    .Items(items =>
    {
        items.Add().Text("Menu 1").Items(child =>
        {
            child.Add().Text("1").LinkHtmlAttributes(new { onClick = "menu('1');" });
            child.Add().Text("2");
        });
    })
)
</div>
<script>
function menu(text) {
    var window = $("#win1").data("kendoWindow");
    switch (text) {
        case "1":
            window.refresh({ url: "@Url.Action("Index", "1")" }).title("1");
            break;
        case "2":
            window.refresh({ url: "@Url.Action("Index", "2")" }).title("2");
            break;
    }
    window.open();
}
</script>

然后我在我的索引中创建了这个默认 window:

@(Html.Kendo().Window()
    .Name("win1")
    .Title("default")
    .LoadContentFrom("Index", "default")
    .Draggable()
    .Resizable()
    .Actions(actions => actions.Close().Minimize().Refresh())
    .Position(p => p.Top(100))
)

这段代码有两个问题:

  1. 我想要多个windows。
  2. Window 的刷新按钮加载上一页的旧内容。

要拥有多个 windows,您可以创建一个局部视图,然后将其注入 HTML 代码 (@Html.Partial("MyGenericWindow")),确保生成新的 window ID(姓名)每次。

像这样:

@{
    var windowId = Guid.NewGuid().ToString();
}

@(Html.Kendo().Window()
    .Name(windowId )
    .Draggable()
    .Resizable()
    .Actions(actions => actions.Close().Minimize().Refresh())
    .Position(p => p.Top(100))
)

要解决刷新问题,试试这个:

function menu(text) {
    var window = $("#@windowId").data("kendoWindow");
    window.title(text);
    window.refresh({
        url: '@Url.Action("Index")',
        data: { myParam: text }
    });

    window.bind("refresh", function () {
        window.center();
        window.open();
    });
}

嗯,这是我的最终解决方案。 :)

_Layout 中的 JS 脚本:

<script>
function menu(text) {
  $('.body-content').load('/Home/win?window='.concat(text));
}
</script>

Home控制器的win动作:

public ActionResult win(string window)
{
    WindowViewModel model = new WindowViewModel();
    model.Name = window;
    switch (window)
    {
        case "1":
        default:
            model.Title = "1";
            model.Url = Url.Action("Index", "1");
            break;
        case "2":
            model.Title = "2";
            model.Url = Url.Action("Index", "2");
            break;
    }
    return PartialView("GenericWindow", model);
}

我的 GenericWindow 局部视图:

@model WindowViewModel

@(Html.Kendo().Window()
    .Name(Model.Name)
    .Draggable()
    .Actions(actions => actions.Close().Minimize().Refresh()).LoadContentFrom(Model.Url)
    .Title(Model.Title)
)

WindowViewModel

public class WindowViewModel
{
    public string Title { get; set; }
    public string Url { get; set; }
    public string Name { get; set; }
}