Problems with table field adding/removing rows.
See original GitHub issueQuestion
I’ve been following the Building a multi-row table field video on CraftQuest, and Ive hit a stumbling point with adding/removing rows.
Here’s the example code that I’m using (simplified down to test)
{% set rows = currentUser.testTable %}
<form sprig s-method="post" s-action="users/save-user">
{{ hiddenInput('userId', currentUser.id) }}
{% set rows = rows ?? [['','']] %}
{% if addRow is defined %}
{% set rows = rows|merge([['','']]) %}
{% elseif removeRow is defined %}
{% set rows = rows|filter((val, key) => removeRow != key) %}
{% endif %}
{% for key, row in rows %}
<textarea style="width:100%;" name="fields[testTable][ {{ loop.index0 }} ][col1]">{{ row.col1 ?? '' }}</textarea>
{% if currentUser.hasErrors('testTable') %}
<p>{{ currentUser.getFirstError('testTable')}}</p>
{% endif %}
{% if rows|length > 1 %}
<button sprig s-val:remove-row="{{ loop.index0 }}" type="button">{{ 'Delete'|t }}</button>
{% endif %}
{% endfor %}
<p><button sprig s-val:add-row="1" type="button">+ Add row</button></p>
<input class="btn" type="submit" value="save">
</form>
This seems to work when I perform one action and save. i.e I add one row and save. Remove a row and save.
If I try removing two rows, values get populated in wrong rows. It also won’t let me add tow empty rows? Am I missing something obvious?
Issue Analytics
- State:
- Created 2 years ago
- Comments:13
Top Results From Across the Web
Resize a table by adding or removing rows and columns
Select one or more table rows or table columns that you want to delete. · On the Home tab, in the Cells group,...
Read more >Cannot insert or delete rows in a table
I believe the problem can be resolved by selecting the table rows itself instead of the Excel row. Selecting the Excel rows will...
Read more >Video: Add and delete table rows and columns
When your table needs more data, add rows and columns, or remove them to get rid of empty cells. Add a row. Select...
Read more >Stop Excel users from inserting or deleting rows
Stop Excel users from inserting or deleting rows with 3 methods covering normal protection, only protecting rows and columns, or VBA.
Read more >Add or remove table rows and columns in Pages on Mac
Add or remove header rows and columns Data in header cells isn't used in calculations. Click the table, then in the Format sidebar,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe I was misunderstanding. In the recipe I was assuming that
fields.rows
is the table field ?I set it to
rows
as I had already set it above to the table fieldUpdating it to the following…
…has fixed it 👍
Thanks for taking a look, your help is much appreciated.
My guess is that you haven’t followed the recipe exactly: https://putyourlightson.com/sprig/cookbook#multi-row-table-field
For example, one thing I notice is your
rows
value is incorrectly set:If the issue persists then please start with the recipe and work up from there.