Search and render data table simple objects

What I’m trying to do:
I would like to incorporate an inventory system to use on my app in order to do so I have decided to use a simple object column in order, therefore, I wouldn’t need a row for every item.

Example data in simple objects column:

{"items":[{"name":"cookie","amount":"1"}]}

I did read the docs on how to search simply objects but that only returns one object from the list while I need to return every item in their inventory.

How would I go about retrieving this data and render it in a data grid so users can view their inventory? The item name in one column, and the quantity in the next column.

Making some assumptions here:

  1. You have that simple object column in a data table row with other columns, like the user whose inventory this is

  2. The simple object column is named inventory, and the user column is named user and links to the users table

If the goal is to have that user be able to see all their inventory, you do not want to search on the simple object column. You want to search on the user column to get the data table row for that user. Let’s say that looks something like this:

user = anvil.users.get_user()
row = app_tables.inventories.search(user=user)

The list of inventory items is then available at row['inventory']['items'] and you can set a repeating panel’s items to that list to get them displayed.

2 Likes

It seems to have thrown an error when I tried using that method is that for objects in general or simply object columns as well?

error thrown:

TypeError: Indexing with [] is not supported on this anvil.tables.SearchIterator

Currently, a user inventory is stored in a table called Inventories using their email as an ID and a simple objects column to store the inventory object

Here is some screenshots to better understand my goal

Data to render:
Screenshot 2021-04-22 6.39.38 PM

Render to:
Screenshot 2021-04-22 6.40.02 PM

Pardon the confusion of what I wanted to do before

Sorry, I used search in my example and you really want get:

row = app_tables.inventories.get(user=user)

search returns an iterator, which you’d then have to get the first item out of. Since you’re only looking to get a single row anyway, get will return the matching row.

That seems to have gotten rid of the error but doesn’t seem to show on the data grid,
does anyone happen to know how to get “name” from the data table simple object column to go under the “item” column in my data grid and “amount” to go under the “#” column in my data grid

{"items":[{"name":"cookie","amount":"1"}]}

by the way, @jshaffstall you have been a great help so far and I appreciate it, thanks

It’d help if you could provide a clone link so we can see how the data grid is setup, how you’re populating it, etc. Just having the code would help, but part of the setup is in the datagrid properties.

Here you go
https://anvil.works/build#clone:I4NWVMHK2I7HG5SO=MFOATLQHLUNQ3YWWNJEAHM2O

A few notes on viewing the app

  • May be hard to navigate due to size and complexity
  • Account will be required for testing

Can you pinpoint the form that has the code you’re having issues with? I don’t see any server functions that are pulling data from the inventories table, and for some reason none of your forms are showing properly for me in the IDE.

The form would be under the name “home” I have it set up so when a user visits that form their characters and inventory would render to the data grids in that form
look for the section of code:

# Any code you write here will run when the form opens.
    user = anvil.users.get_user(allow_remembered=True)
    self.paw_panel.items = app_tables.paws.search(Id=user['email'])
    row = app_tables.inventories.get(Id=user['email'])

Thanks, that helps. I still can’t see the GUI controls in the IDE, so I can’t see how the data grid is setup, but I’ll take a stab at it anyway.

You’re assigning your inventory row to row, but not doing anything with it. What I’d expect to see is something like:

self.repeating_panel_1.items = row['inventories']['items']

Adjusting for the actual name of your repeating panel name and column names, of course. The goal is to get the list out of the simple object column and into the repeating panel.

Thanks that seems to have worked

1 Like