AgGridSelection.vue 3.7 KB
Newer Older
hucy's avatar
hucy committed
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<!--
 * AG-grid 选择
 * https://www.ag-grid.com/vue-data-grid/selection-overview/
 -->
<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue';
import { AgGridVue } from 'ag-grid-vue3';
import AG_GRID_LOCALE from 'src/config/ag-grid-locale';

const defaultColDef = {
  flex: 1,
  minWidth: 100,
  suppressMenu: true, // 是否禁用标题行菜单
};

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

const columnDefs = reactive([
  { field: 'athlete', minWidth: 150, headerName: '运动员' },
  { field: 'age', maxWidth: 90, headerName: '年龄' },
  { field: 'country', minWidth: 150, headerName: '国家' },
  { field: 'year', maxWidth: 90, headerName: '年份' },
  { field: 'date', minWidth: 150, headerName: '日期' },
  { field: 'sport', minWidth: 150, headerName: '运动' },
  { field: 'gold', headerName: '金牌' },
  { field: 'silver', headerName: '银牌' },
  { field: 'bronze', headerName: '铜牌' },
  { field: 'total', headerName: '共计' },
]);

const selectionGroup = ref('single');
const rowSelectionOpt = reactive([
  {
    label: '单选',
    value: 'single',
  },
  {
    label: '多选',
    value: 'multiple',
  },
  {
    label: '单击多选',
    value: 'both',
  },
]);

const state = reactive({
  rowData: [] as any,
  selected: [],
});

const rowSelection = computed(() => {
  let val = '';
  switch (selectionGroup.value) {
    case 'single':
      val = 'single';
      break;

    case 'multiple':
      val = 'multiple';
      break;

    default:
      val = 'multiple';
      break;
  }
  return val;
});

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

function getData() {
  fetch('https://www.ag-grid.com/example-assets/olympic-winners.json')
    .then((resp) => resp.json())
    .then((data) => {
      state.rowData = data;
      console.log('state.rowData[0]', state.rowData[0]);
    });
}

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

function onSelectionChanged() {
  const selectedRows = gridApi.value.getSelectedRows();
  state.selected = selectedRows.map((item: any) => item.athlete);
}

function deselectRows() {
  gridApi.value.deselectAll();
}
</script>
<template>
  <div class="box">
    <div class="text">
      <span>已选择:{{ state.selected.join(',') }}</span>
      <q-option-group
        dense
        v-model="selectionGroup"
        :options="rowSelectionOpt"
        color="primary"
        inline
      />
      <q-btn
        color="primary"
        label="取消所有选择"
        no-caps
        @click="deselectRows"
      />
    </div>
    <!-- 
        rowSelection="multiple":多选,按住control键多选;【control+shift+鼠标点击】可选中多行
        animateRows=true:启用行动画
        :masterDetail="true" 启用展开的细节网格行
        :isRowMaster="isRowMaster" 确定哪一个行该展开
        rowSelection="single" single单选 multiple多选
     -->
    <div class="ag-table">
      <ag-grid-vue
        style="height: 550px"
        class="ag-theme-alpine"
        :animateRows="true"
        :pagination="true"
        :paginationPageSize="10"
        :localeText="AG_GRID_LOCALE"
        :defaultColDef="defaultColDef"
        :columnDefs="columnDefs"
        :rowData="state.rowData"
        :rowSelection="rowSelection"
        :rowMultiSelectWithClick="selectionGroup === 'both'"
        @grid-ready="onGridReady"
        @selection-changed="onSelectionChanged"
      >
      </ag-grid-vue>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.text {
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  row-gap: 10px;
}
.ag-table {
  padding: 0 10px 10px 10px;
}
</style>