首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ag网格中实现查找列值和替换为新值(如查找和替换)

如何在ag网格中实现查找列值和替换为新值(如查找和替换)
EN

Stack Overflow用户
提问于 2019-09-09 15:00:30
回答 1查看 251关注 0票数 0

我是第一次接触ag。我想实现一个功能,我可以找到每个列值,并替换为单个列中的新值。

EN

回答 1

Stack Overflow用户

发布于 2020-09-05 23:14:18

由于ag-grid不提供查找/替换的方法,您可以使用以下算法进行查找/替换。

代码语言:javascript
复制
foundCell = [];
        foundIndex = 0;
        message = '';
        //Find text in ag-grid
        find(findText: string) {
            let found = false;
            let rowNodes: any = [];
            let focusedCell = this.gridApi.getFocusedCell();
            if (focusedCell) {
              let lastFoundObj: any;
              if (this.foundCell.length > 0) {
                lastFoundObj = this.foundCell[this.foundCell.length - 1];
                if (!(lastFoundObj.rowIndex == focusedCell.rowIndex && lastFoundObj.field == focusedCell.column.colId)) {
                  this.foundCell = [];
                  this.foundIndex = focusedCell.rowIndex;
                }
              }
            }
            this.gridApi.forEachNode(function (node,rowIndex) {
                  rowNodes.push(node);
            });
            for(let i=this.foundIndex; i < rowColumnData.rowNodes.length ; i++) {
              let node = rowColumnData.rowNodes[i];
              var rowObj = node.data;
              found = false;
              for (var key in rowObj) {
                if (rowObj[key].includes(findText) && !this.checkIfAlreadyFound(key, node.rowIndex)) {
                  found = true;
                  this.foundCell.push({ 'field': key, 'rowIndex': node.rowIndex});
                  break;
                }
              }
              if (found) {
                break;
              }
            }
            if (found) {
              let lastFoundCell = this.foundCell[this.foundCell.length-1];
              this.gridApi.ensureIndexVisible(lastFoundCell.rowIndex, 'middle');
              this.gridApi.ensureColumnVisible(lastFoundCell.field);
              this.gridApi.setFocusedCell(lastFoundCell.rowIndex,lastFoundCell.field);
              this.found = true;
            } else {
              this.foundIndex = 0;
              this.foundCell = [];
              this.found = false;
              this.message = 'Not found';
            }
        }
        
        
        //Replace text in ag-grid
        replace(findText: string, replaceWith: string, isReplaceAll: boolean) {
            if (this.found || isReplaceAll) {
              let focusedCell: any;
              var cell: any;
              let rowNodes = [];
              focusedCell = this.gridApi.getFocusedCell();
              if (focusedCell) {
                cell = { rowIndex: focusedCell.rowIndex, field: focusedCell.column.colId };
              } else {
                cell = this.foundCell[this.foundCell.length - 1];
              }
              this.gridApi.forEachNode(function (node,rowIndex) {
                  rowNodes.push(node);
              });
              var rowNode: any;
              var newCellValue: any;
              if (isReplaceAll) {
                let allfoundCell = this.findAllCells(rowNodes,findText);
                for (var i = 0; i < allfoundCell.length; i++) {
                  cell = allfoundCell[i];
                  rowNode = gridApi.getDisplayedRowAtIndex(cell.rowIndex);
                  newCellValue = this.gridApi.getValue(cell.field, rowNode).replace(findText,replaceWith);
                  rowNode.setDataValue(cell.field, newCellValue);
                }
              } else {
                rowNode = this.gridApi.getDisplayedRowAtIndex(cell.rowIndex);
                newCellValue = this.gridApi.getValue(cell.field, rowNode).replace(findText,replaceWith);
                if (newCellValue != rowNode.data[cell.field]) {
                  rowNode.setDataValue(cell.field, newCellValue);
                } 
              }
              this.found = false;
            }
         }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57849189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档