pro*_*ons 29 javascript ajax jquery ruby-on-rails twitter-bootstrap
我正在构建一个Rails应用程序,我想通过AJAX将Rails部分内容放入模态中.
在Twitter Bootstrap 2.3.2模式中,我通过文档阅读您可以使用远程密钥通过ajax加载内容.
http://getbootstrap.com/2.3.2/javascript.html#modals
但是,这只允许将内容注入到内容中.modal-body,而不是动态地构建整个模态.
有没有建立整个模式的方式,其中包括.modal-header,.modal-footer,动态与JS?
使用字符串执行此操作似乎非常笨重,如下所示:
partial = render_to_string(:partial => 'some-partial').gsub(%{"}, %{'}).gsub(/'/,"\\\\'").gsub("\n", "")
Run Code Online (Sandbox Code Playgroud)
Amn*_*non 34
更新:
由于张贴这,我找到了一个优雅的引导模式3包装函数在这里,不需要添加一个div的HTML代码.
这是一个演示这个的代码示例.要使用,只需在<body>中添加一个div(在bootstrap的<div class ="container">中,例如:
<div id="idMyModal"></div>
Run Code Online (Sandbox Code Playgroud)
然后你可以通过以下方式使用它:
var header = "This is my dynamic header";
var content = "This is my dynamic content";
var strSubmitFunc = "applyButtonFunc()";
var btnText = "Just do it!";
doModal('idMyModal', header, content, strSubmitFunc, btnText);
Run Code Online (Sandbox Code Playgroud)
要关闭模态,请调用hideModal,也定义如下:
function doModal(placementId, heading, formContent, strSubmitFunc, btnText)
{
var html = '<div id="modalWindow" class="modal hide fade in" style="display:none;">';
html += '<div class="modal-header">';
html += '<a class="close" data-dismiss="modal">×</a>';
html += '<h4>'+heading+'</h4>'
html += '</div>';
html += '<div class="modal-body">';
html += '<p>';
html += formContent;
html += '</div>';
html += '<div class="modal-footer">';
if (btnText!='') {
html += '<span class="btn btn-success"';
html += ' onClick="'+strSubmitFunc+'">'+btnText;
html += '</span>';
}
html += '<span class="btn" data-dismiss="modal">';
html += 'Close';
html += '</span>'; // close button
html += '</div>'; // footer
html += '</div>'; // modalWindow
$("#"+placementId).html(html);
$("#modalWindow").modal();
}
function hideModal()
{
// Using a very general selector - this is because $('#modalDiv').hide
// will remove the modal window but not the mask
$('.modal.in').modal('hide');
}
Run Code Online (Sandbox Code Playgroud)
Cra*_*ger 27
更新
我最近偶然发现了bootbox.js,这是一个专门用于动态创建引导模式并响应用户与它们交互的整个库.虽然与下面的方法不同,但bootbox接受回调而不是函数名称.我还没有亲自使用它,因为我无法证明26kb库基本上可以完成下面的功能.但我认为有人可能会发现它很有用.
2016年8月17日更新
我现在使用bootbox几乎每个我需要动态模态的项目.工作得很好我强烈推荐它.
更新10/1/2018
Bootbox还没有正式支持bootstrap 4,但是有一个bootbox v5.x分支,他们正在使用bootstrap 4支持.根据5.0.0路线图和 Bootbox 5.0发货清单票据,它听起来像分支已经准备好了,但他们还没有发布它.但是有一些关于如何使用它的说明.免责声明:我还没有习惯v5.x分支,不能保证其完整性.
原帖
代码取自Ammon上面的回答.更新bootstrap 3.0
function doModal(placementId, heading, formContent, strSubmitFunc, btnText)
{
html = '<div id="modalWindow" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirm-modal" aria-hidden="true">';
html += '<div class="modal-dialog">';
html += '<div class="modal-content">';
html += '<div class="modal-header">';
html += '<a class="close" data-dismiss="modal">×</a>';
html += '<h4>'+heading+'</h4>'
html += '</div>';
html += '<div class="modal-body">';
html += formContent;
html += '</div>';
html += '<div class="modal-footer">';
if (btnText!='') {
html += '<span class="btn btn-success"';
html += ' onClick="'+strSubmitFunc+'">'+btnText;
html += '</span>';
}
html += '<span class="btn" data-dismiss="modal">';
html += <?php echo "'".__t("Close")."'"; ?>;
html += '</span>'; // close button
html += '</div>'; // footer
html += '</div>'; // content
html += '</div>'; // dialog
html += '</div>'; // modalWindow
$("#"+placementId).html(html);
$("#modalWindow").modal();
$("#dynamicModal").modal('show');
}
Run Code Online (Sandbox Code Playgroud)
这就是我最终用于满足我的需求.还包括一个事件处理程序,用于在关闭时从DOM中删除模态.我只需要一个信息模式,所以我拿出了提交函数和按钮文本参数.
function doModal(heading, formContent) {
html = '<div id="dynamicModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirm-modal" aria-hidden="true">';
html += '<div class="modal-dialog">';
html += '<div class="modal-content">';
html += '<div class="modal-header">';
html += '<a class="close" data-dismiss="modal">×</a>';
html += '<h4>'+heading+'</h4>'
html += '</div>';
html += '<div class="modal-body">';
html += formContent;
html += '</div>';
html += '<div class="modal-footer">';
html += '<span class="btn btn-primary" data-dismiss="modal">Close</span>';
html += '</div>'; // content
html += '</div>'; // dialog
html += '</div>'; // footer
html += '</div>'; // modalWindow
$('body').append(html);
$("#dynamicModal").modal();
$("#dynamicModal").modal('show');
$('#dynamicModal').on('hidden.bs.modal', function (e) {
$(this).remove();
});
}
Run Code Online (Sandbox Code Playgroud)
使用DOM,我创建了Button以及单击Button时弹出的Bootstrap模式.
还包括HTML页面的head部分:
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script
src= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
Run Code Online (Sandbox Code Playgroud)
整个代码需要用JS文件编写.
//首先,创建一个按钮,在单击时显示Bootstrap模态
var button = document.createElement("input");
button.className = 'btn btn-info btn-lg';
button.setAttribute("type", "button");
button.setAttribute("data-toggle", "modal");
button.setAttribute("data-target", "#myModal");
button.setAttribute("value", "More Information...");
document.getElementsByTagName('body')[0].appendChild(button);
Run Code Online (Sandbox Code Playgroud)
//莫代尔创作:
var div1 = document.createElement('div');
div1.id = 'myModal';
div1.className = 'modal fade';
div1.setAttribute("role", "dialog");
var innerDiv1m = document.createElement('div');
innerDiv1m.className = 'modal-dialog modal-sm';
div1.appendChild(innerDiv1m);
var innerDiv2m = document.createElement('div');
innerDiv2m.className = 'modal-content';
innerDiv1m.appendChild(innerDiv2m);
var innerDiv3 = document.createElement('div');
innerDiv3.className = 'modal-header';
innerDiv2m.appendChild(innerDiv3);
var buttonM = document.createElement("button");
buttonM.className = 'close';
buttonM.setAttribute("data-dismiss", "modal");
buttonM.setAttribute("aria-hidden", "true");
buttonM.setAttribute("value", "Close");
innerDiv3.appendChild(buttonM);
var headerM = document.createElement("H4");
headerM.className = 'modal-title';
innerDiv3.appendChild(headerM);
var innerDiv31 = document.createElement('div');
innerDiv31.className = 'modal-body';
innerDiv2m.appendChild(innerDiv31);
var para = document.createElement('p');
innerDiv31.appendChild(para);
para.innerHTML = "paragraph";
var innerDiv32 = document.createElement('div');
innerDiv32.className = 'modal-footer';
innerDiv2m.appendChild(innerDiv32);
var closeButton = document.createElement("input");
closeButton.className = 'btn btn-default';
closeButton.setAttribute("data-dismiss", "modal");
closeButton.setAttribute("type", "button");
closeButton.setAttribute("value", "Close");
innerDiv32.appendChild(closeButton);
document.getElementsByTagName('body')[0].appendChild(div1);
Run Code Online (Sandbox Code Playgroud)
//因此,单击创建的按钮时,模板会在屏幕上弹出.
| 归档时间: |
|
| 查看次数: |
58138 次 |
| 最近记录: |