mirror of https://github.com/sveltejs/svelte
parent
5563b3781e
commit
ddea4fcab7
@ -0,0 +1,52 @@
|
||||
<script>
|
||||
let scoops = 1;
|
||||
let flavours = ['Mint choc chip'];
|
||||
|
||||
let menu = [
|
||||
'Cookies and cream',
|
||||
'Mint choc chip',
|
||||
'Raspberry ripple'
|
||||
];
|
||||
|
||||
function join(flavours) {
|
||||
if (flavours.length === 1) return flavours[0];
|
||||
return `${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`;
|
||||
}
|
||||
</script>
|
||||
|
||||
<h2>Size</h2>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={1}>
|
||||
One scoop
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={2}>
|
||||
Two scoops
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={3}>
|
||||
Three scoops
|
||||
</label>
|
||||
|
||||
<h2>Flavours</h2>
|
||||
|
||||
{#each menu as flavour}
|
||||
<label>
|
||||
<input type=checkbox bind:group={flavours} value={flavour}>
|
||||
{flavour}
|
||||
</label>
|
||||
{/each}
|
||||
|
||||
{#if flavours.length === 0}
|
||||
<p>Please select at least one flavour</p>
|
||||
{:else if flavours.length > scoops}
|
||||
<p>Can't order more flavours than scoops!</p>
|
||||
{:else}
|
||||
<p>
|
||||
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
|
||||
of {join(flavours)}
|
||||
</p>
|
||||
{/if}
|
@ -0,0 +1,53 @@
|
||||
<script>
|
||||
let scoops = 1;
|
||||
let flavours = ['Mint choc chip'];
|
||||
|
||||
let menu = [
|
||||
'Cookies and cream',
|
||||
'Mint choc chip',
|
||||
'Raspberry ripple'
|
||||
];
|
||||
|
||||
function join(flavours) {
|
||||
if (flavours.length === 1) return flavours[0];
|
||||
return `${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`;
|
||||
}
|
||||
</script>
|
||||
|
||||
<h2>Size</h2>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={1}>
|
||||
One scoop
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={2}>
|
||||
Two scoops
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<input type=radio bind:group={scoops} value={3}>
|
||||
Three scoops
|
||||
</label>
|
||||
|
||||
<h2>Flavours</h2>
|
||||
|
||||
<select multiple bind:value={flavours}>
|
||||
{#each menu as flavour}
|
||||
<option value={flavour}>
|
||||
{flavour}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
|
||||
{#if flavours.length === 0}
|
||||
<p>Please select at least one flavour</p>
|
||||
{:else if flavours.length > scoops}
|
||||
<p>Can't order more flavours than scoops!</p>
|
||||
{:else}
|
||||
<p>
|
||||
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
|
||||
of {join(flavours)}
|
||||
</p>
|
||||
{/if}
|
@ -0,0 +1,19 @@
|
||||
---
|
||||
title: Select multiple
|
||||
---
|
||||
|
||||
A select can have a `multiple` attribute, in which case it will populate an array rather than selecting a single value.
|
||||
|
||||
Returning to our [earlier ice cream example](tutorial/group-inputs), we can replace the checkboxes with a `<select multiple>`:
|
||||
|
||||
```html
|
||||
<h2>Flavours</h2>
|
||||
|
||||
<select multiple bind:value={flavours}>
|
||||
{#each menu as flavour}
|
||||
<option value={flavour}>
|
||||
{flavour}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
```
|
@ -0,0 +1,39 @@
|
||||
<script>
|
||||
let todos = [
|
||||
{ done: false, text: 'finish Svelte tutorial' },
|
||||
{ done: false, text: 'build an app' },
|
||||
{ done: false, text: 'world domination' }
|
||||
];
|
||||
|
||||
function add() {
|
||||
todos = todos.concat({ done: false, text: '' });
|
||||
}
|
||||
|
||||
function clear() {
|
||||
todos = todos.filter(t => !t.done);
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1>Todos</h1>
|
||||
|
||||
{#each todos as todo}
|
||||
<div>
|
||||
<input
|
||||
type=checkbox
|
||||
bind:checked={todo.done}
|
||||
>
|
||||
|
||||
<input
|
||||
placeholder="What needs to be done?"
|
||||
bind:value={todo.text}
|
||||
>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
<button on:click={add}>
|
||||
Add new
|
||||
</button>
|
||||
|
||||
<button on:click={clear}>
|
||||
Clear completed
|
||||
</button>
|
@ -0,0 +1,7 @@
|
||||
---
|
||||
title: Each block bindings
|
||||
---
|
||||
|
||||
You can even bind to properties inside an `each` block.
|
||||
|
||||
TODO
|
Loading…
Reference in new issue