ag-gridで列を消せるけど邪魔。

ag-gridでヘッダー列をドラッグしてgrid外に移動させると、列を消すことが可能です。
しかし、消されたくはないので、消えないようにする方法を記載します。

列削除を無効にする。

列削除を無効にするには、suppressDragLeaveHidesColumnstrueを指定します。


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)] }; }); } } };

コメント

タイトルとURLをコピーしました