Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/vue/src/grid/src/composable/useCellEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getEventSource = (e, $table) => {
if (tableEl.dataset?.tableid !== String($table.id)) return

let cellEl = target.closest('.tiny-grid-header__column')
let rowEl, part, rowType, row, column
let rowEl, part, rowType, row, column, rowid

if (cellEl) {
rowEl = cellEl.parentNode
Expand All @@ -27,12 +27,19 @@ const getEventSource = (e, $table) => {
if (!part || !cellEl || !rowEl) return

column = $table.getColumnNode(cellEl)?.item
rowid = rowEl.dataset?.rowid

if (rowEl.dataset?.rowid?.startsWith('row_g_')) {
if (rowid?.startsWith('row_g_')) {
rowType = 'virtual'
} else if (part === 'body') {
rowType = 'normal'
row = $table.getRowNode(rowEl)?.item

if ($table.editStore.insertMap.has(rowid)) {
// 新增行不进缓存,需要单独查找
row = $table.editStore.insertMap.get(rowid)
} else {
row = $table.getRowNode(rowEl)?.item
}
}

return { part, rowType, row, column, cell: cellEl, tr: rowEl }
Expand Down
111 changes: 24 additions & 87 deletions packages/vue/src/grid/src/edit/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ import { debounce } from '@opentiny/utils'
import { hooks } from '@opentiny/vue-common'
import { addClass } from '@opentiny/utils'
import { browserInfo } from '@opentiny/utils'
import {
isArray,
destructuring,
set,
get,
remove,
filterTree,
find,
clone
} from '@opentiny/vue-renderless/grid/static/'
import { isArray, destructuring, set, get, remove, filterTree, find } from '@opentiny/vue-renderless/grid/static/'
import { getCell, getCellValue, setCellValue, emitEvent, getRowid } from '@opentiny/vue-renderless/grid/utils'
import { Renderer } from '../../adapter'
import { error, warn } from '../../tools'
Expand All @@ -48,53 +39,27 @@ import {
handleActivedTryActive
} from './utils/handleActived'

function operArrs({
_vm,
editStore,
newRecords,
newRecordsCopy,
nowData,
row,
tableFullData,
tableSourceData,
rawData
}) {
function operArrs({ _vm, editStore, newRecords, row, tableFullData }) {
if (row === -1) {
Array.prototype.push.apply(nowData, newRecords)
Array.prototype.push.apply(tableFullData, newRecords)
Array.prototype.push.apply(tableSourceData, newRecordsCopy)
Array.prototype.push.apply(rawData, newRecords)
}

if (row && row !== -1) {
let targetIndex = nowData.indexOf(row)
let insertIndex = tableFullData.indexOf(row)

if (targetIndex === -1) {
if (insertIndex === -1) {
throw new Error(error('ui.grid.error.unableInsert'))
}

let insertIndex = tableFullData.indexOf(row)

Array.prototype.splice.apply(nowData, [targetIndex, 0].concat(newRecords))
Array.prototype.splice.apply(tableFullData, [insertIndex, 0].concat(newRecords))
Array.prototype.splice.apply(tableSourceData, [insertIndex, 0].concat(newRecordsCopy))

let rawInsertIndex = rawData.indexOf(row)

if (rawInsertIndex > -1) {
Array.prototype.splice.apply(rawData, [rawInsertIndex, 0].concat(newRecords))
}
}

if (!row) {
Array.prototype.unshift.apply(nowData, newRecords)
Array.prototype.unshift.apply(tableFullData, newRecords)
Array.prototype.unshift.apply(tableSourceData, newRecordsCopy)
Array.prototype.unshift.apply(rawData, newRecords)
}

Array.prototype.unshift.apply(editStore.insertList, newRecords)
Array.prototype.push.apply(_vm.temporaryRows, newRecordsCopy)
newRecords.forEach((row) => editStore.insertMap.set(getRowid(_vm, row), row))
}

export function removeFromTableSourceData({ _vm, rows, tableSourceData }) {
Expand Down Expand Up @@ -147,53 +112,33 @@ export default {
},
// 根据位置从指定行添加数据
_insertAt(records, row) {
let {
afterFullData,
editStore,
isAsyncColumn,
scrollYLoad,
tableFullData,
tableSourceData = [],
treeConfig,
rawData
} = this
let { editStore, isAsyncColumn, scrollYLoad, tableFullData, treeConfig, visibleColumn } = this

if (treeConfig) {
throw new Error(error('ui.grid.error.treeInsert'))
}

// 增加新增标识
if (isAsyncColumn) {
const columnSet = this.getColumns()

columnSet.forEach((column) => {
if (column.format && column.format.async && column.format.async.fetch) {
records[GlobalConfig.constant.insertedField] = true
}
})
}

if (!isArray(records)) {
records = [records]
}

let nowData = afterFullData
let newRecords = records.map((record) => hooks.reactive(this.defineField({ ...record })))
let newRecordsCopy = clone(newRecords, true)
const isColumnFormat = isAsyncColumn && visibleColumn.some((column) => column.format?.async?.fetch)

let newRecords = records.map((record) => {
// 增加新增标识
isColumnFormat && (record[GlobalConfig.constant.insertedField] = true)
// 增加编辑字段和主键字段
return hooks.reactive(this.defineField(Object.assign({}, record)))
})

operArrs({
_vm: this,
editStore,
newRecords,
newRecordsCopy,
nowData,
row,
tableFullData,
tableSourceData,
rawData
newRecords
})

this.updateCache()
this.handleTableData(true)
this.checkSelectionStatus()
this.updateFooter()
Expand All @@ -216,9 +161,9 @@ export default {
* 如果传 rows 则删除多行
*/
_remove(rows) {
let { afterFullData, scrollYLoad, selectConfig = {} } = this
let { selection, tableFullData, treeConfig, tableSourceData = [] } = this
let { insertList, removeList } = this.editStore
let { afterFullData, selectConfig = {} } = this
let { selection, tableFullData, treeConfig } = this
let { insertList, insertMap, removeList } = this.editStore
let { checkField } = selectConfig
let nowData = afterFullData
let rest = []
Expand All @@ -240,7 +185,7 @@ export default {
// 如果不是新增,则保存记录
for (let i = 0; i < rows.length; i++) {
let row = rows[i]
if (this.hasRowInsert(row)) continue
if (this.isTemporaryRow(row)) continue
removeList.push(row)
}

Expand All @@ -259,25 +204,17 @@ export default {
remove(nowData, (row) => inArr(row, rows))
}

// 从备份中移除新增数据
remove(
tableSourceData,
(row) =>
find(insertList, (r) => getRowid(this, r) === getRowid(this, row)) &&
find(rows, (r) => getRowid(this, r) === getRowid(this, row))
)

// 从新增中移除已删除的数据
remove(insertList, (row) => inArr(row, rows))
remove(insertList, (row) => {
const exist = inArr(row, rows)
exist && insertMap.delete(getRowid(this, row))
return exist
})

// 修改缓存
this.updateCache()
this.handleTableData(true)
this.checkSelectionStatus()
this.updateFooter()
if (scrollYLoad) {
this.updateScrollYSpace()
}

let res = {
row: rows && rows.length ? rows[rows.length - 1] : null,
Expand Down
7 changes: 4 additions & 3 deletions packages/vue/src/grid/src/table/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ const Methods = {
const scrollYLoad = scrollY && scrollY.gt > 0 && scrollY.gt <= tableFullData.length

editStore.insertList = []
editStore.insertMap = new Map()
editStore.removeList = []
// 设置全量数据,原始数据,行虚滚标记
Object.assign(this, { tableFullData, tableSynchData: datas, scrollYLoad })
Expand Down Expand Up @@ -326,7 +327,7 @@ const Methods = {
})
},
updateRawData(datas) {
this.rawData = [...datas]
this.rawData = datas
this.rawDataVersion += 1
},
getOriginRow(row) {
Expand Down Expand Up @@ -617,7 +618,7 @@ const Methods = {
isTemporaryRow(row) {
const rowid = getRowid(this, row)

return find(this.temporaryRows, (r) => rowid === getRowid(this, r))
return this.editStore.insertMap.has(rowid)
},
createData(records, copy) {
const isArr = isArray(records)
Expand Down Expand Up @@ -659,7 +660,7 @@ const Methods = {
return this.$nextTick()
},
hasRowInsert(row) {
return this.editStore.insertList.includes(row)
return this.isTemporaryRow(row)
},
compareRow(row, originalRow, field) {
const value = get(row, field)
Expand Down
3 changes: 1 addition & 2 deletions packages/vue/src/grid/src/table/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ export default defineComponent({
copyed: { columns: [], cut: false, rows: [] },
indexs: { columns: [] },
insertList: [],
insertMap: new Map(),
removeList: [],
// 选中源
selected: { column: null, row: null },
Expand Down Expand Up @@ -567,8 +568,6 @@ export default defineComponent({
fullDataRowIdData: {},
fullDataRowMap: new Map(),
backupInfos: { srcIdMap: new WeakMap(), idRawMap: new Map() },
// 临时插入数据集
temporaryRows: [],
// 最后滚动位置
lastScrollLeft: 0,
lastScrollTop: 0,
Expand Down
Loading