本章将描述ag-grid的单元格相关的方法包含getFocusedCell setFocusedCell clearFocusedCell tabToNextCell tabToPreviousCell
//获取当前焦点的单元格
var cell=gridOptions.api.tabToNextCell();
//cell对象为{rowIndex: 0, column: Column, floating: null}
//设置name列的2行获得焦点
gridOptions.api.setFocusedCell(1,'name');
//清除单元格焦点
gridOptions.api.clearFocusedCell();
//设置下一个单元格为当前焦点
gridOptions.api.tabToNextCell();
//设置上一个单元格为当前焦点
gridOptions.api.tabToPreviousCell();
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>ag-grid 单元格焦点例子</title>
<script src="https://www.itxst.com/package/ag-grid/ag-grid-community.js"></script>
<link href="https://www.itxst.com/package/ag-grid/styles/ag-grid.css" rel="stylesheet" />
</head>
<body>
<div style="margin-bottom: 10px;">
<input type="button" value="setFocusedCell" onclick="func2()" />
<input type="button" value="getFocusedCell" onclick="func1()" />
<input type="button" value="clearFocusedCell" onclick="func3()" />
<input type="button" value="tabToNextCell" onclick="func4()" />
<input type="button" value="tabToPreviousCell" onclick="func5()" />
</div>
<div id="itxst" style="width: 100%;height: 300px;" class="ag-theme-balham"></div>
<script>
//定义列
var colData = [
{ headerName: '名称', field: 'name', 'pinned': 'left' },
{ headerName: '剩余', field: 'col2' },
{ headerName: '销量', field: 'col3' },
];
//行数据
var rowData = [];
//模拟数据
for (var i = 1; i < 10; i++) {
rowData.push({ name: 'YO-' + i, col2: 612 * i, col3: 321 * i });
}
//将列和数据赋给gridOptions
var gridOptions = {
columnDefs: colData,
rowData: rowData,
enableCellChangeFlash: true,
onGridReady: function () {
gridOptions.api.sizeColumnsToFit();
},
defaultColDef: {
enableRowGroup: true,
enablePivot: true,
enableValue: true,
sortable: true,
}
};
//在dom加载完成后 初始化agGrid完成
document.addEventListener("DOMContentLoaded", function () {
var eGridDiv = document.querySelector('#itxst');
new agGrid.Grid(eGridDiv, gridOptions);
});
function func1() {
var cell=gridOptions.api.getFocusedCell();
}
function func2() {
var cell=gridOptions.api.setFocusedCell(3,'name',null);
}
function func3() {
var cell=gridOptions.api.clearFocusedCell();
}
function func4() {
var cell=gridOptions.api.tabToNextCell();
}
function func5() {
var cell=gridOptions.api.tabToPreviousCell();
}
</script>
</body>
</html>