mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
470 lines
13 KiB
470 lines
13 KiB
4 years ago
|
<template>
|
||
|
<div class="">
|
||
|
<v-card class="elevation-0">
|
||
|
<v-toolbar flat height="42" class="toolbar-border-bottom">
|
||
|
<v-toolbar-title>
|
||
3 years ago
|
<v-breadcrumbs
|
||
3 years ago
|
:items="[
|
||
|
{
|
||
|
text: nodes.env,
|
||
|
disabled: true,
|
||
|
href: '#',
|
||
|
},
|
||
|
{
|
||
|
text: nodes.dbAlias,
|
||
|
disabled: true,
|
||
|
href: '#',
|
||
|
},
|
||
|
{
|
||
|
text: (nodes.table_name || nodes.view_name) + ' (rows)',
|
||
|
disabled: true,
|
||
|
href: '#',
|
||
|
},
|
||
|
]"
|
||
3 years ago
|
divider=">"
|
||
|
small
|
||
|
>
|
||
|
<template #divider>
|
||
3 years ago
|
<v-icon small color="grey lighten-2"> forward </v-icon>
|
||
4 years ago
|
</template>
|
||
|
</v-breadcrumbs>
|
||
|
</v-toolbar-title>
|
||
3 years ago
|
<v-spacer />
|
||
3 years ago
|
<x-btn v-ge="['rows', 'reload']" outlined tooltip="Reload Rows" color="primary" small @click="refreshTable">
|
||
|
<v-icon small left> refresh </v-icon>
|
||
3 years ago
|
<!-- Reload -->
|
||
|
{{ $t('general.reload') }}
|
||
4 years ago
|
</x-btn>
|
||
3 years ago
|
<x-btn
|
||
|
v-if="!isView"
|
||
3 years ago
|
v-ge="['rows', 'save']"
|
||
3 years ago
|
outlined
|
||
3 years ago
|
:tooltip="$t('tooltip.saveChanges')"
|
||
3 years ago
|
color="primary"
|
||
|
class="primary"
|
||
|
small
|
||
|
:disabled="disableSaveButton"
|
||
|
@click.prevent="saveChanges"
|
||
|
>
|
||
3 years ago
|
<v-icon small left> save </v-icon>
|
||
3 years ago
|
<!-- Save -->
|
||
|
{{ $t('general.save') }}
|
||
4 years ago
|
</x-btn>
|
||
3 years ago
|
<x-btn
|
||
|
v-if="!isView"
|
||
3 years ago
|
v-ge="['rows', 'new']"
|
||
3 years ago
|
outlined
|
||
|
tooltip="Add New Row"
|
||
|
color="primary"
|
||
|
class="primary"
|
||
|
small
|
||
|
@click="addNewRow"
|
||
|
>
|
||
3 years ago
|
<v-icon small left> mdi-plus </v-icon>
|
||
4 years ago
|
New Row
|
||
|
</x-btn>
|
||
|
</v-toolbar>
|
||
|
|
||
|
<v-data-table
|
||
|
:disable-sort="true"
|
||
|
:server-items-length="totalCount"
|
||
|
dense
|
||
|
:headers="cols"
|
||
|
:items="rows"
|
||
|
hide-default-header
|
||
|
:options.sync="tableOptions"
|
||
3 years ago
|
class="column-table"
|
||
4 years ago
|
:footer-props="{
|
||
3 years ago
|
itemsPerPageOptions: [5, 10, 25, 50, 100],
|
||
4 years ago
|
}"
|
||
|
>
|
||
3 years ago
|
<template #header="{ props: { headers } }">
|
||
3 years ago
|
<th v-for="header in headers" :key="header.title" class="py-2 px-1 text-center text-capitalize">
|
||
4 years ago
|
{{ header.text }}
|
||
|
</th>
|
||
|
</template>
|
||
|
|
||
3 years ago
|
<template #item="{ item, index }">
|
||
4 years ago
|
<tr v-if="!item.isNewRow">
|
||
3 years ago
|
<td v-for="({ text, type, ai: ai }, i) in cols" :key="i" class="caption">
|
||
4 years ago
|
<span v-if="isView">
|
||
3 years ago
|
{{ item.data[text] }}
|
||
4 years ago
|
</span>
|
||
|
<template v-else-if="text">
|
||
|
<v-edit-dialog
|
||
3 years ago
|
v-ge="['rows', 'edit-save']"
|
||
4 years ago
|
:return-value.sync="item.data[text]"
|
||
3 years ago
|
@save="save(type, text, item)"
|
||
4 years ago
|
>
|
||
|
{{ item.data[text] }}
|
||
3 years ago
|
<template #input>
|
||
4 years ago
|
<v-text-field
|
||
|
v-model="item.data[text]"
|
||
3 years ago
|
class="mt-0 caption"
|
||
3 years ago
|
:label="$t('general.edit')"
|
||
4 years ago
|
:type="getType(type)"
|
||
|
single-line
|
||
|
hide-details
|
||
|
dense
|
||
|
:disabled="ai"
|
||
3 years ago
|
/>
|
||
4 years ago
|
</template>
|
||
|
</v-edit-dialog>
|
||
|
</template>
|
||
|
<template v-else>
|
||
3 years ago
|
<x-icon
|
||
3 years ago
|
v-ge="['rows', 'delete']"
|
||
3 years ago
|
small
|
||
|
color="error grey"
|
||
|
@click="deleteRow('showDialog', index, item)"
|
||
4 years ago
|
>
|
||
3 years ago
|
mdi-delete-forever
|
||
|
</x-icon>
|
||
4 years ago
|
</template>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr v-else>
|
||
3 years ago
|
<td v-for="({ text, type, ai: ai }, i) in cols" :key="i">
|
||
4 years ago
|
<template v-if="text">
|
||
|
<v-text-field
|
||
3 years ago
|
v-model="item.data[text]"
|
||
3 years ago
|
v-ge="['rows', 'save']"
|
||
4 years ago
|
dense
|
||
|
hide-details
|
||
3 years ago
|
:label="$t('general.edit')"
|
||
4 years ago
|
:type="getType(type)"
|
||
|
:placeholder="text"
|
||
|
:disabled="ai"
|
||
|
single-line
|
||
3 years ago
|
class="caption mt-n1"
|
||
|
@input.passive="save(type, text, item)"
|
||
3 years ago
|
/>
|
||
4 years ago
|
</template>
|
||
|
<template v-else>
|
||
3 years ago
|
<x-icon v-ge="['rows', 'delete']" small color="error" @click="deleteRow('showDialog', index, item)">
|
||
3 years ago
|
mdi-delete-forever
|
||
|
</x-icon>
|
||
4 years ago
|
</template>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</template>
|
||
|
</v-data-table>
|
||
|
</v-card>
|
||
|
<dlgLabelSubmitCancel
|
||
|
v-if="rowDeleteDlg"
|
||
3 years ago
|
type="primary"
|
||
|
:dialog-show="rowDeleteDlg"
|
||
|
:actions-mtd="deleteRow"
|
||
4 years ago
|
heading="Click Submit to Delete the Row"
|
||
|
/>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
3 years ago
|
import { mapGetters } from 'vuex';
|
||
|
import dlgLabelSubmitCancel from '../../utils/DlgLabelSubmitCancel.vue';
|
||
|
import findDataTypeMapping from '../../../helpers/findDataTypeMapping.js';
|
||
4 years ago
|
|
||
|
export default {
|
||
|
components: {
|
||
3 years ago
|
dlgLabelSubmitCancel,
|
||
4 years ago
|
},
|
||
3 years ago
|
data() {
|
||
4 years ago
|
return {
|
||
|
rows: [],
|
||
|
cols: [],
|
||
|
dataTypes: [],
|
||
|
rowDeleteDlg: false,
|
||
|
selectedRowForDelete: null,
|
||
|
client: null,
|
||
|
primaryKeys: [],
|
||
|
test: 'null',
|
||
|
totalCount: 0,
|
||
|
tableOptions: {},
|
||
3 years ago
|
disableSaveButton: true,
|
||
|
};
|
||
4 years ago
|
},
|
||
|
methods: {
|
||
3 years ago
|
async handleKeyDown({ metaKey, key, altKey, shiftKey, ctrlKey }) {
|
||
3 years ago
|
console.log(metaKey, key, altKey, shiftKey, ctrlKey);
|
||
4 years ago
|
// cmd + s -> save
|
||
|
// cmd + l -> reload
|
||
|
// cmd + n -> new
|
||
|
// cmd + d -> delete
|
||
|
// cmd + enter -> send api
|
||
|
|
||
|
switch ([metaKey, key].join('_')) {
|
||
3 years ago
|
case 'true_s':
|
||
|
await this.saveChanges();
|
||
|
break;
|
||
|
case 'true_l':
|
||
|
await this.refreshTable();
|
||
|
break;
|
||
|
case 'true_n':
|
||
|
this.addNewRow();
|
||
|
break;
|
||
4 years ago
|
// case 'true_d' :
|
||
|
// await this.deleteTable('showDialog');
|
||
|
// break;
|
||
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
async refreshTable() {
|
||
4 years ago
|
try {
|
||
3 years ago
|
await this.loadColumnList();
|
||
|
await this.loadRowList();
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
this.$toast.error('error loading rows').goAway(4000);
|
||
|
throw e;
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
async loadColumnList() {
|
||
3 years ago
|
// const columnsList = await this.client.columnList({tn: this.nodes.table_name});
|
||
4 years ago
|
|
||
|
// const columnsList = await this.sqlMgr.sqlOp({
|
||
|
// env: this.nodes.env,
|
||
|
// dbAlias: this.nodes.dbAlias
|
||
3 years ago
|
// }, 'columnList', {tn: this.nodes.table_name})
|
||
4 years ago
|
|
||
3 years ago
|
const columnsList = await this.$store.dispatch('sqlMgr/ActSqlOp', [
|
||
|
{
|
||
|
env: this.nodes.env,
|
||
|
dbAlias: this.nodes.dbAlias,
|
||
|
},
|
||
|
'columnList',
|
||
|
{ tn: this.nodes.table_name || this.nodes.view_name },
|
||
|
]);
|
||
4 years ago
|
|
||
3 years ago
|
this.cols = columnsList.data.list.map(({ cn, dt, ai, pk }) => {
|
||
3 years ago
|
if (pk) {
|
||
|
this.primaryKeys.push(cn);
|
||
4 years ago
|
}
|
||
3 years ago
|
return {
|
||
|
text: cn,
|
||
|
value: cn,
|
||
|
type: findDataTypeMapping(dt),
|
||
|
ai,
|
||
|
};
|
||
|
});
|
||
4 years ago
|
// column for action buttons
|
||
3 years ago
|
if (!this.isView) {
|
||
|
this.cols.push({ text: '', value: '' });
|
||
|
}
|
||
4 years ago
|
},
|
||
3 years ago
|
async loadRowList() {
|
||
3 years ago
|
const { page, itemsPerPage } = this.tableOptions;
|
||
4 years ago
|
|
||
|
// const result = await this.sqlMgr.sqlOp({
|
||
|
// env: this.nodes.env,
|
||
|
// dbAlias: this.nodes.dbAlias
|
||
|
// }, 'list', {
|
||
3 years ago
|
// tn: this.nodes.table_name,
|
||
4 years ago
|
// size: itemsPerPage,
|
||
|
// page,
|
||
|
// orderBy: this.primaryKeys.map(key => ({column: key, order: 'desc'}))
|
||
|
// })
|
||
|
|
||
3 years ago
|
const result = await this.$store.dispatch('sqlMgr/ActSqlOp', [
|
||
|
{
|
||
|
env: this.nodes.env,
|
||
|
dbAlias: this.nodes.dbAlias,
|
||
|
},
|
||
|
'list',
|
||
|
{
|
||
|
tn: this.nodes.table_name || this.nodes.view_name,
|
||
|
size: itemsPerPage,
|
||
|
page,
|
||
|
orderBy: this.primaryKeys.map(key => ({ column: key, order: 'desc' })),
|
||
|
},
|
||
|
]);
|
||
4 years ago
|
|
||
|
// const result = await this.client.list({
|
||
3 years ago
|
// tn: this.nodes.table_name,
|
||
4 years ago
|
// size: itemsPerPage,
|
||
|
// page,
|
||
|
// orderBy: this.primaryKeys.map(key => ({column: key, order: 'desc'}))
|
||
|
// });
|
||
|
|
||
3 years ago
|
this.totalCount = result.data.count;
|
||
4 years ago
|
|
||
3 years ago
|
this.rows = result.data.list.map(row => {
|
||
|
const keys = this.primaryKeys.reduce((obj, key) => Object.assign(obj, { [key]: row[key] }), {});
|
||
|
return { keys, data: { ...row }, dataCopy: { ...row } };
|
||
|
});
|
||
4 years ago
|
},
|
||
3 years ago
|
save(type, text, item) {
|
||
4 years ago
|
// console.log(type, text, item)
|
||
3 years ago
|
item.changed = true;
|
||
|
const { data, dataCopy } = item;
|
||
4 years ago
|
if (type === 'date') {
|
||
3 years ago
|
dataCopy[text] = new Date(data[text]);
|
||
4 years ago
|
} else {
|
||
3 years ago
|
dataCopy[text] = data[text];
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
getType(dt) {
|
||
3 years ago
|
if (dt === 'number') {
|
||
|
return 'number';
|
||
|
}
|
||
|
return 'text';
|
||
4 years ago
|
},
|
||
3 years ago
|
async deleteRow(action = '', index, row) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
if (action === 'showDialog') {
|
||
3 years ago
|
this.rowDeleteDlg = true;
|
||
|
this.selectedRowForDelete = { index, row };
|
||
3 years ago
|
} else if (action === 'hideDialog') {
|
||
3 years ago
|
this.rowDeleteDlg = false;
|
||
4 years ago
|
} else {
|
||
3 years ago
|
this.rowDeleteDlg = false;
|
||
|
const { index, row } = this.selectedRowForDelete;
|
||
4 years ago
|
if (row.isNewRow) {
|
||
3 years ago
|
this.rows.splice(index, 1);
|
||
4 years ago
|
} else {
|
||
3 years ago
|
// this.client.delete(this.nodes.table_name, row.keys)
|
||
4 years ago
|
|
||
3 years ago
|
await this.$store.dispatch('sqlMgr/ActSqlOp', [
|
||
|
{
|
||
|
env: this.nodes.env,
|
||
|
dbAlias: this.nodes.dbAlias,
|
||
|
},
|
||
|
'delete',
|
||
|
{
|
||
|
tn: this.nodes.table_name || this.nodes.view_name,
|
||
|
whereConditions: row.keys,
|
||
|
},
|
||
|
]);
|
||
|
this.rows.splice(index, 1);
|
||
4 years ago
|
}
|
||
3 years ago
|
this.$toast.success('Row deleted successfully').goAway(3000);
|
||
4 years ago
|
}
|
||
3 years ago
|
this.totalCount--;
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
this.$toast.error('Deleting row failed').goAway(3000);
|
||
|
throw e;
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
addNewRow() {
|
||
3 years ago
|
this.rows.unshift({ isNewRow: true, data: {}, dataCopy: {} });
|
||
|
this.totalCount++;
|
||
4 years ago
|
},
|
||
3 years ago
|
async saveChanges() {
|
||
4 years ago
|
try {
|
||
3 years ago
|
const newRows = [];
|
||
|
this.rows.map(async item => {
|
||
|
const { keys, isNewRow, changed, dataCopy } = item;
|
||
4 years ago
|
if (isNewRow) {
|
||
3 years ago
|
newRows.push(dataCopy);
|
||
4 years ago
|
} else if (changed) {
|
||
3 years ago
|
item.changed = false;
|
||
3 years ago
|
// const res = await this.client.update(this.nodes.table_name, dataCopy, keys);
|
||
4 years ago
|
|
||
3 years ago
|
const res = await this.$store.dispatch('sqlMgr/ActSqlOp', [
|
||
|
{
|
||
|
env: this.nodes.env,
|
||
|
dbAlias: this.nodes.dbAlias,
|
||
|
},
|
||
|
'update',
|
||
|
{
|
||
|
tn: this.nodes.table_name,
|
||
|
data: dataCopy,
|
||
|
whereConditions: keys,
|
||
|
},
|
||
|
]);
|
||
4 years ago
|
|
||
3 years ago
|
console.log(res);
|
||
|
this.$toast.success('Row updated successfully').goAway(3000);
|
||
4 years ago
|
}
|
||
3 years ago
|
});
|
||
4 years ago
|
if (newRows.length) {
|
||
3 years ago
|
console.log(newRows);
|
||
4 years ago
|
// todo : multiargs to single object
|
||
3 years ago
|
// const res = await this.client.insert(this.nodes.table_name, newRows)
|
||
4 years ago
|
|
||
3 years ago
|
const res = await this.$store.dispatch('sqlMgr/ActSqlOp', [
|
||
|
{
|
||
|
env: this.nodes.env,
|
||
|
dbAlias: this.nodes.dbAlias,
|
||
|
},
|
||
|
'insert',
|
||
|
{
|
||
|
tn: this.nodes.table_name,
|
||
|
data: newRows,
|
||
|
},
|
||
|
]);
|
||
4 years ago
|
|
||
3 years ago
|
console.log(res);
|
||
4 years ago
|
}
|
||
3 years ago
|
this.$toast.success('Row saved successfully').goAway(3000);
|
||
|
this.loadRowList();
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log(e);
|
||
|
this.$toast.error('Saving changes to table failed').goAway(3000);
|
||
|
throw e;
|
||
4 years ago
|
}
|
||
3 years ago
|
},
|
||
4 years ago
|
},
|
||
|
computed: {
|
||
3 years ago
|
...mapGetters({ sqlMgr: 'sqlMgr/sqlMgr' }),
|
||
3 years ago
|
isView() {
|
||
3 years ago
|
return !!this.nodes.view_name;
|
||
|
},
|
||
4 years ago
|
},
|
||
3 years ago
|
beforeCreated() {},
|
||
4 years ago
|
watch: {
|
||
|
tableOptions: {
|
||
3 years ago
|
handler() {
|
||
3 years ago
|
this.loadRowList();
|
||
4 years ago
|
},
|
||
3 years ago
|
deep: true,
|
||
4 years ago
|
},
|
||
|
rows: {
|
||
3 years ago
|
handler() {
|
||
3 years ago
|
this.disableSaveButton = !this.rows.some(row => row.isNewRow || row.changed);
|
||
4 years ago
|
},
|
||
3 years ago
|
deep: true,
|
||
|
},
|
||
4 years ago
|
},
|
||
3 years ago
|
async created() {
|
||
3 years ago
|
await this.refreshTable();
|
||
3 years ago
|
},
|
||
3 years ago
|
mounted() {},
|
||
|
beforeDestroy() {},
|
||
|
destroy() {},
|
||
3 years ago
|
directives: {},
|
||
3 years ago
|
validate({ params }) {
|
||
3 years ago
|
return true;
|
||
3 years ago
|
},
|
||
3 years ago
|
head() {
|
||
3 years ago
|
return {};
|
||
3 years ago
|
},
|
||
3 years ago
|
props: ['nodes', 'newTable', 'mtdNewTableUpdate', 'deleteTable'],
|
||
|
};
|
||
4 years ago
|
</script>
|
||
|
|
||
3 years ago
|
<style scoped></style>
|
||
4 years ago
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|