gpt4 book ai didi

javascript - 带有模态窗口的 Sinatra/Padrino partials

转载 作者:行者123 更新时间:2023-11-30 18:09:11 25 4
gpt4 key购买 nike

我已经进行了详尽的搜索,但找不到这个问题的答案。

我正在使用 padrino 编写一个小应用程序,我有 2 个 View ,人物和事件。我有这些的 View 和 Controller ,它们工作正常。

我的问题是,从“事件” View ,我想打开一个模式窗口来添加新人。

模态窗口将加载“人”的相关字段。填满字段并单击按钮后,模态窗口将消失,新的“人”将被保存,用户将返回到事件页面。

谁能给我建议如何进行?谢谢。

最佳答案

模态窗口通常使用 javascript 完成。例如,使用 jQuery UI (来自the docs)

<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Dialog - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#dialog" ).dialog();
});
</script>
</head>
<body>

<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>


</body>
</html>

Sinatra/Padrino 将负责布局和 View ,您只需添加脚本部分。假设您使用的是 Haml ,您的 View 将如下所示:

#dialog{title: "Basic dialog"}
%p
This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.

:javascript
$(function() {
$( "#dialog" ).dialog();
});

javascript 不需要像 jQuery 文档所建议的那样进入 head 标签。


编辑:

将局部加载到窗口中有几种选择。一种是使用像 Handlebars 这样的 javascript 库,但我更喜欢让 javascript 从路由加载它,这样所有 View 代码都保持在一起,例如

get "/templates/modal1/?" do
haml :modal1, :layout => !xhr?
end

在 javascript 中,make an AJAX call到那条路线,你只会得到 HTML sans 布局,然后用它来 fill the box :

$.get('ajax/test.html', function(data) {
$('#dialog').html(data);
}

在您看来,只需描述 div:

#dialog1
-# The HTML from the view will be put here by the jQuery code.

类似的东西。

关于javascript - 带有模态窗口的 Sinatra/Padrino partials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15062202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com