AgGridCheckboxSelection.vue 3.79 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
<!--
 * Ag Grid 复选框选择
 * https://www.ag-grid.com/vue-data-grid/row-selection/#checkbox-selection
 -->
<script setup lang="ts">
import { ref, reactive, onMounted, watch } 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 checkAllUsable = ref<any>(null);

const columnDefs = reactive([
  {
    field: 'athlete',
    headerName: '运动员',
    checkboxSelection: (params: any) => {
      return params.data && params.data.year !== 2012;
    },
    showDisabledCheckboxes: true,
  },
  { field: 'sport', headerName: '运动' },
  { field: 'year', maxWidth: 120, headerName: '年份' },
]);

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

watch(checkAllUsable, (val: any) => {
  if (val === true) {
    gridApi.value.forEachNode((node: any) => {
      node.setSelected(true);
    });
  } else if (val === false) {
    deselectRows();
  }
});

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

function getData() {
  fetch('https://www.ag-grid.com/example-assets/small-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 onFirstDataRendered(params: any) {
  params.api.forEachNode((node: any) =>
    node.setSelected(node.data && node.data.year == 2012)
  );
}

function onSelectionChanged() {
  const length1 = state.rowData.length;
  const selectedRows = gridApi.value.getSelectedRows();
  const length2 = selectedRows.length;
  state.selected = selectedRows.map((item: any) => item.athlete);
  if (length1 && length1 === length2) {
    checkAllUsable.value = true;
  } else if (length1 !== length2) {
    checkAllUsable.value = null;
  } else {
    checkAllUsable.value = false;
  }
}

// function isRowSelectable(params: any) {
//   return !!params.data && params.data.year === 2012;
// }

function deselectRows() {
  //   gridApi.value.deselectAll();
  gridApi.value.forEachNode((node: any) => {
    let falg = false;
    if (node.data && node.data.year == 2012) {
      falg = true;
    }
    node.setSelected(falg);
  });
}
</script>
<template>
  <div class="box">
    <div class="text">
      <div>固定选中<q-chip square :ripple="false">年份=2012</q-chip>的行</div>
      <q-checkbox v-model="checkAllUsable" label="选择全部可选内容" dense />
      <span>已选择:{{ state.selected.join(',') }}</span>
    </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"
        :localeText="AG_GRID_LOCALE"
        :defaultColDef="defaultColDef"
        :columnDefs="columnDefs"
        :rowData="state.rowData"
        rowSelection="multiple"
        :suppressRowClickSelection="true"
        @grid-ready="onGridReady"
        @first-data-rendered="onFirstDataRendered"
        @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>