aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/Multiquicksearch.vue
blob: 19d8068a462fd3818d551bdb694bc78941f753b3 (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
<template>
    <div>
        <ul class="clean multiquicksearch">
            <li v-for="(item, index) in items" :key="index">
                <quicksearch :name="name"
                             :searchtype="searchtype"
                             :autocomplete="autocomplete"
                             :modelValue="autocomplete ? item.item_name : item.item_id"
                             :needle="item.item_name"
                             :ref="'qs_' + index"
                             @update:modelValue="(new_id, new_item_name) => editItem(new_id, new_item_name, index)"></quicksearch>
                <a href="" class="delete_item" @click.prevent="deleteItem(index)">
                    <studip-icon shape="trash" class="text-bottom"></studip-icon>
                </a>
            </li>
        </ul>
        <a href="#" @click.prevent="addItem">
            <studip-icon shape="add" class="text-bottom"></studip-icon>
            {{ addlabel }}
        </a>
    </div>
</template>

<script>
export default {
    name: 'multiquicksearch',
    inheritAttrs: false,
    props: {
        name: {
            type: String,
            required: false
        },
        value: {
            type: Object,
            required: false,
            default: []
        },
        searchtype: {
            type: String,
            required: true
        },
        autocomplete: {
            type: Boolean,
            required: false,
            default: false
        },
        addlabel: {
            type: String,
            required: false,
            default: ""
        }
    },
    data () {
        return {
            items: []
        };
    },
    mounted () {
        for (let i in this.value) {
            this.items.push({
                item_id: this.autocomplete ? this.value[i] : i,
                item_name: this.value[i]
            });
        }
    },
    watch: {
        items: {
            handler(newValue, oldValue) {
                let new_val = {};
                for (let i in newValue) {
                    new_val[newValue[i].item_id] = newValue[i].item_name;
                }
                this.$emit('update:modelValue', new_val);
            },
            deep: true
        }
    },
    methods: {
        addItem: function () {
            this.items.push({
                item_id: '',
                item_name: ''
            });
        },
        editItem: function (item_id, item_name, index) {
            this.items[index].item_id = item_id;
            this.items[index].item_name = item_name;
        },
        deleteItem: function (index) {
            if (this.items.length > 0) {
                this.items.splice(index, 1);
            }
        }
    }
}
</script>