AgGridCustomDateComponent.vue 5.31 KB
Newer Older
hcyhuchaoyue's avatar
hcyhuchaoyue committed
1 2 3 4 5 6 7
<!--
 * Ag Grid 自定义日期组件
 * https://www.ag-grid.com/vue-data-grid/component-date/
 -->
<script lang="ts">
import { ref, reactive, defineComponent } from 'vue';
import { AgGridVue } from 'ag-grid-vue3';
hucy's avatar
hucy committed
8 9
// import { date } from 'quasar';

hcyhuchaoyue's avatar
hcyhuchaoyue committed
10 11
import Com from 'src/components';
import AG_GRID_LOCALE from 'src/config/ag-grid-locale';
hucy's avatar
hucy committed
12
import { isEmpty } from 'src/common/utils';
hcyhuchaoyue's avatar
hcyhuchaoyue committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

export default defineComponent({
  name: 'AgGridCustomDateComponent',
  components: {
    'ag-grid-vue': AgGridVue,
    // eslint-disable-next-line vue/no-unused-components
    agDateInput: Com.AgGridDateComponent,
  },
  setup() {
    const gridApi = ref<any>(null);
    const gridColumnApi = ref<any>(null);

    const defaultColDef = reactive({
      flex: 1,
      minWidth: 100,
      resizable: true,
      editable: true, // 可编辑
      sortable: true,
      filter: true,
hucy's avatar
hucy committed
32
      // floatingFilter: true,
hcyhuchaoyue's avatar
hcyhuchaoyue committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    });

    const columnDefs = reactive([
      { field: 'athlete', headerName: '运动员' },
      { field: 'age', headerName: '年龄', filter: 'agNumberColumnFilter' },
      { field: 'country', headerName: '国家' },
      { field: 'year', headerName: '年份' },
      {
        field: 'date',
        minWidth: 190,
        headerName: '日期',
        filter: 'agDateColumnFilter',
        filterParams: {
          comparator: (filterLocalDateAtMidnight: any, cellValue: any) => {
hucy's avatar
hucy committed
47 48 49 50 51 52 53 54 55 56 57
            // const formattedString = date.formatDate(
            //   filterLocalDateAtMidnight,
            //   'YYYY/MM/DD'
            // );
            // console.log(
            //   'filterLocalDateAtMidnight',
            //   formattedString,
            //   'cellValue',
            //   cellValue
            // );

hcyhuchaoyue's avatar
hcyhuchaoyue committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            const dateAsString = cellValue;
            const dateParts = dateAsString.split('/');
            const cellDate = new Date(
              Number(dateParts[2]),
              Number(dateParts[1]) - 1,
              Number(dateParts[0])
            );
            if (filterLocalDateAtMidnight.getTime() === cellDate.getTime()) {
              return 0;
            }
            if (cellDate < filterLocalDateAtMidnight) {
              return -1;
            }
            if (cellDate > filterLocalDateAtMidnight) {
              return 1;
            }
          },
        },
hucy's avatar
hucy committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89
        comparator: (d1: any, d2: any) => {
          return getComparatorDate(d1).getTime() <
            getComparatorDate(d2).getTime()
            ? -1
            : 1;
        },
        valueFormatter: (params: any) => {
          let resStr = '';
          if (!isEmpty(params.value)) {
            const dateArr = params.value.split('/');
            resStr = dateArr[2] + '-' + dateArr[1] + '-' + dateArr[0];
          }
          return resStr;
        },
hcyhuchaoyue's avatar
hcyhuchaoyue committed
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
      },
      { field: 'sport', headerName: '运动' },
      { field: 'gold', headerName: '金牌', filter: 'agNumberColumnFilter' },
      { field: 'silver', headerName: '银牌', filter: 'agNumberColumnFilter' },
      { field: 'bronze', headerName: '铜牌', filter: 'agNumberColumnFilter' },
      { field: 'total', headerName: '共计', filter: false },
    ]);

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

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

    const getData = () => {
      fetch('https://www.ag-grid.com/example-assets/olympic-winners.json')
        .then((resp) => resp.json())
        .then((data) => updateData(data));
    };

    const updateData = (data: any) => {
hucy's avatar
hucy committed
115
      state.rowData = data.slice(0, 5);
hcyhuchaoyue's avatar
hcyhuchaoyue committed
116 117 118
      console.log('rowData[0]', state.rowData[0]);
    };

hucy's avatar
hucy committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    const onReset = () => {
      gridApi.value.setFilterModel(null);
      gridApi.value.destroyFilter('date');
    };

    const getComparatorDate = (data: string) => {
      // 21/01/2008
      const dateArr = data.split('/');
      const year = Number(dateArr[2]);
      const month = Number(dateArr[1]);
      const day = Number(dateArr[0]);
      const newDate = new Date(year, month, day);
      return newDate;
    };

hcyhuchaoyue's avatar
hcyhuchaoyue committed
134 135 136 137 138 139 140
    return {
      AG_GRID_LOCALE,
      Com,
      defaultColDef,
      columnDefs,
      state,
      onGridReady,
hucy's avatar
hucy committed
141
      onReset,
hcyhuchaoyue's avatar
hcyhuchaoyue committed
142 143 144 145 146 147
    };
  },
});
</script>
<template>
  <div>
hucy's avatar
hucy committed
148 149 150
    <div class="btns">
      <q-btn color="primary" label="重置" no-caps unelevated @click="onReset" />
    </div>
hcyhuchaoyue's avatar
hcyhuchaoyue committed
151 152 153 154 155
    <!-- 
        rowSelection="multiple":多选,按住control键多选;【control+shift+鼠标点击】可选中多行
        animateRows=true:启用行动画
        :masterDetail="true" 启用展开的细节网格行
        :isRowMaster="isRowMaster" 确定哪一个行该展开
hucy's avatar
hucy committed
156 157
        :pagination="true"
        :paginationPageSize="100"
hcyhuchaoyue's avatar
hcyhuchaoyue committed
158
     -->
hucy's avatar
hucy committed
159
    <div class="ag-table">
hcyhuchaoyue's avatar
hcyhuchaoyue committed
160
      <ag-grid-vue
hucy's avatar
hucy committed
161
        style="height: 580px"
hcyhuchaoyue's avatar
hcyhuchaoyue committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
        class="ag-theme-alpine"
        :localeText="AG_GRID_LOCALE"
        :animateRows="true"
        :pagination="true"
        :paginationPageSize="10"
        :defaultColDef="defaultColDef"
        :columnDefs="columnDefs"
        :rowData="state.rowData"
        @grid-ready="onGridReady"
      >
      </ag-grid-vue>
    </div>
  </div>
</template>
hucy's avatar
hucy committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189

<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>