Browse Source

fix: group by new record & add new row default value is not reflecting immediately

pull/7218/head
Ramesh Mane 1 year ago
parent
commit
5288d991e8
  1. 5
      packages/nc-gui/components/smartsheet/grid/GroupByTable.vue
  2. 14
      packages/nc-gui/components/smartsheet/grid/Table.vue
  3. 5
      packages/nc-gui/composables/useData.ts
  4. 22
      packages/nc-gui/utils/dataUtils.ts

5
packages/nc-gui/components/smartsheet/grid/GroupByTable.vue

@ -1,7 +1,7 @@
<script lang="ts" setup>
import { UITypes, isLinksOrLTAR } from 'nocodb-sdk'
import Table from './Table.vue'
import { IsGroupByInj, computed, ref } from '#imports'
import { IsGroupByInj, computed, ref, rowDefaultData } from '#imports'
import type { Group, Row } from '#imports'
const props = defineProps<{
@ -38,7 +38,7 @@ const view = inject(ActiveViewInj, ref())
const reloadViewDataHook = inject(ReloadViewDataHookInj, createEventHook())
function addEmptyRow(group: Group, addAfter?: number) {
function addEmptyRow(group: Group, addAfter?: number, metaValue = meta.value) {
if (group.nested || !group.rows) return
addAfter = addAfter ?? group.rows.length
@ -57,6 +57,7 @@ function addEmptyRow(group: Group, addAfter?: number) {
group.rows.splice(addAfter, 0, {
row: {
...rowDefaultData(metaValue?.columns),
...setGroup,
},
oldRow: {},

14
packages/nc-gui/components/smartsheet/grid/Table.vue

@ -448,7 +448,8 @@ const closeAddColumnDropdown = (scrollToLastCol = false) => {
}
async function openNewRecordHandler() {
const newRow = addEmptyRow()
// skip update row when it is `New record form`
const newRow = addEmptyRow(dataRef.value.length, true)
if (newRow) expandForm?.(newRow, undefined, true)
}
@ -697,8 +698,17 @@ function scrollToRow(row?: number) {
scrollToCell?.()
}
function addEmptyRow(row?: number) {
async function saveEmptyRow(rowObj: Row) {
await updateOrSaveRow?.(rowObj)
}
function addEmptyRow(row?: number, skipUpdate: boolean = false) {
const rowObj = callAddEmptyRow?.(row)
if (!skipUpdate && rowObj) {
saveEmptyRow(rowObj)
}
nextTick().then(() => {
scrollToRow(row ?? dataRef.value.length - 1)
})

5
packages/nc-gui/composables/useData.ts

@ -16,6 +16,7 @@ import {
useI18n,
useMetas,
useNuxtApp,
rowDefaultData,
} from '#imports'
import type { CellRange, Row, UndoRedoAction } from '#imports'
@ -53,9 +54,9 @@ export function useData(args: {
},
})
function addEmptyRow(addAfter = formattedData.value.length) {
function addEmptyRow(addAfter = formattedData.value.length, metaValue = meta.value) {
formattedData.value.splice(addAfter, 0, {
row: {},
row: { ...rowDefaultData(metaValue?.columns) },
oldRow: {},
rowMeta: { new: true },
})

22
packages/nc-gui/utils/dataUtils.ts

@ -1,4 +1,4 @@
import { RelationTypes, UITypes } from 'nocodb-sdk'
import { RelationTypes, UITypes, isLinksOrLTAR, isSystemColumn, isVirtualCol } from 'nocodb-sdk'
import type { ColumnType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk'
import type { Row } from 'lib'
import { isColumnRequiredAndNull } from './columnUtils'
@ -86,3 +86,23 @@ export async function populateInsertObject({
return { missingRequiredColumns, insertObj }
}
// a function to get default values of row
export const rowDefaultData = (columns: ColumnType[] = []) => {
const defaultData: Record<string, string> = columns.reduce<Record<string, any>>((acc: Record<string, any>, col: ColumnType) => {
// avoid setting default value for system col, virtual col, rollup, formula, barcode, qrcode, links, ltar
if (
!isSystemColumn(col) &&
!isVirtualCol(col) &&
!isLinksOrLTAR({ uidt: col.uidt! }) &&
![UITypes.Rollup, UITypes.Lookup, UITypes.Formula, UITypes.Barcode, UITypes.QrCode].includes(col.uidt) &&
col?.cdf
) {
const defalutValue = col.cdf
acc[col.title!] = typeof defalutValue === 'string' ? defalutValue.replace(/^'/, '').replace(/'$/, '') : defalutValue
}
return acc
}, {} as Record<string, any>)
return defaultData
}

Loading…
Cancel
Save