You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求帮助:当dx-select-box选中'x'时自动取消dx-check-box勾选

解决DevExtreme中SelectBox选中特定值时自动取消CheckBox勾选的问题

直接修改rowData.data.isChannel = false无效的核心原因是:DevExtreme单元格模板的上下文对象是组件视图模型的包装层,单纯修改数据对象无法触发Angular的变更检测,导致CheckBox的UI无法同步更新。以下是可行的解决方法:

方法一:通过单元格上下文获取行数据并触发变更检测

在setNames函数中,利用模板传递的data对象获取当前行数据,更新isChannel后手动触发变更检测(适用于Angular项目):

import { ChangeDetectorRef } from '@angular/core';
import { DxDataGridComponent } from 'devextreme-angular';

@Component({
  // 组件元数据
})
export class YourComponent {
  @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;

  constructor(private cdr: ChangeDetectorRef) {}

  setNames(event: any, cellData: any) {
    // 更新SelectBox的选中值
    cellData.value = event.value;

    if (event.value === 'x') {
      // 获取当前行的数据源对象
      const rowData = cellData.row.data;
      // 取消CheckBox勾选状态
      rowData.isChannel = false;

      // 触发Angular变更检测,让CheckBox同步更新
      this.cdr.detectChanges();
      // 可选:刷新DataGrid当前行,确保数据完全同步
      this.dataGrid.instance.refresh(cellData.row.key);
    }
  }

  // 其他业务方法...
}

方法二:通过数据源API更新数据(适用于使用Store的场景)

如果你的DataGrid使用CustomStore或ObservableStore,直接调用Store的update方法更新数据,组件会自动同步UI:

import { CustomStore } from 'devextreme/data/custom_store';

// 假设你的DataGrid数据源是CustomStore实例
yourDataStore: CustomStore;

setNames(event: any, cellData: any) {
  cellData.value = event.value;

  if (event.value === 'x') {
    const rowKey = cellData.row.key;
    // 通过Store更新数据,UI会自动刷新
    this.yourDataStore.update(rowKey, { isChannel: false });
  }
}

关键注意点

  • 确认SelectBox模板中的data对象包含row属性:DevExtreme的单元格模板上下文会自动注入当前行信息,通过cellData.row.data可直接访问行数据源。
  • CheckBox的双向绑定必须正确:[(value)]="data.value"需与dxi-column的dataField="isChannel"对应,确保数据路径一致。

内容的提问来源于stack exchange,提问作者Aaron Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:40:38