简体   繁体   English

如果单元格不可见,则以编程方式打开行的编辑模式。 Ag 网格,Vue.js

[英]Open edit mode for a row programmatically if a cell is not visible. Ag Grid, Vue.js

How could I open edit mode for a row programmatically if a cell is not visible?如果单元格不可见,如何以编程方式打开行的编辑模式?

In my case the cell is not visible due to a large number of columns.在我的情况下,由于大量列,单元格不可见。 Here is a reproducible example:是一个可重现的示例:

import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
import 'ag-grid-enterprise';
import { AgGridVue } from 'ag-grid-vue';
import Vue from 'vue';

const VueExample = {
  template: `
        <div style="height: 100%">
            <div class="example-wrapper">
                <div style="margin-bottom: 5px;">
                    <button style="font-size: 12px" v-on:click="onBtStartEditing()">Start Editing Line 2</button>
                    <button style="font-size: 12px" v-on:click="onBtStopEditing()">Stop Editing</button>
                </div>
                <ag-grid-vue
                
                style="width: 100%; height: 100%;"
                class="ag-theme-alpine"
                :columnDefs="columnDefs"
                @grid-ready="onGridReady"
                :defaultColDef="defaultColDef"
                :editType="editType"
                :rowData="rowData"
                @cell-value-changed="onCellValueChanged"
                @row-value-changed="onRowValueChanged"></ag-grid-vue>
            </div>
        </div>
    `,
  components: {
    'ag-grid-vue': AgGridVue,
  },
  data: function () {
    return {
      columnDefs: [
        {
          field: 'make',
          cellEditor: 'agSelectCellEditor',
          cellEditorParams: {
            values: ['Porsche', 'Toyota', 'Ford', 'AAA', 'BBB', 'CCC'],
          },
        },
        { field: 'model' },
        { field: 'field4', headerName: 'Read Only', editable: true },
        { field: 'price', cellEditor: NumericCellEditor },
        {
          headerName: 'Suppress Navigable',
          field: 'field5',
          suppressNavigable: true,
          minWidth: 200,
        },
        { headerName: 'Read Only', field: 'field6', editable: true },
      ],
      gridApi: null,
      columnApi: null,
      defaultColDef: {
        flex: 1,
        editable: true,
      },
      editType: null,
      rowData: null,
    };
  },
  created() {
    this.editType = 'fullRow';
    this.rowData = getRowData();
    for(let i = 0; i < 1000; ++i) {
      this.columnDefs.push({ headerName: 'Read Only', field: 'field6', editable: true });
    }
  },
  methods: {
    onCellValueChanged(event) {
      console.log(
        'onCellValueChanged: ' + event.colDef.field + ' = ' + event.newValue
      );
    },
    onRowValueChanged(event) {
      var data = event.data;
      console.log(
        'onRowValueChanged: (' +
          data.make +
          ', ' +
          data.model +
          ', ' +
          data.price +
          ', ' +
          data.field5 +
          ')'
      );
    },
    onBtStopEditing() {
      this.gridApi.stopEditing();
    },
    onBtStartEditing() {
      this.gridApi.setFocusedCell(1, 'make');
      this.gridApi.startEditingCell({
        rowIndex: 1,
        colKey: 'make',
      });
    },
    onGridReady(params) {
      this.gridApi = params.api;
      this.gridColumnApi = params.columnApi;
    },
  },
};

window.getRowData = function getRowData() {
  var rowData = [];
  for (var i = 0; i < 10; i++) {
    rowData.push({
      make: 'Toyota',
      model: 'Celica',
      price: 35000 + i * 1000,
      field4: 'Sample XX',
      field5: 'Sample 22',
      field6: 'Sample 23',
    });
    rowData.push({
      make: 'Ford',
      model: 'Mondeo',
      price: 32000 + i * 1000,
      field4: 'Sample YY',
      field5: 'Sample 24',
      field6: 'Sample 25',
    });
    rowData.push({
      make: 'Porsche',
      model: 'Boxster',
      price: 72000 + i * 1000,
      field4: 'Sample ZZ',
      field5: 'Sample 26',
      field6: 'Sample 27',
    });
  }
  return rowData;
};

new Vue({
  el: '#app',
  components: {
    'my-component': VueExample,
  },
});

Once the application opens just scroll to the most right of the table.一旦应用程序打开,只需滚动到表格的最右侧。 Then click the Start Editing Line 2 button.然后单击Start Editing Line 2按钮。 The result is that no row gets transferred into the edit mode.结果是没有行被转移到编辑模式。 That is because the make column gets removed from DOM.那是因为make列已从 DOM 中删除。

Now, refresh the page and click the Start Editing Line 2 button without scrolling anywhere.现在,刷新页面并单击Start Editing Line 2按钮,无需滚动任何位置。 The result is that the second row goes into the edit mode.结果是第二行进入编辑模式。

So, how can I transfer a row into an edit mode if not all cells are visible?那么,如果不是所有单元格都可见,我如何将一行转换为编辑模式? Ie in the former case I would like the Start Editing Line 2 button to keep working.即在前一种情况下,我希望“ Start Editing Line 2 ”按钮继续工作。

If the cell is not visible I would prefer not to focus any cell and still get the row transferred in the edit mode.如果单元格不可见,我宁愿不关注任何单元格并仍然在编辑模式下传输行。

You can pin the "make" column (temporarily or permanently)您可以固定“make”列(临时或永久)

onBtStartEditing() {
  this.gridColumnApi.applyColumnState({
    state: [{ colId: 'make', pinned: 'left' }],
    defaultState: { pinned: null },
  });
  this.gridApi.setFocusedCell(1, 'make');
  this.gridApi.startEditingCell({
    rowIndex: 1,
    colKey: 'make',
  });
},

onBtStopEditing() {
  this.gridColumnApi.applyColumnState({
    state: [{ colId: 'make', pinned: null }],
    defaultState: { pinned: null },
  });
  this.gridApi.stopEditing();
},

Another option I found is to set the suppressColumnVirtualisation to true .我发现的另一个选项是将suppressColumnVirtualisation设置为true This way the whole row is drawn to HTML all the time (even on scroll to the most right).这样一来,整行就一直被绘制成 HTML(即使滚动到最右边)。 This way we can be sure that the focused cell will exist all the time and so the edit mode will get opened just fine.通过这种方式,我们可以确保焦点单元始终存在,因此编辑模式将很好地打开。

More details here .更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM