Unable to retrieve primary key on details page from a Gridview

Hello,

My question is similar to the one contained in the topic “Parameter-based navigation using the selected card from a GridView.” (Passing the content of a Gridview card the user clicks on onto a Detail component on a separate page.)

Parameter-based navigation from a Table component to a Detail component works just fine, following the documentation.

Screenshot 1 below shows that I am able to pass the data with {{ data }}.

  1. However, I somehow cannot filter the students_table by its primary key (student_id in screenshot 2). I tried all variables I could think of ( {{ data }}, {{ item }}, {{ params }} and many others but the object retrieved is always empty.

  2. The other issue I am struggling with is the URL pointed to after the user clicks on one of the Gridview cards.

Starting URL (where the Gridview is located): /builder/students

Destination URL : /builder/the-wrong-page?student_id=

But I never selected the path ‘/builder/the-wrong-page’ in the navigation process between the Gridview URL and the Detail component URL.

Thanks for your time & help.

K.

Hey @KBW,

Regarding your first problem, I assume Screenshot 2 shows an action on the studentDetail page? If so, there is 2 problems why you can’t access the student_id.

The first issue, you even say you tried all kinds of variables, is how to access the query parameters. You mention that

Parameter-based navigation from a Table component to a Detail component works just fine, following the documentation.

and I don’t know exactly which documentation you are following, but here in the documentation is written that you access query params with activeRoute.queryParams.<variable_name>. So you should be able to read student_id with activeRoute.queryParams.student_id on the studentDetail page.

But, you also need to change the value you pass to student_id. Currently you pass the whole student object, which in itself might not seem wrong (except having a variable called student_id containing a whole object), however we are talking about query params, the parameters at the end of a URL. These parameters are obviously of the type string, as a part of the URL, so passing an object will result in the string "[object Object]". We can also see this in the URL:
image
So what you wan to do, is either just send {{data.student_id}} or if you want to have the whole object you can send it as {{JSON.stringify(data)}} and on the studentDetails page read it as JSON.parse(activeRoute.queryParams.student_id || "{}").

Now about your second problem. I think it’s safe to assume that the URL you want it to go to is somnething like /builder/studentDetail?student_id= and by looking at your Screenshot 1 it should theoretically go to that path. Since you didn’t share any component settings or any page setup it’s hard to determine why this is happening, but I can give you ideas on what to look out for:

  • Check that no component of the grid item points to the-wrong-path
  • Is any other action besides the one from Screenshot 1 triggered?
  • Is there some redirect active on the studentDetail page?

I hope this can help you getting further, feel free to ask aways if anything is still unclear :+1:t3:.

Hello @Max,

Thank you for your reply.

  1. “Regarding your first problem, I assume Screenshot 2 shows an action on the studentDetail page?”

    Yes, the page is called ‘Student-details’.

    The action on that page which should filter the
    ‘students_table’ table is ‘showStudentDetails’.

    ‘showStudentDetails’ should filter the table
    ‘students_table’ by the primary key ‘student_id’ which
    belongs to the Gridview card the user clicked on.

  2. “
 and I don’t know exactly which documentation you are following, 
”

    To be clear, I followed the article ‘Reading query
    params from URL’ in the documentation (/how-
    tos/layout-and-navigation/read-query-params-from-url)

  3. “But, you also need to change the value you pass to student_id. So what you want to do, is either just send {{data.student_id}} or 
”

    Please refer to the attached screenshot below and
    see the error message in red.

    Note that I did not have this error message when I
    sent {{data}} yesterday.

    I had a green light message. (See yesterday’s
    screenshot)

  4. “I think it’s safe to assume that the URL you want it to go to is somnething like /builder/studentDetail?student_id=”

    That’s correct.

  5. “Check that no component of the grid item points to the-wrong-path”

    There are no components on the grid pointing to ‘the-
    wrong-path’.

    “Is any other action besides the one from Screenshot
    1 triggered?”

    The only other action is the action
    ‘loadPublicStudentsTable’. It populates the grid and it
    works fine.

    Is there some redirect active on the studentDetail
    page?

    I am not aware of any ‘redirect active’ on the Student-details page

(anywhere: sidebar panel on the right where ‘CONTENT’ is found, bottom panel where ‘Actions’ is found)

Thanks.

K.

Mhh weird that you get the error. In the first screenshot you shared, the green text of {{data}} showed an object with the student_id property, so I thought that should do it.

First of all, I don’t understand if you have a gridview or table on the students page, but it shouldn’t make a big difference.

Then regarding

Could it be that you tried to execute the action by the Execute action button and not by clicking on the card/row? That would explain why {{data}} is null and therefore the error message.

Since you only wrote {{data}} for the student object I can only assume you pass the object as action argument under triggers, so something like this?
image
If you did, then you must always test this action by clicking the row/card, or else {{data}} will be null.

To get to the point, I just tried setting up a simple test app, with a UI Bakery integrated postgreSQL database, both a table and a gridview on the students page and a student-details page with a detail component. There were no problems setting it up according to the instructions from the article in the docs you mentioned.
21-01-2025_15-20-16

I think your main problem (besides the-wrong-path problem) is correctly passing the student_id to the student-details page. It should be possible to do with the instructions in the docs, but if want me to help you I need more information:

  • are the students listed in a table or gridview?
  • how exactly are you passing the student data to the LoadSingleStudent action?
  • how exactly are you reading and using the passed query params on the student-details page?

@Max ,

(1)

“First of all, I don’t understand if you have a gridview or table on the students page”

I think the first two paragraphs I wrote in my original entry are pretty clear:

[p1]

My question is similar to the one contained in the topic “Parameter-based navigation using the selected card from a GridView. (Passing the content of a Gridview card the user clicks on onto a Detail component on a separate page.)

[p2]

Parameter-based navigation from a Table component to a Detail component works just fine, following the documentation.

So if it works just fine, I suppose no help is sought.

(Everytime I referred to the term “table” in my previous entries in this topic, I never meant “UI Bakery table component.” I referred to a “Postgres database table used by UI Bakery”.)

(2) Now:

Accessing a detail component on a page from a table component on another page

is the same as :

Accessing a detail component on a page from a gridview component on another page.

I highly doubt this is the same.

Why ?

a) Because if it was, I don’t think this entry would have been created:

