Rails 3 AJAX solution for updating tables?

后端 未结 2 1602
刺人心
刺人心 2021-02-14 21:32

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

2条回答
  •  温柔的废话
    2021-02-14 21:53

    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.

提交回复
热议问题