question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with table field adding/removing rows.

See original GitHub issue

Question

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:closed
  • Created 2 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
shornukcommented, Aug 31, 2021

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 field

{% set rows = currentUser.testTable %}

Updating it to the following…

{% set rows = fields.testTable ?? [defaultRow] %}

…has fixed it 👍

Thanks for taking a look, your help is much appreciated.

0reactions
bencrokercommented, Aug 31, 2021

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:

Yours:
{% set rows = rows ?? [defaultRow] %}

The recipe:
{% set rows = fields.rows ?? [defaultRow] %}

If the issue persists then please start with the recipe and work up from there.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found