Delete row
Each row of an invoice table has a Delete button. A click asks for confirmation, sends DELETE to the server, and
removes the row when the server answers.
Try it
Live demo
| Customer | Amount | Actions |
|---|---|---|
| Harbor Coffee Roasters | $1,280.00 | |
| Linden Street Books | $342.50 | |
| Oak & Anvil Woodworks | $2,975.00 | |
| Blue Heron Dental | $610.00 |
Real requests to this site. What you change is kept in a cookie of your own for an hour and is invisible to every other visitor.
Delete a few rows, reload this page and they stay deleted, for you only. Restore the rows puts them back.
The page
<table>
<thead><tr><th>Customer</th><th>Amount</th><th></th></tr></thead>
<tbody>
<tr>
<td>Harbor Coffee Roasters</td>
<td>$1,280.00</td>
<td>
<form ypy-delete="/invoices/1" ypy-headers='{"X-CSRF-Token": "…"}'
ypy-confirm="Delete the invoice for Harbor Coffee Roasters?"
ypy-target="closest tr" ypy-swap="delete">
<button>Delete</button>
</form>
</td>
</tr>
</tbody>
</table>What the server answers
DELETE /invoices/1, with the header X-CSRF-Token, answers 200 with an empty body and Vary: YPY-Request.
| Case | Status | Body |
|---|---|---|
| Deleted | 200 |
Empty |
| Already gone or unknown id | 404 |
Empty |
| No valid CSRF token | 419 |
The framework's refusal; the row stays |
Use 200, not 204: YPY does not swap a 204, and a swap is what removes the row.
How it works
ypy-confirmasks before anything is sent. Cancel the dialog and no request leaves the page.ypy-target="closest tr"reaches the row from the button, so the rows need no ids.ypy-swap="delete"removes the target whatever the answer holds, so an empty200is enough.-
A
DELETEhas no body, so a hidden_tokenfield cannot reach the server.ypy-headerssends the token as theX-CSRF-Tokenheader instead; theypy-csrfextension does the same for every request from one<meta>tag. See CSRF. - An answer of
419or404is an error, not a success, so the row stays on the page.
Variations
-
Fade the row out first with a CSS transition:
ypy-swap="delete swap:300ms"waits 300 ms while the target carries theypy-swappingclass. See Animations. -
Put the confirmation in a dialog with the
ypy-promptextension, or see Confirm and prompt.