轨更新表3 AJAX的解决方案?解决方案、AJAX

2023-09-10 16:25:28 作者:浅月.

我知道这是一个热门的话题,但我一直在寻找了不少努力寻找答案,奇怪的是没有发现任何已经能够帮助...

我已经有了一个基本的Rails 3应用程序在一个表中显示的数据库条目。一个单独的应用程序发送新的条目到数据库中,和我想的Rails应用程序轮询和刷新,以显示新的内容。现在,我有一个可怕的一件简单的JavaScript重新加载整个页面和它的证明是我的障碍。我有一个搜索栏,并检查条删除多个项目,但这些都是在一个页面刷新两个复位(再次,我知道这是可怕的和打破的窘况)。

从我读过,我了解,我应该使用AJAX来查询数据库,并简单地渲染比显示最新的一项新的数据库条目。如何precisely执行,这是另一个故事...

大多数我发现已经过时了,我一直无法得到传统的code运行教程。我是新来的两个AJAX和Rails,我会爱,如果任何人有任何指导,咨询,辅导,或个人的批评(:P)。这可能指向我的方向是正确的。

谢谢!

编辑:我有被拉入页新条目,但它们不是正确的渲染在所有。出于某种原因,他们没有被添加为项目进入我的表。这是我的code,希望你们也许能挑出来的东西我错过: *注 - >请求,实际上是我的数据库条目的名称。要求为模型。

的意见/请求/ index.html.erb

 <表类=中心>
< D​​IV ID =请求>
 &其中; TR>
    &其中;第i;&其中;%=排序Patient_Name,患者姓名%>&所述; /第i个
    百分位><%=排序Room_Number,房间号%>< /第i个
    &其中;第i;&其中;%=排序请求%>&所述; /第i个
    百分位><%=排序紧急%>< /第i个
    百分位><%=排序伯爵,请求计数%>< /第i个
    &其中;第i;&其中;%=排序created_at,创建在%>&所述; /第i个
    百分位>< /第i个
  < / TR>
&其中;%=渲染@requests%GT;
< / DIV>
< /表>
 

请求部分:观点/请求/ _request.html.erb

 < D​​IV CLASS =请求的数据时=<%= request.created_at.to_i%>>
    &其中; TR>
        &其中; TD>&其中;%= request.Patient_Name%GT;&所述; / TD>
        &其中; TD>&其中;%= request.Room_Number%GT;&所述; / TD>
        &其中; TD>&其中;%= request.Request%GT;&所述; / TD>
        &其中; TD>&其中;%= request.Urgency%GT;&所述; / TD>
        &其中; TD>&其中;%= request.count%GT;&所述; / TD>
        &其中; TD>&其中;%&GT%= request.created_at.getlocal.strftime(关于%D%R);&其中; / TD>
        < TD><%=的link_to删除,请求:确认=> 你确定吗?,:方法=> :删除%>< / TD>
    < / TR>
< / DIV>
 

