Count <li> items in div, RJS

Hello,

How i can count

  • items in div using RJS?
  • one
  • two
  • three
  • <%= link_to_remote.......ADD %>

    and in add action i want to add new li item with unique ID and get

  • 's elements count?

    Thanks

  • Hi Igor,

    Igor K. wrote:

    unique ID and get

  • 's elements count?
    Just count them in the controller method you invoke as you add them.
    Store
    the count in a session variable or in a table to persist your count.
  • HTH,
    Bill

    unique ID and get

  • 's elements count?
    Just count them in the controller method you invoke as you add them.
    Store
    the count in a session variable or in a table to persist your count.
  • I have a following code in add.rjs

    count = 0
    page.select(’#results li’).each do |element|
    count = count + 1
    #element.hide
    end
    page.insert_html :bottom, ‘results’, "

  • #{a.to_s + "

    " + count.to_s}

  • "
    page.visual_effect :highlight, count.to_s, :duration => 2.5

    and following in view

  • one
  • <%= link_to_remote "add", :url => {:action => :add} %>

    and i always get count = 1

    Hi Igor,

    Igor K. wrote:

    and following in view

  • one
  • <%= link_to_remote "add", :url => {:action => :add} %>

    and i always get count = 1

    I’m not sure I understand exactly what it is you’re trying to
    accomplish,
    but in general… I would recomment keeping track of ‘count’ in your
    controller method; storing / retrieving it there and making it available
    to
    your view via an instance variable. You’re not dealing with a static
    page
    of which you have no knowledge. Everything that’s on the page is put
    there
    by your app during the session. No?

    HTH,
    Bill

    would recomment keeping track of ‘count’ in your

    controller method; storing / retrieving it there and making it available
    to
    your view via an instance variable. You’re not dealing with a static
    page

    Yes, if i work with dynamic pages i can save elements count to session,
    but my question is connected with static pages.
    For example designer add one element to div, and i set count variable in
    controller to 1, but when designed add new element to div, again i need
    to change count variable.
    i just want to know is it possible count some elements, for example

  • by id of
    where are they located.

    maybe anybody can give a advice how to set id of new element to unique
    value(i guess i can user time(in milliseconds) for example)

    Thanks

  • Hi Igor

    In an action in the controller, responding to the xhr request when you
    do

    render :update to |page|

    I just did a page.inspect in the log and found @content_for_layout - I
    think
    you would have to do something like page.session.content_for_layout -
    the
    log is pretty ugly - you can check.

    But, if you have @content_for_layout you could count the occurance of

  • in that and unless you have
  • in your layout you will have the number of li's.

    However, this sounds very convoluted - you would probably be better of
    just
    adding the milliseconds id for the li.

    cheers
    Ivor