mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 00:23:57 +00:00
free table editing operations
This commit is contained in:
@@ -66,6 +66,10 @@ export default class ChangeSetGrider extends Grider {
|
|||||||
this.rowCacheIndexes.add(index);
|
this.rowCacheIndexes.add(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get canInsert() {
|
||||||
|
return !!this.display.baseTable;
|
||||||
|
}
|
||||||
|
|
||||||
getRowData(index: number) {
|
getRowData(index: number) {
|
||||||
this.requireRowCache(index);
|
this.requireRowCache(index);
|
||||||
return this.rowDataCache[index];
|
return this.rowDataCache[index];
|
||||||
|
|||||||
@@ -598,7 +598,7 @@ export default function DataGridCore(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const insertNewRow = () => {
|
const insertNewRow = () => {
|
||||||
if (display.baseTable) {
|
if (grider.canInsert) {
|
||||||
const rowIndex = grider.insertRow();
|
const rowIndex = grider.insertRow();
|
||||||
const cell = [rowIndex, (currentCell && currentCell[1]) || 0];
|
const cell = [rowIndex, (currentCell && currentCell[1]) || 0];
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ export default abstract class Grider {
|
|||||||
revertAllChanges() {}
|
revertAllChanges() {}
|
||||||
undo() {}
|
undo() {}
|
||||||
redo() {}
|
redo() {}
|
||||||
|
get canInsert() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
get rowCountInUpdate() {
|
get rowCountInUpdate() {
|
||||||
return this.rowCount;
|
return this.rowCount;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,59 @@ import Grider, { GriderRowStatus } from '../datagrid/Grider';
|
|||||||
|
|
||||||
export default class FreeTableGrider extends Grider {
|
export default class FreeTableGrider extends Grider {
|
||||||
public model: FreeTableModel;
|
public model: FreeTableModel;
|
||||||
public rows: any[];
|
private batchModel: FreeTableModel;
|
||||||
|
|
||||||
constructor(public modelState, public dispatchModel) {
|
constructor(public modelState, public dispatchModel) {
|
||||||
super();
|
super();
|
||||||
this.model = modelState && modelState.value;
|
this.model = modelState && modelState.value;
|
||||||
this.rows = this.model.rows;
|
|
||||||
}
|
}
|
||||||
getRowData(index: any) {
|
getRowData(index: any) {
|
||||||
return this.rows[index];
|
return this.model.rows[index];
|
||||||
}
|
}
|
||||||
get rowCount() {
|
get rowCount() {
|
||||||
return this.rows.length;
|
return this.model.rows.length;
|
||||||
|
}
|
||||||
|
get currentModel(): FreeTableModel {
|
||||||
|
return this.batchModel || this.model;
|
||||||
|
}
|
||||||
|
set currentModel(value) {
|
||||||
|
if (this.batchModel) this.batchModel = value;
|
||||||
|
else this.dispatchModel({ type: 'set', value });
|
||||||
|
}
|
||||||
|
setCellValue(index: number, uniqueName: string, value: any) {
|
||||||
|
const model = this.currentModel;
|
||||||
|
if (model.rows[index])
|
||||||
|
this.currentModel = {
|
||||||
|
...model,
|
||||||
|
rows: model.rows.map((row, i) => (index == i ? { ...row, [uniqueName]: value } : row)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
get canInsert() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
insertRow(): number {
|
||||||
|
const model = this.currentModel;
|
||||||
|
this.currentModel = {
|
||||||
|
...model,
|
||||||
|
rows: [...model.rows, {}],
|
||||||
|
};
|
||||||
|
return this.currentModel.rows.length - 1;
|
||||||
|
}
|
||||||
|
deleteRow(index: number) {
|
||||||
|
const model = this.currentModel;
|
||||||
|
this.currentModel = {
|
||||||
|
...model,
|
||||||
|
rows: model.rows.filter((row, i) => index != i),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
beginUpdate() {
|
||||||
|
this.batchModel = this.model;
|
||||||
|
}
|
||||||
|
endUpdate() {
|
||||||
|
if (this.model != this.batchModel) {
|
||||||
|
this.dispatchModel({ type: 'set', value: this.batchModel });
|
||||||
|
this.batchModel = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static factory({ modelState, dispatchModel }): FreeTableGrider {
|
static factory({ modelState, dispatchModel }): FreeTableGrider {
|
||||||
|
|||||||
@@ -10,6 +10,20 @@ import FreeTableGrid from '../freetable/FreeTableGrid';
|
|||||||
export default function FreeDataTab({ conid, database, schemaName, pureName, tabVisible, toolbarPortalRef, tabid }) {
|
export default function FreeDataTab({ conid, database, schemaName, pureName, tabVisible, toolbarPortalRef, tabid }) {
|
||||||
const [config, setConfig] = useGridConfig(tabid);
|
const [config, setConfig] = useGridConfig(tabid);
|
||||||
const [modelState, dispatchModel] = useUndoReducer(createFreeTableModel());
|
const [modelState, dispatchModel] = useUndoReducer(createFreeTableModel());
|
||||||
|
const storageKey = `tabdata_freetable_${tabid}`;
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const existingData = localStorage.getItem(storageKey);
|
||||||
|
if (existingData) {
|
||||||
|
const value = JSON.parse(existingData);
|
||||||
|
// @ts-ignore
|
||||||
|
dispatchModel({ type: 'reset', value });
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
localStorage.setItem(storageKey, JSON.stringify(modelState.value));
|
||||||
|
}, [modelState]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FreeTableGrid
|
<FreeTableGrid
|
||||||
|
|||||||
Reference in New Issue
Block a user