List widget it can contain ListItems. Add items to it by using the standard append(item, key) function or generate a filled list from a string list by means of the function new_from_list. Use the list in conjunction of its onselection event. Register a listener with ListView.onselection.conn
| 2573 | |
| 2574 | |
| 2575 | class ListView(Container): |
| 2576 | """List widget it can contain ListItems. Add items to it by using the standard append(item, key) function or |
| 2577 | generate a filled list from a string list by means of the function new_from_list. Use the list in conjunction of |
| 2578 | its onselection event. Register a listener with ListView.onselection.connect. |
| 2579 | """ |
| 2580 | |
| 2581 | def __init__(self, selectable=True, *args, **kwargs): |
| 2582 | """ |
| 2583 | Args: |
| 2584 | kwargs: See Container.__init__() |
| 2585 | """ |
| 2586 | super(ListView, self).__init__(*args, **kwargs) |
| 2587 | self.type = 'ul' |
| 2588 | self._selected_item = None |
| 2589 | self._selected_key = None |
| 2590 | self._selectable = selectable |
| 2591 | |
| 2592 | @classmethod |
| 2593 | def new_from_list(cls, items, **kwargs): |
| 2594 | """Populates the ListView with a string list. |
| 2595 | |
| 2596 | Args: |
| 2597 | items (list): list of strings to fill the widget with. |
| 2598 | """ |
| 2599 | obj = cls(**kwargs) |
| 2600 | for item in items: |
| 2601 | obj.append(ListItem(item)) |
| 2602 | return obj |
| 2603 | |
| 2604 | def append(self, value, key=''): |
| 2605 | """Appends child items to the ListView. The items are accessible by list.children[key]. |
| 2606 | |
| 2607 | Args: |
| 2608 | value (ListItem, or iterable of ListItems): The child to be appended. In case of a dictionary, |
| 2609 | each item's key is used as 'key' param for the single append. |
| 2610 | key (str): The unique string identifier for the child. Ignored in case of iterable 'value' |
| 2611 | param. |
| 2612 | """ |
| 2613 | if isinstance(value, type('')) or isinstance(value, type(u'')): |
| 2614 | value = ListItem(value) |
| 2615 | |
| 2616 | keys = super(ListView, self).append(value, key=key) |
| 2617 | if type(value) in (list, tuple, dict): |
| 2618 | for k in keys: |
| 2619 | if self.EVENT_ONCLICK not in self.children[k].attributes: |
| 2620 | self.children[k].onclick.connect(self.onselection) |
| 2621 | self.children[k].attributes['selected'] = False |
| 2622 | else: |
| 2623 | # if an event listener is already set for the added item, it will not generate a selection event |
| 2624 | if self.EVENT_ONCLICK not in value.attributes: |
| 2625 | value.onclick.connect(self.onselection) |
| 2626 | value.attributes['selected'] = False |
| 2627 | return keys |
| 2628 | |
| 2629 | def empty(self): |
| 2630 | """Removes all children from the list""" |
| 2631 | self._selected_item = None |
| 2632 | self._selected_key = None |