ag-gridでヘッダー列をドラッグしてgrid外に移動させると、列を消すことが可能です。
しかし、消されたくはないので、消えないようにする方法を記載します。
列削除を無効にする。
列削除を無効にするには、suppressDragLeaveHidesColumns
にtrue
を指定します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<br />import { AgGridVue } from "ag-grid-vue"; import { LAST_NAMES, FIRST_NAMES, PREFECTURES } from "../datas"; export default { name: "AgGridSample", components: { AgGridVue }, data() { return { gridOptions: null }; }, created() { this.gridOptions = { columnDefs: this.createColumnDef(), rowData: this.createRowData(), enableSorting: true, enableFilter: true, suppressDragLeaveHidesColumns: true } }, methods: { createColumnDef() { return [ { headerName: "苗字", field: "lastName" }, { headerName: "名", field: "firstName" }, { headerName: "性別", field: "sex" }, { headerName: "年齢", field: "age" }, { headerName: "都道府県", field: "prefecture" } ]; }, /** * 行データ変更時の処理。 */ onRowDataChanged() { this.$nextTick(() =>{ // 列幅をgridに合わせる。 this.gridOptions.api.sizeColumnsToFit(); }); }, /** * ag-gridで表示するデータを作成。 * */ createRowData() { return [...Array(1000)].map(() => { const sex = ["MALE", "FEMALE"][Math.floor(Math.random() * 2)]; return { sex, lastName: LAST_NAMES[Math.floor(Math.random() * LAST_NAMES.length)], firstName: FIRST_NAMES[sex][ Math.floor(Math.random() * FIRST_NAMES[sex].length) ], age: Math.floor(Math.random() * 30), prefecture: PREFECTURES[Math.floor(Math.random() * PREFECTURES.length)] }; }); } } }; |