跳到主要内容

useSelection

获取当前单元格光标所选择的区域的 recordId 和 fieldId。 当光标移动或者切换视图的时候,会触发重新渲染。

如果你仅需要激活单元格的信息,可以使用 useActiveCell

返回值

undefined | { recordIds: string[] ; fieldIds: string[] }

示例

import { useSelection, useRecords, useFields, useActiveViewId } from '@apitable/widget-sdk';

// 渲染当前选区信息
function Selection() {
const selection = useSelection();
const viewId = useActiveViewId();
const records = useRecords(viewId, { ids: selection?.recordIds });
const fields = useFields(viewId, { ids: selection?.fieldIds });

return (<table>
<thead>
<tr>
{fields.map(field => <th key={field.id}>{field.name || '_'}</th>)}
</tr>
</thead>
<tbody>
{records.map(record =>
<tr key={record.id}>
{fields.map(field =>
<td key={field.id}>{record.getCellValueString(field.id) || '_'}</td>
)}
</tr>
)}
</tbody>
</table>);
}