下拉默认全选,选择展示对象的字段list

javascript 复制代码
<template>
  <div style="padding: 20px">
    <h3>opened + selection-changed </h3>

    <DxDropDownBox
      v-model="selectedItems"
      :show-clear-button="true"
      placeholder="请选择水果..."
      display-expr="name"
      value-expr="id"
      @opened="onOpened"
      width="400"
    >
      <template #content>
        <DxDataGrid
          ref="dataGrid"
          :data-source="dataSource"
          :hover-state-enabled="true"
          :height="250"
          :show-borders="false"
          @selection-changed="onSelectionChanged"
        >
          <DxColumn data-field="id" caption="编号" />
          <DxColumn data-field="name" caption="水果名称" />
          <DxColumn data-field="color" caption="颜色" />
          <DxSelection mode="multiple" />
        </DxDataGrid>
      </template>
    </DxDropDownBox>

    <div style="margin-top: 10px;">
      <strong>已选中 ID:</strong> {{ selectedItems }}
    </div>
  </div>
</template>

<script>
import { DxDropDownBox } from 'devextreme-vue/drop-down-box';
import { DxDataGrid, DxColumn, DxSelection } from 'devextreme-vue/data-grid';

export default {
  components: {
    DxDropDownBox,
    DxDataGrid,
    DxColumn,
    DxSelection,
  },
  data() {
    return {
      selectedItems: [],
      dataSource: [
        { id: 1, name: '苹果', color: '红色' },
        { id: 2, name: '香蕉', color: '黄色' },
        { id: 3, name: '橙子', color: '橙色' },
        { id: 4, name: '葡萄', color: '紫色' },
      ],
    };
  },
  methods: {
    onOpened() {
      const grid = this.$refs.dataGrid?.instance;
      if (grid) {
        grid.clearSelection(); // (可选,顺序会重排)清除当前选择,再全选 
        grid.selectAll();  // 注意:selectAll() 会触发 @selection-changed,所以 selectedItems 会由 onSelectionChanged 自动更新
      }
    },

    onSelectionChanged(e) {
      this.selectedItems = e.selectedRowKeys.map(item =>item.id);
    },
  },
};
</script>
相关推荐
kyriewen11 小时前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒11 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
山河木马12 小时前
矩阵专题2-怎么创建视图矩阵(uViewMatrix)
javascript·webgl·计算机图形学
小林攻城狮12 小时前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦12 小时前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer13 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队13 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY13 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_13 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏13 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端