如何动态生成动态项目?

发布于 2024-07-26 08:02:02 字数 251 浏览 7 评论 0原文

我正在寻找一种使用 javascript 附加元素的方法。 基本上我有两个项目清单。 一个有一个带有“ADD”按钮(使用“link_to_remote”)的项目列表,另一个有一个带有“REMOVE”按钮(使用“link_to_remote”)的项目列表。 当我单击“添加”时,它立即将该项目放入另一个列表中。 但是,我需要能够让新插入的项目执行“删除”的相反操作。

我看不出有什么办法(此外,也许创建一个部分来渲染非对象)来动态生成这种性质的动态项目。

I'm looking for a way to append elements with javascript using javascript. Basically I have two lists of items. One has a list of items with an "ADD" button (using "link_to_remote") and the other has a list of items with a "REMOVE" button (using "link_to_remote"). When I click the "ADD" it immediately places the item into the other list. However, I need to be able to have the newly inserted item perform the reverse action for "REMOVE".

I see no way (besides, perhaps creating a partial to render a non-object) to dynamically generate dynamic items of this nature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-08-02 08:02:02

您检查过 jQuery 吗? 它是一个流行的 JavaScript 库。 它可以轻松地完成您所描述的操作。 我已经使用它很多年了,它从来没有让我失望过。

本页文档显示了 append() 方法。 你可以做类似的事情...

$("span").appendTo("#foo");

$("span") 部分是查找 DOM 的一个或多个元素,然后是 appendTo("#foo") 部分将其附加到 DOM 的另一个元素。

您还可以附加任意 HTML 片段。 如果需要,可以通过 AJAX 请求检索这样的片段。 jQuery 具有非常简单且可靠的 AJAX 支持。

使用 jQuery,基本概念是将 JavaScript 方法绑定到单独的 .js 文件中的 DOM 元素,而不是使用 HTML“on_this”或“on_that< /代码>”属性。 因此,如果您在这种情况下使用 jQuery,您就不会考虑通过模板生成 JS 代码。 add()remove() 函数将位于您的 .js 文件中,您可以将它们绑定或取消绑定到 DOM 元素如所须。 例如,当您向列表中添加 li 时,您需要从 li 中取消绑定 add() 函数,并绑定 li >remove() 函数。

Have you checked out jQuery? It is a popular JavaScript library. It can do exactly what you described with ease. I've used it for years and it's never let me down.

This page of the documentation shows the append() method. You can do stuff like...

$("span").appendTo("#foo");

The $("span") part is finding an element (or elements) of the DOM, then the appendTo("#foo") part is appending it to another element of the DOM.

You can also append arbitrary snippets of HTML. If needed, such a snippet could be retrieved via an AJAX request. jQuery has really simple and reliable AJAX support.

With jQuery, the basic concept is that you bind JavaScript methods to DOM elements in a separate .js file, rather than using HTML "on_this" or "on_that" attibutes. So if you used jQuery for this situation, you wouldn't think in terms of generating JS code through templates. The add() and remove() functions would be in your .js file ready to go and you would bind and unbind them to DOM elements as needed. For example, when you added an li to the list, you'd unbind your add() function from the li and bind the remove() function to it.

夜清冷一曲。 2024-08-02 08:02:02

您可能正在寻找一些 javascript 模板解决方案。 查看此链接...

http://www .hokstad.com/mini-reviews-of-19-ruby-template-engines.html

希望这能回答您的问题。

you might be looking for some javascript templating solution. Check this link out...

http://www.hokstad.com/mini-reviews-of-19-ruby-template-engines.html

Hope this answers your question.

青衫负雪 2024-08-02 08:02:02

警告:hacky解决方案。

我有一个选择列表,需要动态添加和删除元素。 两者都是 AJAX 调用。

控制器方法呈现包含 Javascript 的部分,以添加和/或从选择列表中删除项目。 该部分是一个普通的旧 erb 文件,与任何其他部分一样,插入适当的 DOM 标识符(从列表中对象的对象 ID 生成)。

