【问题标题】:Display some html until final page is loaded在加载最后一页之前显示一些 html
【发布时间】:2013-04-09 01:13:37
【问题描述】:

我正在使用 Rails 发出搜索请求。

搜索表单的操作是某个路径 "/results/foo" .. 它会命中一个控制器,该控制器对输入进行一些验证,然后如果搜索文本是干净的,则重定向到 "results/foobar"。

<h1 class="center"><progress style="font-size: 160%;">Loading...</progress></h1>

我想显示上面的 html.. 并且只显示上面的 html(在视图中或通过 jquery),直到“results/foobar”完全加载。

有没有办法在最后一页完成加载之前显示这样的进度条动画 (html)?

更多详情(搜索栏页面有这样的代码):

<form class="form-search" action="/s_results/iscan" method="POST">
        <div class="input-append">
            <input name="searchtext" type="text" class="span4 search-query" placeholder="Type number here...">
            <button type="submit" class="btn">
                <i class="icon-search"></i>
                Search
            </button>
        </div>
    </form>

【问题讨论】:

  • 您所说的涉及 AJAX,实际上更像是一个 JS 问题,尽管您的后端需要用有用的数据进行响应。也许提供更多信息,说明您正在处理什么样的进展。
  • 嗨 @NathanLilienthal ,我只是想将上面的 html [Loading...] 显示为叠加层(高 z-index).. 或其他方法,直到我加载完最后一页。
  • 啊,我误会了,我以为你在寻找更新进度。
  • 还更新了添加“javascript”标签的问题。我可能很快就会对这个问题进行赏金!

标签: javascript jquery ruby-on-rails html angularjs


【解决方案1】:

在这种情况下,您所做的就是在后台加载数据时保持一个完整的窗口占据您的内容的 div http://jsfiddle.net/basarat/qfrJE/

.busyPanel {
    /* absolute position all over */
    position: absolute;
    top: 1px;
    bottom: 1px;
    left: 1px;
    right: 1px;
    /* Remove any defaults */
    border: none;
    margin: 0;
    padding: 0;
    /* Make sure it stays on top */
    z-index: 2500;
    /* any image */
    background: grey;
    /* initial visibility */
    display: visible;
}

然后在加载完成后将其删除:

$(".busyPanel").fadeOut('slow');

【讨论】:

  • 感谢您的回答!但是您正在使用 setTimeout(function(){ $(".busyPanel").fadeOut('slow'); },2000); ..如何翻译该行以模拟页面加载? $(window).load(function () {});发生得太晚了,所以#(document).ready..还有其他方法吗?
  • 通过 ajax 例如 $.get 加载数据并在 on success 函数中隐藏面板。
  • 很抱歉成为 AJAX 假人,但哪个视图文件(在 rails 中)将包含“$.get”javascript?搜索栏视图?还是搜索结果视图?..我的项目也具有搜索栏页面和搜索结果页面之间的中间控制器的复杂性..这个存在用于验证搜索输入并重定向到搜索结果页面..
  • 概述:在 Rails 中,您需要使用控制器中的 format.json 来返回 json,然后使用 jquery $.get 调用它并将其加载到 DOM 中。
  • 抱歉又打扰了..但是哪个控制器? searchBarController -> searchValidateController -> searchResultsController(呈现最终结果)...“-> = 重定向到”。
【解决方案2】:

您最好的选择是在单击显示您的div 的表单条目的链接时使用一些 JS,然后它会在您的请求处理时出现在屏幕上,一旦搜索完成并且您的新页面渲染它就消失了。

-- html
<div class="progress">Loading...</div>

-- css
.progress { display: none; }

-- js
$('#search').click(function() {
  $('.progress').show();
  window.location.href = 'foo';
});

JSFiddle,简单易行。

【讨论】:

  • 你的意思是我必须在前端用javascript而不是后端验证输入(确保它的数字)?
  • 不,我在说明如何在加载页面时显示加载 div。
  • 我试过这个..一旦我的控制器开始重定向到搜索结果页面,“正在加载...”文本就会消失。 (/results/foobar).. 但是,我想继续显示 div "while" /results/foobar 正在加载。
  • 这实际上是有效的,只是你需要使用javascript来做导航。我不确定你为什么会遇到这个问题。你能把你的一些代码发布到要点什么的吗?
  • 我有很多相互关联的文件。你会推荐哪个特定部分,哪个对要点最有意义?一个或两个控制器?还是我正在使用的 3 个视图中的一个或全部?
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 2014-04-03
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多