# 计划:在 addInsertRows 中添加跳转到新增行第一页的功能 ## 需求分析 在 `addInsertRows` 函数执行后,自动跳转到新增行所在的第一个页面。如果当前页已经是新增行所在的页,则不进行跳转。 ## 代码分析 ### 相关函数位置 * **addInsertRows**: [feegrid-manager.js:832-843](file:///d:/workspace/code/jsp/js/feegrid-manager.js#L832-L843) * **loadPage**: [feegrid-manager.js:644-743](file:///d:/workspace/code/jsp/js/feegrid-manager.js#L644-L743) * **pagination 状态**: state.pagination(包含 pageNumber, pageSize, remoteTotal) ### 数据结构理解 1. `state.cacheManager.getInsertRows()` - 获取所有新增行(draftState === 'add') 2. `state.pagination.remoteTotal` - 远程数据的总条数 3. 新增行始终位于所有数据的最末端(即 remoteTotal 之后) ### 分页计算逻辑 * 总数据量 = remoteTotal + insertRows.length * 新增行的起始位置 = remoteTotal * 新增行第一行所在的页码 = Math.ceil((remoteTotal + 1) / pageSize) * 或者:Math.floor(remoteTotal / pageSize) + 1 ## 实现步骤 ### Step 1: 修改 addInsertRows 函数 在 `addInsertRows(rows)` 函数末尾添加逻辑: 1. 获取所有新增行 `insertRows = state.cacheManager.getInsertRows()` 2. 如果 `insertRows.length === 0`,直接返回(不应该发生,但做防御性检查) 3. 计算新增行第一行所在的页码: * `var remoteTotal = state.pagination.remoteTotal` * `var pageSize = state.pagination.pageSize` * `var firstInsertPage = Math.floor(remoteTotal / pageSize) + 1` * (或者 `Math.ceil((remoteTotal + 1) / pageSize)`) 4. 获取当前页码 `var currentPage = state.pagination.pageNumber` 5. 如果 `firstInsertPage !== currentPage`,则调用 `loadPage(firstInsertPage)` 跳转 ### Step 2: 代码放置位置 在 `addInsertRows` 函数的 for 循环之后(行842之后),在函数结束的大括号之前(行843之前)插入新逻辑。 ## 预期代码变更 ```javascript function addInsertRows(rows) { if (!$.isArray(rows)) rows = [rows]; var cm = state.cacheManager; for (var i = 0; i < rows.length; i++) { var row = rows[i]; var key = cm.getRowKey(row); if (!cm.getRowFromCache(key)) { cm.setRowToCache(row, true); } } // 新增:跳转到新增行所在的第一页 var insertRows = state.cacheManager.getInsertRows(); if (insertRows.length > 0) { var remoteTotal = state.pagination.remoteTotal; var pageSize = state.pagination.pageSize; var firstInsertPage = Math.floor(remoteTotal / pageSize) + 1; var currentPage = state.pagination.pageNumber; if (firstInsertPage !== currentPage) { loadPage(firstInsertPage); } } } ```