https://community.uibakery.io/t/parameter-based-navigation-using-the-selected-card-from-a-gridview/184

b) Have a look at screenshot 2, screenshot 3 and screenshot 4 below.

screenshot 2 (we are on the students page): the query params are as you suggested.

screenshot 3 (we are on the students page): the navigation points to the Student-details page.

screenshot 4: Whichever card I click on:

  • I am redirected to the wrong page (the so-called ‘/builder/the-wrong-page’ and I know nothing about url redirect)

  • the url I get shows ‘?student_id= (empty)’ → the query param in screenshot 2 is
    definitely not passed anywhere.

So we have two inherent problems with Gridviews and again, everything works perfectly fine everytime with Table components (see [p2] above).

Do you have any suggestions?

K.

Oh sorry, it seems I totaly misunderstood the table/gridview thing. Just to make it clear as you didn’t give a definite answer: You were successful doing parameter-based navigation from a table but are currently having problems doing the same with gridview?


About “accessing a detail component on a page from a table or gridview”, it is pretty much the same. In fact, it comes down to just 2 differences:

  1. The trigger for the table is On Row Select and for gridview On Click
  2. The action parameter for the table is {{selectedRow.data}} and for gridview {{item}}

And the second difference is the exact reason that other question has been asked here.


Next, looking at the screenshots, the {{activeRoute.queryParams.student_id}} for the query parameter will not work. With activeRoute.queryParams.<param> you can read query parameters, but in the case of the navigation action it will probably only give undefined back, as we are trying to set the exact query param you are trying to read in that moment. Depending on how your action trigger is set up, you should be able to just use {{data.id}} or {{params.id}} (in the context of an action parameter it’s the same).

