javascript - Kendo UI Grid Inline - 在网格的特定位置插入新行

标签 javascript jquery kendo-ui kendo-grid

我正在尝试通过 addRow() 方法向网格中插入一个新行,但我希望将其添加到网格上当前选定行之后或当前选定行之前?有人可以帮忙吗?目前我得到的是:

Grid.addRow();

$(".k-grid-edit-row").appendTo("#Grid tbody");

但这会在表格底部插入一行。我需要它在特定位置添加行,网格已经有行。

最佳答案

这些是步骤:

  1. 获取选中的项
  2. 获取所选项目对应的项目。
  3. 获取选中项对应的索引。
  4. 使用DataSource中的insert方法指定要插入的位置。

代码:

var grid = $("#grid").data("kendoGrid");
// Get selected item
var sel = grid.select();
// Get the item
var item = grid.dataItem(sel);
// Get the index in the DataSource (not in current page of the grid)
var idx = grid.dataSource.indexOf(item);
// Insert element before
grid.dataSource.insert(idx, { /* Your data */ });
// Insert element after
grid.dataSourcer.insert(idx + 1, { /* Your data */ });

在此处查看实际效果:http://jsfiddle.net/OnaBai/8J4kr/

EDIT 如果在插入行后你想在编辑模式下输入该行,你必须记住你要插入的页面内的位置,因为 editRow 工作在DOM 级别:

var grid = $("#grid").data("kendoGrid");
// Get selected item
var sel = grid.select();
// Remember index of selected element at page level
var sel_idx = sel.index(); 
// Get the item
var item = grid.dataItem(sel);
// Get the index in the DataSource (not in current page of the grid)
var idx = grid.dataSource.indexOf(item);
// Insert element before (use {} if you want an empty record)
grid.dataSource.insert(idx, { LastName: "Smith", FirstName: "John", City: "Baiona" });
// Edit inserted row
grid.editRow($("#grid tr:eq(" + ( sel_idx + 1) + ")"));    

对于你应该做的事

// Insert element before (use {} if you want an empty record)
grid.dataSource.insert(idx + 1, { LastName: "Smith", FirstName: "John", City: "Baiona" });
// Edit inserted row
grid.editRow($("#grid tr:eq(" + ( sel_idx + 2) + ")"));  

在此处查看实际效果:http://jsfiddle.net/OnaBai/8J4kr/1/

还有一个问题是,您可能需要移动到上一页或下一页,具体取决于您是在页面的第一行之前还是在页面的最后一行之后插入(使用 page 用于在 Grip 页面之间移动)。

关于javascript - Kendo UI Grid Inline - 在网格的特定位置插入新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931390/

相关文章:

javascript - 你可以隐藏由数组渲染的某些 react 片段吗?

jquery - 为什么将 Backbone.js 与 Rails 结合使用?

ajax - XmlHttpRequest 在 IE 中是否比在 Chrome 中花费更多时间?

angular - Kendo UI Tabstrip Onclick 事件

javascript - 最新 Kendo 版本的页面加载缓慢问题

javascript - 什么时候使用 stopPropagation() 的好习惯?

javascript - 验证输入元素后提交 Ajax 表单

javascript - 我如何从 CasperJS 访问 iframe?

javascript - 在同一页面的 div 中加载 PHP 回显链接的内容

javascript - Bootstrap 导航选项卡 : active class doesnt work