gridLib/agGrid

agGrid data 추가, 가져오기

dddzr 2023. 8. 14. 15:19

api 가져오기

그리드가 생성 될 때 배열에 저장해뒀다가 사용

onGridReady: function (api) {
  this.agGrids = []
  this.agGrids.push({key: api});
}

onGridReady: function (api) {
  this.agGrids = {}
  this.agGrids.[key] = api;
}

 

set Data

api.setRowData([]);

 

get Data

//한 줄
api.getRowNode(id);

//모두
getAllRows(api);
getAllRows(api) {
  let rowData = [];
  api.forEachNode(node => rowData.push(node.data));
  return rowData;
}

//선택된 것
api.getSelectedRows()

 

+ 추가하거나 삭제할 때 객체 비교

let orgData = getRowData(api);
for (let i = 0; i < orgData.length; i++) {
  if (isEqual(orgData[i], addObj)) {
    //같은 데이터가 존재합니다.
  }
}

function isEqual(obj1, obj2) {
  if (obj1 === obj2) return true;

  if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) return false;

  let keys1 = Object.keys(obj1);
  let keys2 = Object.keys(obj2);

  if (keys1.length !== keys2.length) return false;

  for (let key of keys1) {
    if (!keys2.includes(key) || !isEqual(obj1[key], obj2[key])) return false;
  }

  return true;
}