AgGridDetailGrids.vue 4.71 KB
Newer Older
1
<!--
hucy's avatar
hucy committed
2 3
 * AG-grid 细节网格细节配置
 * https://www.ag-grid.com/vue-data-grid/master-detail-grids/
4 5 6 7
 -->
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { AgGridVue } from 'ag-grid-vue3';
hcyhuchaoyue's avatar
hcyhuchaoyue committed
8
import Com from 'src/components';
hucy's avatar
hucy committed
9
import AG_GRID_LOCALE from 'src/config/ag-grid-locale';
10 11

const defaultColDef = {
hcyhuchaoyue's avatar
hcyhuchaoyue committed
12
  resizable: true,
13
  flex: 1,
hcyhuchaoyue's avatar
hcyhuchaoyue committed
14
  suppressMenu: false, // 是否禁用标题行菜单
15 16 17
};

const gridApi = ref<any>(null);
hucy's avatar
hucy committed
18
const gridColumnApi = ref<any>(null);
hucy's avatar
hucy committed
19 20
const detailCellGridApi = ref<any>(null);
const detailCellGridColumnApi = ref<any>(null);
hucy's avatar
hucy committed
21

22 23 24
const detailCellRendererParams = reactive({
  // 提供要在细节网格上使用的网格选项
  detailGridOptions: {
hucy's avatar
hucy committed
25
    localeText: AG_GRID_LOCALE,
26
    columnDefs: [
hucy's avatar
hucy committed
27 28 29 30
      {
        field: 'callId',
        headerName: '呼叫标识',
        checkboxSelection: true, // 给该列配置复选框
hucy's avatar
hucy committed
31
        headerCheckboxSelection: true, // 给该列标题栏设置复选框
hucy's avatar
hucy committed
32
      },
hucy's avatar
hucy committed
33 34 35 36 37 38 39 40
      { field: 'direction', headerName: '方向' },
      { field: 'number', headerName: '号码' },
      {
        field: 'duration',
        headerName: '持续时间',
        valueFormatter: 'x.toLocaleString() + "秒"',
      },
      { field: 'switchCode', headerName: '开关代码' },
41
    ],
hucy's avatar
hucy committed
42 43 44 45 46
    defaultColDef: {
      flex: 1,
      sortable: true,
    },
    rowSelection: 'multiple', // 多选
hucy's avatar
hucy committed
47 48
    suppressRowClickSelection: true, // 抑制行点击选择
    enableRangeSelection: true, // 启用范围选择
hucy's avatar
hucy committed
49 50
    // pagination: true,
    // paginationAutoPageSize: true,
hcyhuchaoyue's avatar
hcyhuchaoyue committed
51 52 53 54
    noRowsOverlayComponent: Com.AgGridNoRowsComponent,
    noRowsOverlayComponentParams: {
      dense: true,
    },
hucy's avatar
hucy committed
55
    loadingOverlayComponent: Com.AgGridLoadingOverlayComponent,
hucy's avatar
hucy committed
56 57 58 59
    onGridReady: (params: any) => {
      detailCellGridApi.value = params.api;
      detailCellGridColumnApi.value = params.columnApi;
    },
60 61 62
  },
  // 获得每个细节网格的行数
  getDetailRowData: (params: any) => {
hucy's avatar
hucy committed
63 64 65
    setTimeout(function () {
      params.successCallback(params.data.callRecords || []);
    }, 1000);
66 67 68
  },
});

hcyhuchaoyue's avatar
hcyhuchaoyue committed
69 70 71 72
const noRowsOverlayComponentParams = reactive({
  noRowsMessageFunc: () => '没有数据啦',
});

73 74
const state = reactive({
  columnDefs: [
hucy's avatar
hucy committed
75 76 77
    { field: 'name', headerName: '姓名', cellRenderer: 'agGroupCellRenderer' },
    { field: 'account', headerName: '帐户' },
    { field: 'calls', headerName: '呼叫' },
78
    {
hucy's avatar
hucy committed
79 80 81
      field: 'minutes',
      headerName: '分钟',
      valueFormatter: 'x.toLocaleString() + "分钟"',
82 83 84 85 86 87
    },
  ],
  rowData: [] as any,
});

onMounted(() => {
hcyhuchaoyue's avatar
hcyhuchaoyue committed
88 89 90 91 92 93 94 95
  setTimeout(() => {
    fetch('https://www.ag-grid.com/example-assets/master-detail-data.json')
      .then((result) => result.json())
      .then((remoteRowData) => {
        let arr = [...remoteRowData];
        state.rowData = remoteRowData.concat(arr);
        console.log('rowData', remoteRowData[0]);
      });
hucy's avatar
hucy committed
96
  }, 1000);
97 98 99 100
});

function onGridReady(params: any) {
  gridApi.value = params.api;
hucy's avatar
hucy committed
101
  gridColumnApi.value = params.columnApi;
102 103 104 105
}

function isRowMaster(dataItem: any) {
  let flag;
hucy's avatar
hucy committed
106
  if (dataItem && dataItem.callRecords && dataItem.callRecords.length > 0) {
107 108 109 110 111 112
    flag = true;
  } else {
    flag = false;
  }
  return flag;
}
hucy's avatar
hucy committed
113 114 115 116 117

function getSelectedRows() {
  const res = detailCellGridApi.value.getSelectedRows();
  console.log('获取选择行 ==', res);
}
118 119 120
</script>
<template>
  <div class="box">
hucy's avatar
hucy committed
121 122 123 124 125 126 127 128
    <div class="btns">
      <q-btn
        color="primary"
        label="获取选择的数据"
        no-caps
        @click="getSelectedRows"
      />
    </div>
129 130 131
    <!-- 
        rowSelection="multiple":多选,按住control键多选;【control+shift+鼠标点击】可选中多行
        animateRows=true:启用行动画
hucy's avatar
hucy committed
132 133
        :masterDetail="true" 启用展开的细节网格行
        :isRowMaster="isRowMaster" 确定哪一个行该展开
hcyhuchaoyue's avatar
hcyhuchaoyue committed
134
        :noRowsOverlayComponentParams="noRowsOverlayComponentParams"
135
     -->
hucy's avatar
hucy committed
136
    <div class="ag-table">
137
      <ag-grid-vue
hcyhuchaoyue's avatar
hcyhuchaoyue committed
138
        style="height: 500px"
139
        class="ag-theme-alpine"
hucy's avatar
hucy committed
140
        :localeText="AG_GRID_LOCALE"
141 142 143 144 145
        :columnDefs="state.columnDefs"
        :rowData="state.rowData"
        :defaultColDef="defaultColDef"
        :animateRows="true"
        :masterDetail="true"
hucy's avatar
hucy committed
146
        :isRowMaster="isRowMaster"
147
        :detailCellRendererParams="detailCellRendererParams"
hcyhuchaoyue's avatar
hcyhuchaoyue committed
148 149 150 151
        :noRowsOverlayComponent="Com.AgGridNoRowsComponent"
        :noRowsOverlayComponentParams="noRowsOverlayComponentParams"
        :pagination="true"
        :paginationPageSize="5"
152 153 154 155 156 157 158
        @grid-ready="onGridReady"
      >
      </ag-grid-vue>
    </div>
  </div>
</template>

hucy's avatar
hucy committed
159 160 161 162 163 164 165 166 167 168 169 170 171
<style lang="scss" scoped>
.btns {
  padding: 10px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  column-gap: 10px;
}
.ag-table {
  padding: 0 10px 10px 10px;
}
</style>