在Ag-Grid中获取数据

时间:2019-11-10 22:16:04

标签: vue.js ag-grid

我能够使用bootstrap-vue将数据拉入数据库。

我需要转移到ag-grid,但我不知道该怎么做

有人帮我,这是ag-grid中的代码:

<template v-if="instituicao">
    <div>
        <button @click="getSelectedRows()">Get Selected Rows</button>
        <ag-grid-vue style="width: 1000px; height: 500px;" 
            class="ag-theme-balham"
            v-model="instituicao.codigo" required
            :floatingFilter="true" 
            :gridReady="onGridReady" 
            :enableColResize="true" 
            :columnDefs="columnDefs" 
            :rowData="rowData" 
            :enableSorting="true" 
            :enableFilter="true">
        </ag-grid-vue>
    </div>
</template>

<script>
import PageTitle from '../template/PageTitle'
import axios from 'axios'
import { baseApiUrl, showError } from '@/global'
import { AgGridVue } from "ag-grid-vue"
import { transformHeader, transformRows } from '../../lib/grid.js'
/* import "ag-grid-enterprise" */

export default {
    name: 'InstituicaoAdmin',
    components: { AgGridVue, PageTitle },
    data() {
        return {
            columnDefs: null,
            rowData: null,
            mode: 'save',
            instituicao: {},
            instituicoes: [],
            fields: [
                { key: 'id', label: 'Id', sortable: true },
                { key: 'name', label: 'Name', sortable: true }
            ],
        }
    },
    methods: {
        onGridReady(params) {
            this.gridApi = params.api;
            this.columnApi = params.columnApi;
            this.gridApi.setHeaderHeight(50);
        },
            sizeToFit() {
            this.gridApi.sizeColumnsToFit();
            // this.autosizeHeaders();
        },
        autoSizeAll() {
            var allColumnIds = [];
            this.columnApi.getAllColumns().forEach(function(column) {
                allColumnIds.push(column.colId);
            });
            this.columnApi.autoSizeColumns(allColumnIds);
        },
        getSelectedRows() {
            const selectedNodes = this.gridApi.getSelectedNodes();
            const selectedData = selectedNodes.map(node => node.data);
            const selectedDataStringPresentation = selectedData.map(node => node.make + ' ' + node.model).join(', ');
            alert(`Selected nodes: ${selectedDataStringPresentation}`);
        },
        loadInstituicoes() {
            const url = `${baseApiUrl}/instituicao`
            axios.get(url).then(res => {
                this.instituicoes = res.data
            })
        },
        reset() {
            this.mode = 'save'
            this.instituicao = {}
            this.loadInstituicoes()
        },
        save() {
            const method = this.instituicao.id ? 'put' : 'post'
            const id = this.instituicao.id ? `/${this.instituicao.id}` : ''
            axios[method](`${baseApiUrl}/instituicao${id}`, this.instituicao)
                .then(() => {
                    this.$toasted.global.defaultSuccess()
                    this.reset()
                })
                .catch(showError)
        },
        remove() {
            const id = this.instituicao.id
            axios.delete(`${baseApiUrl}/instituicao/${id}`)
                .then(() => {
                    this.$toasted.global.defaultSuccess()
                    this.reset()
                })
                .catch(showError)
        },
        loadInstituicao(instituicao, mode = 'save') {
            this.mode = mode
            this.instituicao = { ...instituicao }
        }
    },
    beforeMount() {
        this.columnDefs = [
            {headerName: 'Id', field: 'id', sortable: true },
            {headerName: 'Name', field: 'name', sortable: true, filter: true },
            {headerName: 'Price', field: 'price', sortable: true, filter: true }
        ];

        fetch('https://api.myjson.com/bins/15psn9')
            .then(result => result.json())
            .then(rowData => this.rowData = rowData);
    },
    computed: {
        columnDefs() {
        return transformHeader(this.payload.header);
        },
        rowData() {
        return transformRows(this.payload.data.rows, this.columnDefs);
        }
    },    
    mounted() {
        this.loadInstituicoes()
    }
}
</script>

<style>
  @import "../../../node_modules/ag-grid/dist/styles/ag-grid.css";
  @import "../../../node_modules/ag-grid/dist/styles/ag-theme-balham.css";
</style>

我的问题是如何使用ag-grid框架执行GET,PUT,POST和DELETE操作。

Usando o bootstrap-vue,eu poderia executar ou obter o banco de dadosatravésda propriedade“ fields”。

<script>
import PageTitle from '../template/PageTitle'
import axios from 'axios'
import { baseApiUrl, showError } from '@/global'

export default {
    name: 'InstituicaoAdmin',
    components: { PageTitle },
    data: function() {
        return {
            mode: 'save',
            instituicao: {},
            instituicoes: [],
            fields: [
                { key: 'id', label: 'Id', sortable: true },
                { key: 'name', label: 'Name', sortable: true }
            ]
        }
    },
    methods: {
        loadInstituicoes() {
            const url = `${baseApiUrl}/instituicao`
            axios.get(url).then(res => {
                this.instituicoes = res.data
            })
        },
        reset() {
            this.mode = 'save'
            this.instituicao = {}
            this.loadInstituicoes()
        },
        save() {
            const method = this.instituicao.id ? 'put' : 'post'
            const id = this.instituicao.id ? `/${this.instituicao.id}` : ''
            axios[method](`${baseApiUrl}/instituicao${id}`, this.instituicao)
                .then(() => {
                    this.$toasted.global.defaultSuccess()
                    this.reset()
                })
                .catch(showError)
        },
        remove() {
            const id = this.instituicao.id
            axios.delete(`${baseApiUrl}/instituicao/${id}`)
                .then(() => {
                    this.$toasted.global.defaultSuccess()
                    this.reset()
                })
                .catch(showError)
        },
        loadInstituicao(instituicao, mode = 'save') {
            this.mode = mode
            this.instituicao = { ...instituicao }
        }
    },
    mounted() {
        this.loadInstituicoes()
    }
}
</script>

1 个答案:

答案 0 :(得分:0)

我能够使用axios通过解构来解决问题:

    beforeMount() {
        this.columnDefs = [
            {headerName: 'Id', field: 'id', editable: true},
            {headerName: 'Name', field: 'name', editable: true}
        ];
        axios.get(`${baseApiUrl}/instituicao`)
            .then(({data}) => this.rowData = data)
    },