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

如何在ag-grid表格列中实现进度条?求相关参考资源

Adding Progress Bars to AG Grid Columns

Hey there! Even though the official AG Grid docs don’t have a dedicated "progress bar" section, you can easily build this using custom cell renderers—it’s one of the grid’s most powerful customization tools. Here’s how the community typically approaches this:

1. Pure JavaScript Cell Renderer (Framework-Agnostic)

This works for vanilla JS setups. Create a reusable renderer function that generates the progress bar HTML dynamically:

// Define the progress bar renderer
const progressBarRenderer = function(params) {
  const progress = params.value || 0;
  
  // Create outer container
  const container = document.createElement('div');
  container.style.width = '100%';
  container.style.height = '20px';
  container.style.border = '1px solid #ddd';
  container.style.borderRadius = '4px';
  container.style.overflow = 'hidden';
  container.style.position = 'relative';

  // Create progress fill
  const fill = document.createElement('div');
  fill.style.width = `${progress}%`;
  fill.style.height = '100%';
  fill.style.backgroundColor = progress > 70 ? '#ef4444' : progress > 40 ? '#f59e0b' : '#10b981';
  fill.style.transition = 'width 0.3s ease';

  // Add percentage label (centered)
  const label = document.createElement('span');
  label.style.position = 'absolute';
  label.style.top = '50%';
  label.style.left = '50%';
  label.style.transform = 'translate(-50%, -50%)';
  label.style.color = 'white';
  label.style.fontSize = '12px';
  label.style.fontWeight = '500';
  label.textContent = `${progress}%`;

  container.appendChild(fill);
  container.appendChild(label);
  return container;
};

// Use it in your column definitions
const columnDefs = [
  { headerName: 'Project Name', field: 'name', width: 200 },
  { 
    headerName: 'Completion', 
    field: 'completion', 
    cellRenderer: progressBarRenderer,
    width: 250
  }
];

2. Framework-Specific Components (React/Vue/Angular)

If you’re using a framework, you can leverage its component system for cleaner code. Here’s a React example:

// React component for progress bar cells
const ProgressBarCell = ({ value }) => {
  const progress = value || 0;
  const barColor = progress > 70 ? '#ef4444' : progress > 40 ? '#f59e0b' : '#10b981';

  return (
    <div className="progress-bar-container">
      <div 
        className="progress-bar-fill"
        style={{ width: `${progress}%`, backgroundColor: barColor }}
      />
      <span className="progress-label">{progress}%</span>
    </div>
  );
};

// Add CSS (in your stylesheet)
.progress-bar-container {
  width: 100%;
  height: 20px;
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.progress-bar-fill {
  height: 100%;
  transition: width 0.3s ease;
}

.progress-label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 12px;
  font-weight: 500;
}

// Column definition in AG Grid
const columnDefs = [
  { headerName: 'Project Name', field: 'name', width: 200 },
  {
    headerName: 'Completion',
    field: 'completion',
    cellRenderer: 'agReactCellRenderer',
    cellRendererParams: {
      reactComponent: ProgressBarCell
    },
    width: 250
  }
];

3. Pro Tips from the Community

  • Dynamic Updates: Ensure your data source is reactive (e.g., React state, Vue ref) and AG Grid will automatically re-render the progress bar when values change.
  • Tooltip Enhancements: Add a tooltip to show extra details (like target completion dates) by setting tooltipField in your column definition, or adding a title attribute to the container element in your renderer.
  • Theming: Match the progress bar styles to your app’s theme by using CSS variables instead of hardcoded colors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:07:04