Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the Show Column value via API #970

Open
beddows opened this issue May 8, 2024 · 4 comments
Open

Changing the Show Column value via API #970

beddows opened this issue May 8, 2024 · 4 comments

Comments

@beddows
Copy link

beddows commented May 8, 2024

In Grist-electron, I'm trying to change the Show Column value of a Reference column. Using PATCH columns, I'm attempting to update the visibleCol field to a new value.

In order to find out what the correct value might be, I'm using GET columns to retrieve the current visibleCol integer value.

The problem I'm having is that changing the visibleCol value only changes the Show Column dropdown value. The column itself remains unchanged. Even refreshing the page shows the correct value in the dropdown with no effect on the table.

How is the column value changed? And how is the visibleCol value derived for each reference column?

@dsagal
Copy link
Member

dsagal commented May 9, 2024

There is a bit more complicated, and requires two actions. One to update visibleCol (like you tried), which affects what's shown in the dropdown, and one to update the display formula for the column, since the values are shown using a hidden helper formula column. You can in fact send both of those actions in a single API call, but it needs to use an undocumented /apply endpoint.

All of this is reaching into the internals a bit, so there is risk it would get changed over time, but it's been stable.

For both the reference column you are changing, and for the visible column, you'll need their integer ID (colRef in /column API docs) and their string names (id in /column API docs). So you'll need: DOC_ID, TABLE_ID (the string identifier of the table containing the reference column to update), COL_REF (integer), COL_ID (string), VISIBLE_COL_REF (integer), VISIBLE_COL_ID (string).

POST /api/docs/DOC_ID/apply
[
  ["UpdateRecord", "_grist_Tables_column", COL_REF, {"visibleCol": VISIBLE_COL_REF}],
  ["SetDisplayFormula", TABLE_ID, null, COL_REF, "$COL_ID.VISIBLE_COL_ID"]
]

For example, in the template in https://templates.getgrist.com/doc/afterschool-program, to change the Student column in the "Enrollments" table to show First_Name (instead of Full_Name), the body of the JSON POST would be this:

[
  ["UpdateRecord", "_grist_Tables_column", 39, {"visibleCol": 25}],
  ["SetDisplayFormula", "Enrollments", null, 39, "$Student.First_Name"]
]

@beddows
Copy link
Author

beddows commented May 10, 2024

Thanks @dsagal , I feel like I'm getting closer... Let me walk you through what I have.

Xnip2024-05-09_19-00-03 Xnip2024-05-09_19-00-30
  1. When the tables are initially created, UserID is set to Show Column "Row ID". I can manually change it to Name or Email, no issues.

  2. After running POST apply, Show Column is correctly set to "Name". But this is what UserID looks like below. The Name values don't appear until I double click on the cell. So the reference is correctly made. But even after selecting John or Jane, they don't "stick" as values, the cells remain empty.

Xnip2024-05-09_19-32-28 Xnip2024-05-09_19-32-50 Xnip2024-05-09_19-33-12
  1. In step 1, if I manually change Row ID to Email, then POST apply works fine and changes UserID and Show Column to Name. So the table works and POST apply works, they just don't work together programmatically...

To confirm:

COL_REF: the colRef of UserID (e.g.: 77)
VISIBLE_COL_REF: the colRef of Name (e.g.: 73)
TABLE_ID: "Users"
COL_ID: "UserID"
VISIBLE_COL_ID: "Name"

@dsagal
Copy link
Member

dsagal commented May 10, 2024

Ah, try TABLE_ID of "Orders". It should be the table containing the reference column that needs to display differently. (I had a mistake on that in my example, correcting it now.)

@beddows
Copy link
Author

beddows commented May 10, 2024

Thanks, all working now! Love what you guys have created here, so much potential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants