1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from pyjamas import DOM
16 from pyjamas import Factory
17
18 from FocusWidget import FocusWidget
19 from pyjamas.ui import Event
20 from __pyjamas__ import console
21
33
35 self.changeListeners.append(listener)
36
37 - def addItem(self, item, value = None):
38 self.insertItem(item, value, self.INSERT_AT_END)
39
44
47
48 - def getItemText(self, index):
49 child = DOM.getChild(self.getElement(), index)
50 return DOM.getInnerText(child)
51
54
56 """ returns the selected item's index on a single-select
57 listbox. returns -1 if no item is selected.
58 for multi-select, use repeated calls to isItemSelected.
59 """
60 return DOM.getIntAttribute(self.getElement(), "selectedIndex")
61
67
70
71
77
82
85
95
97 self.changeListeners.remove(listener)
98
102
107
110
113
116
118 """ selects the ListBox according to a value.
119 to select by item, see selectItem.
120 # http://code.google.com/p/pyjamas/issues/detail?id=63
121 """
122 for n in range(self.getItemCount()):
123 if self.getValue(n) == value:
124 self.setSelectedIndex(n)
125 return n
126 return None
127
129 """ selects the ListBox according to an item's text
130 to select by value, see selectValue.
131 # http://code.google.com/p/pyjamas/issues/detail?id=63
132 """
133 for n in range(self.getItemCount()):
134 if self.getItemText(n) == item:
135 self.setSelectedIndex(n)
136 return n
137 return None
138
139 - def setItemText(self, index, text):
140 self.checkIndex(index)
141 if text is None:
142 console.error("Cannot set an option to have null text")
143 return
144 DOM.setOptionText(self.getElement(), text, index)
145
151
154
160
161 - def getSelectedItemText(self, ignore_first_value = False):
162 selected = []
163 if ignore_first_value:
164 start_idx = 1
165 else:
166 start_idx = 0
167 for i in range(start_idx,self.getItemCount()):
168 if self.isItemSelected(i):
169 selected.append(self.getItemText(i))
170 return selected
171
173 selected = []
174 if ignore_first_value:
175 start_idx = 1
176 else:
177 start_idx = 0
178 for i in range(start_idx,self.getItemCount()):
179 if self.isItemSelected(i):
180 selected.append(self.getValue(i))
181 return selected
182
183 - def setItemTextSelection(self, values):
184 if not values:
185 values = []
186 self.setSelectedIndex(0)
187 for i in range(0,self.getItemCount()):
188 if self.getItemText(i) in values:
189 self.setItemSelected(i, "selected")
190 else:
191 self.setItemSelected(i, "")
192
202
203 Factory.registerClass('pyjamas.ui.ListBox', ListBox)
204