Coding widgets?

I’m trying to set up a simple accounting system. I want several tables, one for each activity. Following Dmitry’s suggestion I created a master table like so:

+------+------+-----+-------+------------+
| Date | IN   | OUT | Descr | Cost center|
+------+------+-----+-------+------------+

Where Cost center is a reference to a table of cost centers. Then I can create a new page with a table linked to the master, set the filter to only display a single cost center and hide that column, then add a grid widget to display the totals and their difference. This requires quite some work. Duplicating a page to add another table and modify it doesn’t work, while re-doing all the procedure is error prone if some time passed.

There is a Code View at the bottom left. It doesn’t show the widgets. It’s not editable. It has a function _default_Cost_center() which always returns 2, which is not what I meant. I grepped all files below grist-core and none of them had that code in it. How is that code used?

Is there a way to programmatically add a new page with widgets as above, just changing name and cost center number? It would be much easier to change a piece of code than clicking around anew. I browsed the API docs, but the only approximately fit thing I found is Modify a workspace, but the example only shows how to rename it. The Grist API help generated locally is much richer, but doesn’t mention widgets.

Hi! So let me try to address your questions first, and then explain how I would do it, which may make the questions moot.

The Code View shows the code in your document, i.e. your tables, columns, and formulas, so you won’t find it in grist-core (assuming you mean the codebase of Grist software at https://github.com/gristlabs/grist-core). Specifically _default_X function is the default or trigger formula for column X. E.g. _default_Cost_center is the default or trigger formula for “Cost center”. That’s the formula you enter in the creator panel, e.g. like this:

Screenshot 2025-02-05 at 14.02.26

I think the issue is that you want “Cost center” to be filled in automatically when you add a record, and a trigger formula seems the right tool, but unfortunately it’s not: a trigger formula has no knowledge of the view you are in when you add a record, so it can only produce a value that’s independent of the view.

The way that new record could have a suitable value is if you use widget linking – i.e. the “Select By” option when adding a widget, documented in Linking Page Widgets.

The simplest way, and what I would use, is to have a single page; add a table of Cost Centers to it, and add your master table as a second table on the same page, linked to the first one. You can also add a table of totals (summarized – using 𝚺 – by Cost Center) and link it to the first widget also. In this setup, you have one page, but it has “subpages” if you will, i.e. within this page you can select Cost Center, and see the details and summary for it. Adding rows will then automatically fill in their “Cost Center” based on the selected one.

There is also a way to make it work in separate pages, but it’s more awkward and harder to set up. Essentially, it is the same way I just described, but filter the “Cost centers” table to select just one cost center you want to see on this page. Save that filter. Then collapse that widget, so that it doesn’t take up so much space. The structure is still the same: master table is linked to “Cost centers” table. You are just not seeing the full “Cost centers” table, and because it’s filtered, it will always be open to the same cost center record. Then to make other pages, just duplicate it, open the collapsed “Cost centers” widget, change filters to select a different cost center, and save those filters.

Hope this helps!

Hi, thanks a lot for the hints.

However, I haven’t been able to to have the “Cost center” field automatically filled. I tried setting filters to either table and to the summary widget, but when I insert new values in the master table (MOVIMENTI) the “Cost center” remains blank. What am I doing wrong?

Afterwards, I happened to play with an online demo (Lightweight CRM), which does insert the selected contact on adding interactions. I redid setting up my thing, and this time works! Why?

While I tag Dmitri’s solution as such, I still regret not being able to figure out where I went wrong. I find that the Code View is rather uninformative, as it only indicates the column types, albeit in a way that is difficult to trace back to the method used to define them. No mention of links and other magic. What I’d need is a formal description that can be used to rebuild the same structure again.