我研究了一些 Javascript 模板解决方案,但没有一个足够灵活。 最后,我自己生成 Javascript 变得更容易了。

WARNING: hacky solution.

I have a select list that I need to dynamically add and remove elements from. Both are AJAX calls.

The controller method renders a partial that contains Javascript to add and/or remove the items from the select list. The partial, which is a plain old erb file like any other partial, inserts the appropriate DOM identifiers (which are generated from the object IDs of the objects in the list).

I looked at a few Javascript templating solutions, but none of them were flexible enough. In the end it was easier to generate the Javascript myself.

岁月静好 2024-08-02 08:02:02

这相当容易。 有很多事情可以为你做这件事(查看 jquery)。

我不久前手工编码了一些东西(很抱歉代码写得不好)。 它从播放列表中添加和删除曲目

控制器:

def add_track
    unless session[:admin_playlist_tracks].include?(params[:recording_id])
      session[:admin_playlist_tracks] << params[:recording_id]
    end
    render :partial => "all_tracks"
  end

  def remove_track
    session[:admin_playlist_tracks].delete_if {|x| x.to_s == params[:recording_id].to_s }
    render :partial => "all_tracks"
  end

容器:

<div id="artist_recordings" class="autocomplete_search"></div>

添加内容:

<%= link_to_remote  "Add", 
                                :url => {:controller => :playlists, :action => :add_track, :recording_id => recording.id}, 
                                :update =>"all_tracks",
                                :complete => visual_effect(:highlight, 'all_tracks') %>

显示/删除内容:

<%session[:admin_playlist_tracks].each do |recording_id|%>
        <div style="margin-bottom:4px;">
            [<%=link_to_remote "Remove", 
                                    :url => {:controller => :playlists, :action => :remove_track, :recording_id => recording_id}, 
                                    :update =>"all_tracks"%>]
            <%recording = Recording.find_by_id(recording_id)%>                      
            <%=recording.song.title%> <br />
            by  <%=recording.artist.full_name%> (<%=recording.release_year%>)
        </div>                  
    <%end%>

这对我有用,因为我可以使用会话多变的。 不过要小心! 在某些情况下,用户将拥有具有相同表单的多个窗口,这肯定会中断,因为会话变量上会有并发访问。

由于我也在这方面进行了一些自动完成,因此缺少一些部分,但我希望这将帮助您获得重要的想法。

This can be fairly easy. There are a bunch of things doing this for you (check out jquery).

I handcoded something a while ago (sorry for the poorly written code). It adds and remove tracks from a playlist

controller:

def add_track
    unless session[:admin_playlist_tracks].include?(params[:recording_id])
      session[:admin_playlist_tracks] << params[:recording_id]
    end
    render :partial => "all_tracks"
  end

  def remove_track
    session[:admin_playlist_tracks].delete_if {|x| x.to_s == params[:recording_id].to_s }
    render :partial => "all_tracks"
  end

container:

<div id="artist_recordings" class="autocomplete_search"></div>

Adding stuff:

<%= link_to_remote  "Add", 
                                :url => {:controller => :playlists, :action => :add_track, :recording_id => recording.id}, 
                                :update =>"all_tracks",
                                :complete => visual_effect(:highlight, 'all_tracks') %>

Displaying / removing stuff:

<%session[:admin_playlist_tracks].each do |recording_id|%>
        <div style="margin-bottom:4px;">
            [<%=link_to_remote "Remove", 
                                    :url => {:controller => :playlists, :action => :remove_track, :recording_id => recording_id}, 
                                    :update =>"all_tracks"%>]
            <%recording = Recording.find_by_id(recording_id)%>                      
            <%=recording.song.title%> <br />
            by  <%=recording.artist.full_name%> (<%=recording.release_year%>)
        </div>                  
    <%end%>

This worked with me because I could use session variable. But be careful! In some cases where a user will have various windows with the same form, this will surely break since there will be concurrent access on the session variable.

There are some parts missing because I'm doing some autocomplete on this as well, but I hope this will help you get the big idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文