-
-
Save Jartim00/433b7b5b57500044bad13134adbf75a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I use the following to get all results from a mysql table using sqlalchemy: | |
def get_all_users(self): | |
selectUsers = select(self.Users.UserID, self.Users.UniqueIdentifier, self.Users.DisplayName, self.Users.Status, self.Users.PermissionID) | |
with Session(self.engine) as session: | |
result = session.execute(selectUsers) | |
result = [list(x) for x in result] | |
return result | |
# This collects all the rows in my table and returns it as a list. | |
# In my main.py file: | |
class SelectableUserRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout): | |
''' Adds selection and focus behaviour to the view. ''' | |
class SelectableUserBoxLayout(RecycleDataViewBehavior, BoxLayout): | |
''' Add selection support to the Label ''' | |
index = None | |
selected = BooleanProperty(False) | |
selectable = BooleanProperty(True) | |
def refresh_view_attrs(self, rv, index, data): | |
''' Catch and handle the view changes ''' | |
self.index = index | |
return super(SelectableUserBoxLayout, self).refresh_view_attrs(rv, index, data) | |
def on_touch_down(self, touch): | |
''' Add selection on touch down ''' | |
if super(SelectableUserBoxLayout, self).on_touch_down(touch): | |
return True | |
if self.collide_point(*touch.pos) and self.selectable: | |
return self.parent.select_with_touch(self.index, touch) | |
def apply_selection(self, rv, index, is_selected): | |
''' Respond to the selection of items in the view. ''' | |
self.selected = is_selected | |
rv.data[index]['selected'] = self.selected | |
if is_selected: | |
print("selection changed to {0}".format(rv.data[index])) | |
# Here I add data to a second RecycleView that shows all the user's reservations | |
app.root.ids.management_scr.resdata.data = [{"resid": str(x[0]),"startTime": str(x[1]), "endTime": str(x[2]), "userId": str(x[3]), "lockerId": str(x[4]), "selected":False} for x in database.get_reservation_by_userid(userid)] | |
else: | |
print("selection removed for {0}".format(rv.data[index])) | |
app.root.ids.management_scr.resdata.data = [] | |
class UserListView(RecycleView): | |
userdata = ObjectProperty() | |
def __init__(self, **kwargs): | |
super(UserListView, self).__init__(**kwargs) | |
# This is the important part, this populates your RecycleView with data | |
self.data = [{"userid": str(x[0]),"uniqueid": x[1], "displayname": str(x[2]), "status": str(x[3]), "permissionid": str(x[4]), "selected":False} for x in database.get_all_users()] | |
# In my .kv file: | |
<SelectableUserBoxLayout>: | |
# Draw a background to indicate selection | |
font_size: '26sp' | |
canvas.before: | |
Color: | |
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1) | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
userid: "" | |
displayname: "" | |
status: "" | |
permissionid: "" | |
GridLayout: | |
cols:4 | |
Label: | |
text: root.userid | |
size_hint_x: .1 | |
Label: | |
text: 'Anonymous' | |
size_hint_x: .6 | |
Label: | |
text: root.status | |
size_hint_x: .1 | |
Label: | |
text: root.permissionid | |
size_hint_x: .2 | |
<UserListView>: | |
id: userdata | |
viewclass: 'SelectableUserBoxLayout' | |
SelectableUserRecycleBoxLayout: | |
id: controller | |
default_size: None, dp(56) | |
default_size_hint: 1, None | |
size_hint_y: None | |
height: self.minimum_height | |
orientation: 'vertical' | |
multiselect: False | |
touch_multiselect: False | |
font_size: '26sp' | |
# inside my screen I add the following: | |
<Screen>: | |
BoxLayout: | |
orientation: 'vertical' | |
Label: | |
size_hint_y: .1 | |
text: 'User List:' | |
Userlistview: | |
id: userdata | |
size_hint_y: .9 | |
# So when my table has users inside of it, I get a scrollable list with all my users, and is selectable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment