AgGridCustomCellEdit.vue 5.59 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8
<!--
 * Ag Grid 自定义单元格编辑器
 * https://www.ag-grid.com/vue-data-grid/component-cell-editor/
 * AG-grid 自定义单元格
 * https://www.ag-grid.com/vue-data-grid/cell-rendering/
-->
<script setup lang="ts">
import { ref, reactive, onMounted, toRaw } from 'vue';
hucy's avatar
hucy committed
9
import { useQuasar } from 'quasar';
hucy's avatar
hucy committed
10 11
import { useMessage } from 'src/common/hooks';

hucy's avatar
hucy committed
12
import { AgGridVue } from 'ag-grid-vue3';
hucy's avatar
hucy committed
13
import { MyBtn, ComDialogTip } from 'src/components';
hucy's avatar
hucy committed
14 15 16 17
import AG_GRID_LOCALE from 'src/config/ag-grid-locale';
import DoubleNumberEdit from '../ag-grid-element/DoubleNumberEdit.vue';
import MyCellEditer from '../ag-grid-element/MyCellEditer.vue';
import ButtonCellRender from '../ag-grid-element/ButtonCellRender.vue';
hucy's avatar
hucy committed
18 19 20

import type { ColumnDefsType, ButtonCellRenderType } from 'src/common/types';

hucy's avatar
hucy committed
21
const $q = useQuasar();
hucy's avatar
hucy committed
22
const { info } = useMessage();
hucy's avatar
hucy committed
23 24 25 26 27 28 29 30 31 32 33

const defaultColDef = {
  resizable: true,
  flex: 1,
  suppressMenu: true, // 抑制标题行的菜单
  editable: true, // 可以编辑单元格
};

const gridApi = ref<any>(null);
const gridColumnApi = ref<any>(null);

hucy's avatar
hucy committed
34
const columnDefs = reactive<Array<ColumnDefsType>>([
hucy's avatar
hucy committed
35 36 37 38 39 40 41 42 43 44 45 46
  {
    field: 'name',
    headerName: '姓名',
    cellEditor: MyCellEditer,
  },
  {
    field: 'value',
    headerName: '值',
    cellEditor: DoubleNumberEdit,
  },
  {
    headerName: '操作',
hucy's avatar
hucy committed
47
    minWidth: 200,
hucy's avatar
hucy committed
48 49 50 51 52 53
    pinned: 'right',
    editable: false,
    cellRenderer: ButtonCellRender,
    cellRendererParams: {
      buttons: [
        {
hucy's avatar
hucy committed
54
          hide: false,
hucy's avatar
hucy committed
55
          tooltip: '删除',
hucy's avatar
hucy committed
56
          color: 'grey-9',
hucy's avatar
hucy committed
57
          round: true,
hucy's avatar
hucy committed
58 59
          icon: 'bi-trash',
          flat: true,
hucy's avatar
hucy committed
60 61 62 63 64
          callback: (data: any) => {
            onDel(data);
          },
        },
      ],
hucy's avatar
hucy committed
65 66 67 68 69 70 71 72 73 74 75 76
      moreButtons: {
        hide: false,
        icon: 'bi-three-dots',
        color: 'grey-9',
        round: true,
        flat: true,
        children: [
          {
            hide: false,
            label: '复制',
            icon: 'bi-clipboard-check',
            color: 'light-green-6',
hucy's avatar
hucy committed
77 78
            callback: (data: any) => {
              onCopy(data);
hucy's avatar
hucy committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            },
          },
          {
            label: '调试',
            icon: 'bi-bug',
            color: 'purple-3',
            callback: () => {
              info('调试');
            },
          },
          {
            label: '计划',
            icon: 'bi-clipboard-data',
            color: 'orange',
            callback: () => {
              info('计划');
            },
          },
        ],
      },
    } as ButtonCellRenderType,
hucy's avatar
hucy committed
100 101 102 103
  },
]);

const state = reactive({
hucy's avatar
hucy committed
104
  rowData: [] as any[],
hucy's avatar
hucy committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
});

onMounted(() => {
  getData();
});

// 点击单元格
function cellWasClicked(event: any) {
  return;
  console.log('cellWasClicked >>>>', event);
}

function onGridReady(params: any) {
  gridApi.value = params.api;
  gridColumnApi.value = params.columnApi;
}

function getData() {
  let arr = [
    { name: '张三', value: 18 },
    { name: '李四', value: 22 },
  ];
  state.rowData = arr;
}

function onView() {
  console.log('rowData', toRaw(state.rowData));
}

function onAdd() {
  state.rowData.push({ name: null, value: null });
hucy's avatar
hucy committed
136 137 138
  setTimeout(() => {
    gridApi.value.setRowData(state.rowData);
  }, 0);
hucy's avatar
hucy committed
139 140 141
}

function onDel(data: any) {
hucy's avatar
hucy committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  $q.dialog({
    component: ComDialogTip,
    componentProps: {
      persistent: true,
      text: `确定删除当前列 ${data.data.name}`,
      color: 'negative',
    },
  }).onOk(() => {
    state.rowData.splice(data.rowIndex, 1);
    setTimeout(() => {
      gridApi.value.setRowData(state.rowData);
    }, 0);
  });
}

function onCopy(data: any) {
  state.rowData.push(data.data);
  setTimeout(() => {
    gridApi.value.setRowData(state.rowData);
  }, 0);
hucy's avatar
hucy committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}
</script>
<template>
  <div>
    <div>
      <div class="text">
        <p>双击可编辑单元格</p>
        <p>
          在【值】这一列,输入值后面 + "=",执行(value = value *
          2);(列如:输入:"20=",结果为:40)
        </p>
      </div>
      <div class="btns">
        <my-btn @click="onView" label="查看" />
        <my-btn @click="onAdd" label="添加" />
      </div>
    </div>
    <!-- 
      :suppressContextMenu="true" 阻止显示右键单击时的上下文菜单
      :suppressCellFocus="true" 阻止单元格聚焦
     -->
    <div class="ag-table">
      <ag-grid-vue
        style="height: 500px"
        class="ag-theme-alpine"
        :localeText="AG_GRID_LOCALE"
        :rowHeight="50"
        :headerHeight="54"
        rowClass="page9-ag-grid-element-my-edit-rowbox"
        :suppressContextMenu="true"
hucy's avatar
hucy committed
192
        :suppressCellFocus="true"
hucy's avatar
hucy committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        :columnDefs="columnDefs"
        :rowData="state.rowData"
        :defaultColDef="defaultColDef"
        :animateRows="true"
        @cell-clicked="cellWasClicked"
        @grid-ready="onGridReady"
      >
      </ag-grid-vue>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.text {
  padding: 10px;
  padding-bottom: 0;
  p {
    margin-bottom: 6px;
    &:nth-last-child(1) {
      margin-bottom: 0;
    }
  }
}
.btns {
  padding: 10px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  column-gap: 10px;
}
:deep(.ag-table) {
  padding: 0 10px 10px 10px;
  .ag-cell-inline-editing {
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    padding: 0 !important;
    background-color: transparent !important;
  }
}
</style>
<style lang="scss">
.page9-ag-grid-element-my-edit-rowbox {
  display: flex;
  float: row;
  flex-wrap: nowrap;
  align-items: center;
  .ag-cell-not-inline-editing {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
</style>