mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
1.9 KiB
91 lines
1.9 KiB
6 years ago
|
<!-- https://eugenkiss.github.io/7guis/tasks#crud -->
|
||
|
|
||
|
<script>
|
||
6 years ago
|
let people = [
|
||
5 years ago
|
{ first: 'Hans', last: 'Emil' },
|
||
|
{ first: 'Max', last: 'Mustermann' },
|
||
|
{ first: 'Roman', last: 'Tisch' }
|
||
6 years ago
|
];
|
||
6 years ago
|
|
||
|
let prefix = '';
|
||
|
let first = '';
|
||
|
let last = '';
|
||
|
let i = 0;
|
||
|
|
||
6 years ago
|
$: filteredPeople = prefix
|
||
2 years ago
|
? people.filter((person) => {
|
||
|
const name = `${person.last}, ${person.first}`;
|
||
|
return name.toLowerCase().startsWith(prefix.toLowerCase());
|
||
|
})
|
||
5 years ago
|
: people;
|
||
6 years ago
|
|
||
5 years ago
|
$: selected = filteredPeople[i];
|
||
6 years ago
|
|
||
6 years ago
|
$: reset_inputs(selected);
|
||
6 years ago
|
|
||
6 years ago
|
function create() {
|
||
|
people = people.concat({ first, last });
|
||
|
i = people.length - 1;
|
||
|
first = last = '';
|
||
|
}
|
||
|
|
||
|
function update() {
|
||
5 years ago
|
selected.first = first;
|
||
|
selected.last = last;
|
||
|
people = people;
|
||
6 years ago
|
}
|
||
|
|
||
|
function remove() {
|
||
6 years ago
|
// Remove selected person from the source array (people), not the filtered array
|
||
|
const index = people.indexOf(selected);
|
||
|
people = [...people.slice(0, index), ...people.slice(index + 1)];
|
||
6 years ago
|
|
||
|
first = last = '';
|
||
6 years ago
|
i = Math.min(i, filteredPeople.length - 2);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
function reset_inputs(person) {
|
||
6 years ago
|
first = person ? person.first : '';
|
||
|
last = person ? person.last : '';
|
||
6 years ago
|
}
|
||
|
</script>
|
||
|
|
||
2 years ago
|
<input placeholder="filter prefix" bind:value={prefix} />
|
||
4 years ago
|
|
||
|
<select bind:value={i} size={5}>
|
||
|
{#each filteredPeople as person, i}
|
||
|
<option value={i}>{person.last}, {person.first}</option>
|
||
|
{/each}
|
||
|
</select>
|
||
|
|
||
2 years ago
|
<label><input bind:value={first} placeholder="first" /></label>
|
||
|
<label><input bind:value={last} placeholder="last" /></label>
|
||
4 years ago
|
|
||
2 years ago
|
<div class="buttons">
|
||
|
<button on:click={create} disabled={!first || !last}>create</button>
|
||
|
<button on:click={update} disabled={!first || !last || !selected}>update</button>
|
||
|
<button on:click={remove} disabled={!selected}>delete</button>
|
||
4 years ago
|
</div>
|
||
|
|
||
6 years ago
|
<style>
|
||
|
* {
|
||
|
font-family: inherit;
|
||
|
font-size: inherit;
|
||
|
}
|
||
|
|
||
|
input {
|
||
|
display: block;
|
||
|
margin: 0 0 0.5em 0;
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
float: left;
|
||
|
margin: 0 1em 1em 0;
|
||
|
width: 14em;
|
||
|
}
|
||
|
|
||
|
.buttons {
|
||
|
clear: both;
|
||
|
}
|
||
2 years ago
|
</style>
|