But the weirdest thing is the redirect to the wrong page. Because the card can only cause a redirect with triggers and if LoadingSingleStudent is the only action triggered On Click, I honestly don’t understand how that is happening. I know it’s a stretch, but check if any of the components inside the card
open that page, or if the gridview is inside a container like parent-component (container, modal, etc.) that would trigger an action that navigates to the wrong page.
Something else I just thought could be the case, is that the Page URL of the student-details page is not what you’re expecting, so check that as well.
image

If you can’t find out why the redirect to the wrong page happens, I personally would, depending on how much else is on the page, either just delete and make the gridview component again or create the whole page again. This mechanic itself is not a lot of work if you know what to do, so below I’ll describe the whole process how to “access a detail component on a page from a gridview”.

How to make parameter-based navigation using the selected card from a GridView
  1. First of all, we’ll set up the database, but since that’s not on the topic and might vary depending on which database you use, I’ll leave that to your own. For the example I created a table in the UI Bakery integrated postgreSQL database like so
    CREATE TABLE dogs(
        id bigserial primary key,
        name varchar(100) NOT NULL,
        age integer NOT NULL,
        breed varchar(100) NOT NULL,
        fav_toy timestamp default NULL
    );
    
    And added some test data
    INSERT INTO dogs (name, age, breed, fav_toy) VALUES
    ('Buddy', 3, 'Golden Retriever', 'Tennis Ball'),
    ('Charlie', 2, 'Beagle', 'Frisbee'),
    ('Max', 4, 'Bulldog', 'Stuffed Animal'),
    ('Bella', 1, 'Labrador', 'Rope Toy'),
    ('Lucy', 5, 'Poodle', 'Squeaky Bone');
    
  2. Create 2 pages: one for displaying all objects and one for the details of an object
    image
  3. On the dogs page add a gridview component
  4. Create an action that retrieves all rows from the table and set it as the Data of the gridview component

    image
  5. Setup cards in gridview to your liking, I set them up like this
  6. Create new action that navigates to dog-details with the id of the object as query parameter
  7. Add a trigger to the cards in the gridview with the object as action parameter
    image
  8. On the dog-details page add a detail component
  9. Still on the dog-details page, create a new action that loads the specific row by the id and add it as the Data of the detail component

    image
  10. Now if you go back to the dogs page, click on any card and it will navigate to dog-details with the query params set to ?dog_id=<id>. The action loading the object will execute and passes the result to the detail component. Press Regenerate structure so the details component automatically sets the right structure
    image
    image

And with that we’re done, clicking on the cards in the gridview will take you to the details page and load the appropiate object. But, since we are loading all the objects on the dogs page already, we could send the whole object as query param. Not that I recommend it, in fact, it’s probably not favorable to do since objects can get quite big and the URL doesn’t look very nice with all that stuff in there. But, I’ll quickly show how to do it as a reference for what you could do:

  1. Change the navigate action on the dogs page so it’ll send the whole object as string
  2. Create a new action on the dog-details page which parses the string and returns the object and set it as the Data of the details component

    image

Now again, if you click on the cards on dogs it will navigate to dog-details and “load” the object into the details component.

1 Like

@Max

Thanks for your help & support, I will look into this.

Just to make it clear as you didn’t give a definite answer: You were successful doing parameter-based navigation from a table but are currently having problems doing the same with gridview?

Parameter-based navigation from a Table component to a Detail component works just fine, following the documentation. So if it works just fine, I suppose no help is sought.

K.

@Max,

One last question please:

You wrote on January 20th:

“So you should be able to read student_id with activeRoute.queryParams.student_id on the studentDetail page.”

On January 22nd, you wrote:

"Next, looking at the screenshots, the {{activeRoute.queryParams.student_id}} for the query parameter will not work. "

Can you please clarify this?

K.

Ah yes, I probably didn’t clarify good enough.
So, in my response on the 20th

I meant that on the studentDetail page you can use {{activeRoute.queryParams.student_id}} in the action that returns the data for the detail component to get the ?student_id=<id> query parameter from the URL.
But in your response on the 21st, I can see in ‘Screenshot 2’ that you used {{activeRoute.queryParams.student_id}} on the students page in the action that navigates to studentDetail and adds the query parameter to the URL. So basically you are trying there to use/read the query parameter student_id in the action where you are setting it.