Home Fetch data from server by Vue.js and jQuery
Post
Cancel

Fetch data from server by Vue.js and jQuery

Overview

This guide provides step-by-step instructions on how to create get data from server by Vue.js & jQuery.

Entrypoint and parameters for task

Example how to create button which by click filling dropdown with new data by Vue.js component:

  1. There are button and dropdown Tasks section
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
const ListComponent = {
    data()  {
        return {
            list: [],
            loading: false,
        }
    },
    template: `
        <button @click="updateList">
            Update list
            <i class="spinner-loader" v-if="loading"></i>
        </button>
        <select class="selectpicker" data-style="btn">
            <option
                v-for="(item, index) in list"
                :value="item"
                :key="index"
            >
                {{ item }}
            </option>
        </select>
    `,
    methods: {
        updateList() {
            this.loading = true;
            ApiFetchItems().then(data => {
                this.list = data;
            }).finally(() => {
                this.loading = false;
            })
        }
    },
}


const ApiFetchItems = async () => {
    const api_url = V.build_api_url('pluginName', 'fileName')
    const res = await fetch(`${api_url}/${getSelectedProjectId()}`, {
        method: 'GET',
    })
    return res.json();
}

Example how to create button which by click filling dropdown with new data by jQuery:

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
<!doctype html>
<html lang="en">
<head>
</head>
<body>
    <button onclick="updateList()">
        Update list
        <i  id="loading" class="spinner-loader d-none"></i>
    </button>
    <select id="selectList" class="selectpicker" data-style="btn">
    </select>
</body>
<script>
    const updateList = () => {
        $('#loading').show();
        ApiFetchItems().then(data => {
            const $select = $('#selectList');
            $select.empty();
            data.forEach(item => {
                const option = `<option value="${item}">${item}</option>`;
                $select.append(option);
            });
            $select.selectpicker('refresh');
        }).finally(() => {
            $('#loading').hide();
        })
    }

    const ApiFetchItems = async () => {
        const api_url = V.build_api_url('pluginName', 'fileName')
        const res = await fetch(`${api_url}/${getSelectedProjectId()}`, {
            method: 'GET',
        })
        return res.json();
    }
</script>
</html>
This post is licensed under CC BY 4.0 by the author.