Button that creates multiple rows

Hi,

I have this table
Customer - Product - number of products - button

When I push on the button I want the button to create multiple rows on another table based on the “number of products”. 2 products=2 rows.

I also want the button to add the customer on the first collum thats created in every row on that table.

Is there a way to do this?

1 Like

Hello Lasse,

I believe this possible by combining the “AddRow” and “FormulaMap” functions, see example here:

https://coda.io/d/Untitled_d19OjdM8lW4/Section-1_suxWf#_lug4l

In the example, I mimicked your table, and made the formula for the button as follows:

LeftPad("",[Number of Products]-1,"|").Split("|").FormulaMap(AddRow(SubProduct, SubProduct.Customer, thisRow.Customer, SubProduct.Product, thisRow.Product))

The Leftpad and Split is used to create a list that has the correct number of empty elements. There may be a more elegant way to do this.

The FormulaMap is then used to create a repeater function, so if the number was 3, Coda with then run:
AddRow(SubProduct, SubProduct.Customer, thisRow.Customer, SubProduct.Product, thisRow.Product)
AddRow(SubProduct, SubProduct.Customer, thisRow.Customer, SubProduct.Product, thisRow.Product)
AddRow(SubProduct, SubProduct.Customer, thisRow.Customer, SubProduct.Product, thisRow.Product)

Which I believe is what you are looking for!

Let me know if that works, thanks,
Bobby

4 Likes

We added a Sequence() formula that can help here too.

So for this example, you could do Sequence(1, [Number of Products], 1).FormulaMap(AddRow(...))

As a sidenote: we’re having lots of discussion on FormulaMap() internally. It’s an incredibly powerful formula, but perhaps not obvious when/how to use it. Would love thoughts on how to improve it?

9 Likes

Hello Shishir,

I imagine there is a reason you didn’t call it “ForEach”, but that is the name that comes to mind to me for what “FormulaMap” does. I think it works great however, thanks!

2 Likes

Good question. We’ve debated that name specifically.

The challenge is that for programmers, ForEach() implies an iterative loop without an explicit return value and it felt like that might be confusing plus we may want to add that capability at some point.

Some names we discussed: ForEach(), ForEvery(), ListTransform(), ListIterate(), Iterate(), Each(), For(), Every(). Another alternative is something like | - e.g. List(...) | Formula().

Would love thoughts!

4 Likes

@shishir
I was going to submit this comment later, but I’ll submit it here, and then also later.

ADOPTION OPTIMIZATION >> Help users translate from the language they know to the language Coda is creating. PUT IT RIGHT IN YOUR DOCUMENTATION.

Exactly what you wrote right here would have saved me hours (multiplied by similar situations with other formulas). If, on your Formulas page, under FormulaMap, there was a note that said, “This is similar to ForEach loops, in the following ways… But it’s not exactly the same because it is different in the following ways…”

I’m working hard to learn Coda, and I would appreciate if your team would go through all of your documentation and make these types of notes where appropriate. :smiley:

2 Likes

Finally, loops!
I never tried to understand formula map. It seemed too complicated, but I’ll have a look on it.
I agree that the formulas documentation could be clearer. It lacks a bit more explanations sometimes.

What about Map, that is established in the functional programming community as well as in javascript? The code I picture myself writing would be something like:

[Some Table].Filter(...).Map(doAction)

I don’t like the piping option, since piping usually refers to the whole thing. So the way I read List(...) | Formula() is that Formula() is processing the whole list at once, and not by element.

1 Like

This is the exact solution for what I need but am a little lost with the intricacies of the LeftPad formula without having access to the example doc. Hopefully it’s still available to view!

I’m trying to populate a table which has rows based on the # of vendors…the FormulaMap function is working but like I said am having trouble with the LeftPad and Split.

I’ve been using Formula Map to add multiple rows to a table, with the Column 1 names drawn from a Multiselect control.

The issue is that I want the “Order #” for each of those rows to incrementally increase… I currently have the following formula, which sets the “Order #” of the row as the MAX value in the table +1… but formula map is not re-calculating the max value as it progresses from row to row. Each row ends up with the same “Order #.”

FormulaMap([Multiselect Control],AddRow([Order Table],[Order Table].[Order #],max([Order #])+1,[Col1],CurrentValue))

Any ideas?

Hello @Steve_Simon,

I’m do something simliar and got stuck in the same place, try doing something like this instead, and iterate through a set of number instead of your list of values. I didn’t test the below but logically it is how I have it working for my doc, thanks!

FormulaMap(Sequence(1,[Multiselect Control].Count()),AddRow([Order Table],[Order Table].[Order #],max([Order #])+CurrentValue,[Col1],[Multiselect Control].Nth(CurrentValue)))

1 Like

I’m still experimenting with this, but would simply “Do()” be an appropriate name for what FomulaMap() does?

I was thinking just as @Bobby_Ritter… trying to get a ForEach-like formula and just couldn’t find anything.
Luckily there’s the community here, so I got the FormulaMap – and it really is powerful.

1 Like

Is there a simple guide for how to use a button to Add Rows from one table to another? Coda support sent me here but the doc in Bobby_Ritter’s example doesn’t work and I am looking for a generic way of doing it rather than response to Lesses’ one.

EDIT: I worked it out. Then I realised I hadn’t. @Pch solution worked best for me by looking at FormulaMap() : When and where? - #4 by Andrei_Kharlanov

Hi Jonathan,

In the link below is an example where ForEach() (Previously FormulaMap() ) is used to copy rows from a table with all numbers, copying all the odd rows to one table, and all the even rows to another table.

I hope it will give you a starting point.
Piet

Here’s also a topic which helped me a lot when I was struggling with FormulaMap()/ForEach() :innocent:

2 Likes

Thanks both, that has helped

3 Likes