的意见/请求/ index.js.erb的

  $('#请求)追加(<%=原escape_javascript(渲染(@requests))%>中)。
 
Ajax 跨域,这应该是最全的解决方案了

解决方案

这是怎么回事?

http://www.datatables.net/

另外一整套的位置:

http://plugins.jquery.com/plugin-tags/ajax-table

有时,最好使用那里的东西,而不是建立新的。

但是,为了回答你的问题,它取决于你是否自己操纵DOM或者让原型,jQuery的,MooTools的,或任何做繁重。我们的想法是设置一个计时器,在JavaScript中,当计时器火灾,要求服务器进行更新。服务器 - 你的Rails应用程序 - 将打包一切发生了变化,它发回

第1步:确定一个框架。我使用jQuery但大多数人会做的。

步骤2:决定一数据格式。我使用JSON,但XML就行了。

第三步:设置定时循环。它涉及到使用SetTimer的(),对每一观察后重置它。

步骤4:在执行该计时器被激发时,包了最后出现时间戳和进行Ajax请求的处理程序。你怎么做到的变化从框架,框架 - 或者如果你是正确的编码对DOM,从浏览器到浏览器

第五步:设置一个成功的处理程序,这样,当您的服务器会返回零个或多个数据行,您可以在表中插入这些TR-TD- / TD- / TR行

第六步:更新任何内部的垃圾像数或行柜台或任何其他JavaScript是跟踪

请注意:您可能会发现有插件等人都写,将工作的伟大,并保存你的麻烦,你应该对如何做到这一点有基本的了解。

I know this is a popular topic, but I've been searching quite a bit trying to find the answer and strangely haven't found anything that has been able to help...

I've got a basic Rails 3 application displaying database entries in a table. A separate application posts new entries into the database, and I'd like the Rails app to poll and refresh to display the new content. Right now, I have a terrible piece of javascript simply reloading the whole page and its proving to be my roadblock. I have a search bar and check bars to remove multiple items, but these are both reset upon a page refresh (again, I know this is awful and horrendously broken).

From what I have read, I gather that I should use AJAX to poll the database and simply render database entries that are newer than the newest entry displayed. How to precisely execute this is another story...

Most of the tutorials I have found have been out of date, and I've been unable to get the legacy code running. I am new to both AJAX and Rails, and I would love if anyone has any guidance, advice, tutorials, or personal criticism (:P) that may point me in the right direction.

Thanks!

EDIT: I've got new entries being pulled into the page, but they aren't rendering correctly at all. For some reason, they aren't being added as entries into my table. Here's my code, hopefully you guys might be able to pick something out I missed: * Note -> requests are actually the name of my database entries. Requests is the model.

views/requests/index.html.erb

    <table class="center">
<div id="requests">
 <tr>
    <th><%= sortable "Patient_Name", "Patient Name" %></th>
    <th><%= sortable "Room_Number", "Room Number" %></th>
    <th><%= sortable "Request" %></th>
    <th><%= sortable "Urgency" %></th>
    <th><%= sortable "count", "Request Count" %></th>
    <th><%= sortable "created_at", "Created At" %></th>
    <th></th>
  </tr>
<%= render @requests %>
</div>
</table>

Request Partial: views/requests/_request.html.erb

 <div class="request" data-time="<%= request.created_at.to_i %>">
    <tr>
        <td><%= request.Patient_Name %></td>
        <td><%= request.Room_Number %></td>
        <td><%= request.Request %></td>
        <td><%= request.Urgency %></td>
        <td><%= request.count %></td>
        <td><%= request.created_at.getlocal.strftime("%r on %D") %></td>
        <td><%= link_to 'Remove', request, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
</div>

views/requests/index.js.erb

$('#requests').append("<%=raw escape_javascript(render(@requests))%>");

解决方案

How is this?

http://www.datatables.net/

another whole set are here:

http://plugins.jquery.com/plugin-tags/ajax-table

Sometimes it's better to use what's there than to build new.

But to answer your question, it depends on whether you are manipulating the DOM yourself or letting Prototype, jQuery, MooTools, or whatever do the heavy lifting. The idea is to set a timer in your Javascript and when the timer fires, ask the server for updates. The server -- your Rails app -- will package up whatever has changed and send it back.

Step 1: Decide on a framework. I use jQuery but most will do.

Step 2: Decide on a data format. I use JSON, but XML will do.

Step 3: Set up the timer loop. It involves using settimer() and resetting it after each observation.

Step 4: In the handler that executes when the timer fires, package up a "last seen" timestamp and make the Ajax request. How you do it varies from framework to framework -- or if you are coding right to the DOM, from browser to browser.

Step 5: Set up a "success" handler so that when your server returns zero or more rows of data, you can insert these TR-TD-/TD-/TR rows in your table.

Step 6: Update any internal junk like number or row counters or whatever else your Javascript is tracking.

Note: You may find there are plugins other people have written that will work great and save you the trouble, but you should have a basic understanding of how this is done.