aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/form_inputs/QuicksearchListInput.vue
blob: d1f1fede5341508c25c9a12cc606d2cfbf50567d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
    <div>
        <quicksearch :searchtype="searchtype"
                     :autocomplete="autocomplete"
                     @update:model-value="addElement"></quicksearch>
        <table v-if="elements.length > 0" ref="results" class="default">
            <tbody>
                <tr v-for="(element, index) in elements"
                    :key="element.id">
                    <td>
                        {{ element.name }}
                    </td>
                    <td class="actions">
                        <a @click="removeElement(index)"
                           :title="$gettext('Dieses Element entfernen')">
                            <studip-icon shape="trash"></studip-icon>
                        </a>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="hidden"
               :name="name"
               :value="realValue"
        >
    </div>
</template>

<script>
import quicksearch from '../Quicksearch.vue';

export default {
    name: 'QuicksearchList',
    components: [ quicksearch ],
    emits: ['update:modelValue'],
    props: {
        name: {
            type: String,
            required: true
        },
        value: {
            type: String,
            default: ''
        },
        searchtype: {
            type: String,
            required: true
        },
        autocomplete: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            elements: [],
        }
    },
    computed: {
        realValue() {
            this.$emit('update:modelValue', JSON.stringify(this.elements));
            return JSON.stringify(this.elements);
        }
    },
    methods: {
        addElement(id, name) {
            if (!this.elements.map(e => e.id).includes(id)) {
                const element = {
                    id: id,
                    name: name
                };
                this.elements.push(element);
            }
        },
        removeElement(index) {
            this.elements.splice(index, 1);
        }
    },
    created() {
        if (this.value !== '') {
            this.elements = JSON.parse(this.value);
        }
    }
}
</script>

<style lang="scss" scoped>
table.default {
    margin-bottom: unset;
    margin-top: 15px;
    width: 50%;

    .actions {
        text-align: right;
    }
}

</style>