File tree 4 files changed +27
-92
lines changed
01-svelte/06-bindings/06-multiple-select-bindings
4 files changed +27
-92
lines changed Original file line number Diff line number Diff line change 2
2
title : Select multiple
3
3
---
4
4
5
- A select can have a ` multiple ` attribute, in which case it will populate an array rather than selecting a single value.
5
+ A ` < select> ` element can have a ` multiple ` attribute, in which case it will populate an array rather than selecting a single value.
6
6
7
- Returning to our [ earlier ice cream example ] ( /tutorial/group-inputs ) , we can replace the checkboxes with a ` <select multiple> ` :
7
+ Replace the checkboxes with a ` <select multiple> ` :
8
8
9
9
``` svelte
10
10
/// file: App.svelte
11
11
<h2>Flavours</h2>
12
12
13
- <select multiple bind:value={flavours}>
14
- {#each menu as flavour}
15
- <option value={flavour}>
16
- {flavour}
17
- </option>
13
+ +++<select multiple bind:value={flavours}>+++
14
+ {#each ['cookies and cream', 'mint choc chip', 'raspberry ripple'] as flavour}
15
+ +++ <option>{flavour}</option>+++
18
16
{/each}
19
- </select>
17
+ +++ </select>+++
20
18
```
21
19
22
- > Press and hold the ` control ` key (or the ` command ` key on MacOS) for selecting multiple options.
20
+ Note that we're able to omit the ` value ` attribute on the ` <option> ` , since the value is identical to the element's contents.
21
+
22
+ > Press and hold the ` control ` key (or the ` command ` key on MacOS) to select multiple options.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
<script >
2
2
let scoops = 1 ;
3
- let flavours = [' Mint choc chip ' ];
3
+ let flavours = [];
4
4
5
- let menu = [
6
- ' Cookies and cream' ,
7
- ' Mint choc chip' ,
8
- ' Raspberry ripple'
9
- ];
10
-
11
- function join (flavours ) {
12
- if (flavours .length === 1 ) return flavours[0 ];
13
- return ` ${ flavours .slice (0 , - 1 ).join (' , ' )} and ${ flavours[flavours .length - 1 ]} ` ;
14
- }
5
+ const formatter = new Intl.ListFormat (' en' , { style: ' long' , type: ' conjunction' });
15
6
</script >
16
7
17
8
<h2 >Size</h2 >
18
9
19
- <label >
20
- <input type =radio bind:group ={scoops } value ={1 }>
21
- One scoop
22
- </label >
23
-
24
- <label >
25
- <input type =radio bind:group ={scoops } value ={2 }>
26
- Two scoops
27
- </label >
10
+ {#each [1 , 2 , 3 ] as number }
11
+ <label >
12
+ <input
13
+ type =" radio"
14
+ name =" scoops"
15
+ value ={number }
16
+ bind:group ={scoops }
17
+ />
28
18
29
- <label >
30
- <input type =radio bind:group ={scoops } value ={3 }>
31
- Three scoops
32
- </label >
19
+ {number } {number === 1 ? ' scoop' : ' scoops' }
20
+ </label >
21
+ {/each }
33
22
34
23
<h2 >Flavours</h2 >
35
24
36
25
<select multiple bind:value ={flavours }>
37
- {#each menu as flavour }
38
- <option value ={flavour }>
39
- {flavour }
40
- </option >
26
+ {#each [' cookies and cream' , ' mint choc chip' , ' raspberry ripple' ] as flavour }
27
+ <option >{flavour }</option >
41
28
{/each }
42
29
</select >
43
30
48
35
{:else }
49
36
<p >
50
37
You ordered {scoops } {scoops === 1 ? ' scoop' : ' scoops' }
51
- of {join (flavours )}
38
+ of {formatter . format (flavours )}
52
39
</p >
53
- {/if }
40
+ {/if }
Original file line number Diff line number Diff line change 115
115
background : var (--bg-1 );
116
116
}
117
117
118
- select {
118
+ select : not ([ multiple ]) {
119
119
background : var (--bg-2 );
120
120
}
121
121
You can’t perform that action at this time.
0 commit comments