Fork me on GitHub
#hoplon
<
2017-02-24
>
raywillig16:02:06

hi All, i think this question has been asked/answered before, but has anyone experienced the situation where when the options of a select have been changed in a loop-tpl and then the selected attribute doesn't respond?

dm316:02:21

what do you mean by “doesn’t respond”?

raywillig16:02:30

meaning that when the options change, even though the selected attribute is being set correctly (I can inspect it), the correct option is not actually selected on the web page

dm316:02:57

is it a plain <select> or some javascript plugin?

raywillig16:02:18

plain old select

dm317:02:09

this seems to work:

(page "index.html")

(defc items [1 2 3 4 5])
(defc selected 1)

(defn- remove-last [_]
  (when (seq @items)
    (swap! items pop)))

(defn- append [_]
  (swap! items
    (fn [items]
      (if-let [l (last items)]
        (conj items (inc l))
        [1]))))

(html
  (body
    (div
      (h1 "Select")
      (select
        (for-tpl [item items]
          (option :id (cell= (str item))
                  :value (cell= (str item))
                  :selected (cell= (= item selected))
                  :change (fn [_] (reset! selected item))
                  (text item))))
      (button :click remove-last "Remove last")
      (button :click append "Append"))))

dm317:02:37

now you are setting selected somehow, right?

dm317:02:49

updated the snippet

dm317:02:48

does this look like what you’re trying to do?

raywillig17:02:03

yes, I think from looking at this i may have a handle on the problem

alandipert20:02:48

was doing some hoplon with @candera today and he showed me some stuff he was doing with document fragments

alandipert20:02:02

realized: what if document fragment is what loop-tpl should return?

alandipert20:02:33

altho i don't know if a document fragment "disappears" if you append one to an elment

alandipert20:02:09

like if you make a doc fragn w/ 3 li kids, and then append the frag to an ol. do the kids become children of the ol?

alandipert20:02:16

all questions i could answer for myself in 30 seconds of js console, brb

alandipert20:02:05

ok wow. when you appendChild a document fragment to an element, the fragment disappears. its children are spliced in to the new parent

alandipert20:02:58

the frag object is still around, but its children are emptied