QBEPreviewDialog.vue 5.7 KB
Newer Older
BojanSovticEngIT's avatar
BojanSovticEngIT committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
    <div id="qbe-preview-dialog" class="p-fluid kn-dialog--toolbar--primary">
        <Toolbar class="kn-toolbar kn-toolbar--secondary p-p-0 p-m-0 p-col-12">
            <template #start>
                {{ $t('common.preview') }}
            </template>
            <template #end>
                <i class="fa fa-times kn-cursor-pointer" @click="close"></i>
            </template>
        </Toolbar>

        <DataTable
            id="qbe-preview-datatable"
            class="p-datatable-sm kn-table"
            v-model:first="first"
            :value="rows"
            :paginator="true"
            :lazy="true"
            :rows="20"
            :totalRecords="lazyParams.size"
dbulatovicx32's avatar
dbulatovicx32 committed
21
22
            paginatorTemplate="CurrentPageReport FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink"
            :currentPageReportTemplate="$t('common.table.footer.paginated', { first: '{first}', last: '{last}', totalRecords: '{totalRecords}' })"
BojanSovticEngIT's avatar
BojanSovticEngIT committed
23
24
25
26
27
28
29
30
31
32
33
34
35
            responsiveLayout="stack"
            breakpoint="960px"
            :scrollable="true"
            :scrollHeight="QBEPreviewDialogDescriptor.scrollHeight"
            @page="onPage($event)"
        >
            <template #empty>
                <div>
                    {{ $t('common.info.noDataFound') }}
                </div>
            </template>
            <Column class="kn-truncated" v-for="col of columns" :field="col.dataIndex" :header="col.header" :key="col.field" :sortable="true">
                <template #body="slotProps">
BojanSovticEngIT's avatar
BojanSovticEngIT committed
36
                    {{ col.metawebDateFormat ? getFormattedDate(slotProps.data[col.dataIndex], col) : slotProps.data[col.dataIndex] }}
BojanSovticEngIT's avatar
BojanSovticEngIT committed
37
38
39
40
41
42
43
44
45
46
47
48
                </template>
            </Column>
        </DataTable>
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { formatDate } from '@/helpers/commons/localeHelper'
import Column from 'primevue/column'
import DataTable from 'primevue/datatable'
import QBEPreviewDialogDescriptor from './QBEPreviewDialogDescriptor.json'
BojanSovticEngIT's avatar
BojanSovticEngIT committed
49
import QBEDescriptor from '../../QBEDescriptor.json'
BojanSovticEngIT's avatar
BojanSovticEngIT committed
50
51
52
53

export default defineComponent({
    name: 'qbe-preview-dialog',
    components: { Column, DataTable },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
54
    props: { id: { type: String }, queryPreviewData: { type: Object }, pagination: { type: Object }, entities: { type: Array }, selectedQuery: { type: Object, required: true } },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
55
56
57
58
    emits: ['close', 'pageChanged'],
    data() {
        return {
            QBEPreviewDialogDescriptor,
BojanSovticEngIT's avatar
BojanSovticEngIT committed
59
            QBEDescriptor,
BojanSovticEngIT's avatar
BojanSovticEngIT committed
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
            columns: [] as any[],
            rows: [] as any[],
            lazyParams: {} as any,
            first: 0,
            loading: false
        }
    },
    watch: {
        queryPreviewData() {
            this.loadData()
        },
        pagination() {
            this.loadPagination()
        }
    },
    created() {
        this.loadData()
        this.loadPagination()
    },
    methods: {
        loadData() {
            if (this.queryPreviewData) {
                this.setPreviewColumns(this.queryPreviewData)
                this.rows = this.queryPreviewData.rows
            }
        },
        setPreviewColumns(data: any) {
            this.columns = []
BojanSovticEngIT's avatar
BojanSovticEngIT committed
88

BojanSovticEngIT's avatar
BojanSovticEngIT committed
89
            for (let i = 1; i < data.metaData?.fields?.length; i++) {
BojanSovticEngIT's avatar
BojanSovticEngIT committed
90
91
92
93
                const tempColumn = data.metaData?.fields[i]
                if (['timestamp', 'date'].includes(tempColumn.type)) {
                    const field = this.findField(tempColumn) as any
                    if (field) tempColumn.metawebDateFormat = field.format
BojanSovticEngIT's avatar
BojanSovticEngIT committed
94
95
96
                    if (!tempColumn.metawebDateFormat) {
                        this.setCalculatedFieldDateFormat(tempColumn, i - 1)
                    }
BojanSovticEngIT's avatar
BojanSovticEngIT committed
97
                }
BojanSovticEngIT's avatar
BojanSovticEngIT committed
98
99
100
                this.columns.push(data.metaData?.fields[i])
            }
        },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
101
102
103
104
        setCalculatedFieldDateFormat(tempColumn: any, index: number) {
            if (!this.selectedQuery || !this.selectedQuery.fields) return
            if (this.selectedQuery.fields[index]) tempColumn.metawebDateFormat = this.selectedQuery.fields[index].id.format ?? 'L'
        },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        findField(column: any) {
            if (!this.entities) return

            let field = null

            for (let i = 0; i < this.entities.length; i++) {
                const tempEntity = this.entities[i] as any
                for (let j = 0; j < tempEntity.children.length; j++) {
                    if (tempEntity.children[j].attributes.field === column.header) {
                        field = tempEntity.children[j]
                        break
                    }
                }
            }

            return field
        },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
122
123
124
125
126
127
128
129
130
131
132
133
134
        loadPagination() {
            this.lazyParams = this.pagination as any
            this.first = this.pagination?.start
        },
        onPage(event: any) {
            this.lazyParams = { paginationStart: event.first, paginationLimit: event.rows, paginationEnd: event.first + event.rows, size: this.lazyParams.size }
            this.$emit('pageChanged', this.lazyParams)
        },
        close() {
            this.$emit('close')
            this.first = 0
            this.lazyParams = {}
        },
BojanSovticEngIT's avatar
BojanSovticEngIT committed
135
136
137
138
139
140
141
        getFormattedDate(date: any, column: any) {
            const inputFormat = column.type === 'timestamp' ? 'DD/MM/YYYY HH:mm:ss.SSS' : 'DD/MM/YYYY'
            let format = undefined as string | undefined
            if (QBEDescriptor.admissibleDateFormats.includes(column.metawebDateFormat)) {
                format = column.metawebDateFormat
            }
            return formatDate(date, format, inputFormat)
BojanSovticEngIT's avatar
BojanSovticEngIT committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        }
    }
})
</script>

<style lang="scss">
#qbe-preview-dialog .p-dialog-header,
#qbe-preview-dialog .p-dialog-content {
    padding: 0;
    padding-bottom: 35px;
}

#qbe-preview-dialog .p-dialog-content {
    display: flex;
    flex-direction: column;
    flex: 1;
}
</style>