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

SlickGrid行详情设置singleRowExpand:true后出现undefined问题求助

解决slickgrid-universal singleRowExpand模式下内容变为undefined的问题
  • 校验索引合法性,修复handleRemoveRow逻辑
    在handleRemoveRow方法中先判断索引是否有效,避免因无效索引获取undefined数据:

    if (idx == null || idx < 0 || idx >= this.dataView.getLength()) {
      return;
    }
    const item = this.dataView.getItemByIdx(idx);
    // 后续业务逻辑
    

    同时确认dataView的idProperty配置与数据主键字段一致,这是行数据定位的核心依据。

  • 自定义单展开行状态管理
    绕过内置的自动切换逻辑,手动维护当前展开行ID,避免内置方法的索引异常:

    let currentExpandedRowId = null;
    
    // 绑定行点击事件处理展开/关闭
    function toggleRowExpand(rowId) {
      if (currentExpandedRowId === rowId) {
        this.grid.collapseRow(rowId);
        currentExpandedRowId = null;
      } else {
        if (currentExpandedRowId) {
          this.grid.collapseRow(currentExpandedRowId);
        }
        this.grid.expandRow(rowId);
        currentExpandedRowId = rowId;
      }
    }
    
  • 同步数据视图与渲染逻辑
    数据更新后确保调用dataView.refresh()或dataView.updateItem()维持视图同步;在分页、滚动加载场景下,重新初始化行详情状态。

  • 给行详情模板加非空校验
    渲染行详情时先判断行数据是否存在,避免直接访问undefined属性:

    function renderRowDetail(rowData) {
      if (!rowData) {
        return '<div>无可用数据</div>';
      }
      return `<div> ${rowData.name} - ${rowData.desc} </div>`;
    }
    

内容的提问来源于stack exchange,提问作者yogesh chikhale

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 16:39:50