下拉默认全选,选择展示对象的字段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>
相关推荐
小码哥_常6 分钟前
Android消息机制:Handler、Looper和Message的深度剖析
前端
小码哥_常8 分钟前
安卓开发新姿势:文件Picker全攻略,无痛适配不再难
前端
happymaker062619 分钟前
web前端学习日记——DAY04
前端·学习
发现一只大呆瓜25 分钟前
React-路由监听 / 跳转 / 守卫全攻略(附实战代码)
前端·react.js·面试
swipe1 小时前
为什么 RAG 一定离不开向量检索:从文档向量化到语义搜索的工程实现
前端·llm·agent
OpenTiny社区2 小时前
AI-Extension:让 AI 真的「看得到、动得了」你的浏览器
前端·ai编程·mcp
IT_陈寒2 小时前
Redis缓存击穿:3个鲜为人知的防御策略,90%开发者都忽略了!
前端·人工智能·后端
竹林8183 小时前
在Web3前端用Node.js子进程批量校验钱包,我踩了这些性能与安全的坑
javascript·node.js
农夫山泉不太甜3 小时前
Tauri v2 实战代码示例
前端
yuhaiqiang3 小时前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试