Load children and compare scoped totals
Explicit parent IDs, recoverable child loads and separate loaded versus full-match totals keep the scope clear.
Keyboard
- Use the first column Right arrow to expand a parent; Left collapses it or returns to its parent. Reading direction reverses these keys.
- Collapsed and unknown children are excluded from grid ranges. Draft values remain in controlled host records.
- Retry a failed child load; source changes abort and ignore stale responses.
- Open a record to expand its separate detail panel without changing fixed virtual row heights.
States covered
- treegrid
- lazy children
- abortable retry
- stable row IDs
- controlled edits
- expanded details
- loaded totals
- query-matched service totals
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-data-grid-hierarchy-summaries.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useCallback, useMemo, useRef, useState } from 'react';
import { AdvancedDataGrid, Button, Checkbox, parseDataValue, type DataColumn, type DataEdit, type DataGridTree, type DataSummarySpec } from '@bld8/ui';
type WorkRow={id:string;revision:number;parent:string;title:string;hours:number;more:boolean};
const roots:WorkRow[]=[{id:'P-A',revision:1,parent:'',title:'Interface review',hours:0,more:true},{id:'P-B',revision:1,parent:'',title:'Release checks',hours:0,more:true}];
const children:Record<string,WorkRow[]>={
'P-A':[{id:'A-1',revision:1,parent:'P-A',title:'Keyboard review',hours:3,more:false},{id:'A-2',revision:1,parent:'P-A',title:'Spacing review',hours:5,more:false}],
'P-B':[{id:'B-1',revision:1,parent:'P-B',title:'Archive interaction',hours:2,more:false},{id:'B-2',revision:1,parent:'P-B',title:'Saved view restore',hours:4,more:false}]
};
const columns:DataColumn<WorkRow>[]=[{key:'id',label:'Record',type:'text',pinned:'start',width:180,hideable:false},{key:'title',label:'Title',type:'text',editable:true,width:260,maxLength:120},{key:'hours',label:'Hours',type:'number',editable:true,min:0,max:100,width:112}];
const summaries:DataSummarySpec<WorkRow>[]=[{id:'count',label:'Visible records',operation:'count'},{id:'sum',label:'Hours',operation:'sum',key:'hours'},{id:'avg',label:'Hours per record',operation:'average',key:'hours'}];
const matching={queryKey:'all-work',recordCount:6,values:[{id:'count',value:6,count:6},{id:'sum',value:14,count:6},{id:'avg',value:14/6,count:6}]};
export default function HierarchicalRecordsExample(){
const [rows,setRows]=useState(roots),[sourceKey,setSourceKey]=useState(0),[fail,setFail]=useState(false),[stale,setStale]=useState(false),[notice,setNotice]=useState(''),[ignore,setIgnore]=useState(false),[duplicates,setDuplicates]=useState(false),[acceptedParents,setAcceptedParents]=useState<string[]>([]);const failNext=useRef(false),ignoreNext=useRef(false),duplicateNext=useRef(false),pendingChildren=useRef<{parent:WorkRow;complete:(rows:WorkRow[])=>void}[]>([]);
const loadChildren=useCallback(async(parent:WorkRow,{signal}:{signal:AbortSignal})=>{
if(duplicateNext.current)return new Promise<WorkRow[]>((resolve,reject)=>{
const entry={parent,complete:(rows:WorkRow[])=>{signal.removeEventListener('abort',stop);resolve(rows);}};
function stop(){pendingChildren.current=pendingChildren.current.filter(item=>item!==entry);reject(new DOMException('Aborted','AbortError'));}
if(signal.aborted){stop();return;}pendingChildren.current.push(entry);signal.addEventListener('abort',stop,{once:true});
});
const ignoreAbort=ignoreNext.current;ignoreNext.current=false;setIgnore(false);
await new Promise<void>((resolve,reject)=>{const timer=setTimeout(done,ignoreAbort?350:120);function stop(){clearTimeout(timer);reject(new DOMException('Aborted','AbortError'));}function done(){signal.removeEventListener('abort',stop);resolve();}if(!ignoreAbort){if(signal.aborted){stop();return;}signal.addEventListener('abort',stop,{once:true});}});
if(failNext.current){failNext.current=false;setFail(false);throw new Error('Example child load failed.');}return children[parent.id]??[];
},[]);
const onChildrenLoaded=useCallback((parent:WorkRow,loaded:readonly WorkRow[])=>{setAcceptedParents(current=>[...current,parent.id]);setRows(current=>{const next=new Map(current.map(row=>[row.id,row]));loaded.forEach(row=>next.set(row.id,row));return [...next.values()];});},[]);
const tree=useMemo<DataGridTree<WorkRow>>(()=>({parentId:row=>row.parent||null,hasChildren:row=>row.more,loadChildren,onChildrenLoaded}),[loadChildren,onChildrenLoaded]);
function edit(edits:DataEdit<WorkRow>[]){const values=new Map<string,Partial<WorkRow>>();for(const change of edits){const column=columns.find(column=>column.key===change.key);if(!column)return;const parsed=parseDataValue(column,change.value);if(parsed.error){setNotice(parsed.error);return;}values.set(change.id,{...values.get(change.id),[change.key]:parsed.value});}setRows(current=>current.map(row=>({...row,...values.get(row.id)})));setNotice('Changes stay in this local example.');}
return <div style={{minWidth:0}}>
<div style={{display:'flex',gap:12,flexWrap:'wrap',alignItems:'center'}}><Checkbox label="Fail next child load" checked={fail} onCheckedChange={value=>{failNext.current=value;setFail(value);}}/><Checkbox label="Show stale totals" checked={stale} onCheckedChange={setStale}/><Checkbox label="Ignore cancellation once" checked={ignore} onCheckedChange={value=>{ignoreNext.current=value;setIgnore(value);}}/><Checkbox label="Concurrent duplicate children" checked={duplicates} onCheckedChange={value=>{duplicateNext.current=value;setDuplicates(value);}}/><Button size="sm" variant="secondary" onClick={()=>{const pending=pendingChildren.current.splice(0);pending.forEach(entry=>entry.complete([{id:'SHARED',revision:1,parent:entry.parent.id,title:'Shared child of '+entry.parent.id,hours:1,more:false}]));}}>Release pending child loads</Button><Button size="sm" variant="secondary" onClick={()=>{setSourceKey(key=>key+1);setRows(roots);setAcceptedParents([]);}}>Reset tree source</Button></div>
<output aria-label="Accepted child batches">{JSON.stringify(acceptedParents)}</output>
<AdvancedDataGrid label="Project task hierarchy" rows={rows} columns={columns} tree={tree} sourceKey={sourceKey} onEdit={edit} height={360} layoutControls bulkEditing summaries={summaries} queryKey={stale?'another-query':'all-work'} allMatchingSummary={matching} renderDetails={row=><p>{row.title} · {row.hours} hours · saved revision {row.revision}.</p>}/>
<p className="bui-hint">The duplicate-child mode deliberately releases two conflicting responses in the same event turn. Only a validated batch reaches the host callback; retry after disabling it. The full-match totals above are authored fixtures representing a service response. Parents have zero hours and are included in the record count. Real services must return totals for the exact applied query. Uploads, authentication and durable writes are separate host concerns.</p><p role="status">{notice}</p>
</div>;
}