Kendo Grid 指定位置增加记录

来源:互联网 发布:网络语cbd女生 编辑:程序博客网 时间:2024/06/15 23:29

原文出处:https://stackoverflow.com/questions/24931390/kendo-ui-grid-inline-insert-new-row-at-a-specific-position-on-the-grid

Ask Question
up vote1down votefavorite

I am trying to insert a new row via the addRow() method to the grid but I want it to be added AFTER the currently selected row on the grid OR BEFORE the currently selected row? Can anyone help with this please? At the moment all I got was:

Grid.addRow();                                                    // 在第1行增加

$(".k-grid-edit-row").appendTo("#Grid tbody");   //  在最后1行增加(配合上面的Grid.addRow()用)

But this inserts a row at the bottom of the table. I need it to add rows at specific positions with the grid which would already have rows.

shareimprove this question
 

1 Answer

activeoldestvotes
up vote13down voteaccepted

These are the steps:  (在某行前、后插入记录步骤)

  1. Get the selected item
  2. Get the item corresponding to the selected item.
  3. Get the index corresponding to the item that is selected.
  4. Use insert method in DataSource for specifying the position where you want to insert.

Code:

var grid = $("#grid").data("kendoGrid");// Get selected itemvar sel = grid.select();// Get the itemvar 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 beforegrid.dataSource.insert(idx, { /* Your data */ });// Insert element aftergrid.dataSourcer.insert(idx + 1, { /* Your data */ });

See it in action here: http://jsfiddle.net/OnaBai/8J4kr/ 

EDIT If after inserting the row you want to enter that row in edit mode, you have to remember the position inside the page where you are inserting since editRow works at DOM level:

var grid = $("#grid").data("kendoGrid");// Get selected itemvar sel = grid.select();// Remember index of selected element at page levelvar sel_idx = sel.index(); // Get the itemvar 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 rowgrid.editRow($("#grid tr:eq(" + ( sel_idx + 1) + ")"));    

For after you should do

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

See it in action here: http://jsfiddle.net/OnaBai/8J4kr/1/

There is an additional question that is that you might need to move to previous or next page depending if you are inserting before the first row of a page or after the last row of a page (use page for moving between Grip pages).

shareimprove this answer
 
 
Thank you for your response. However, in my case I am using inline editing, so I cant really state static data within the grid.datasource.insert(). And only one column is editable which is a dropdownlist. Is there a way of showing the row and the user selecting from the dropdownlist and then it being saved at the correct position? – Simmy Dhanda Jul 24 '14 at 11:08
 
I'm not getting what does a dropdownlist have to do with programmatically add a row at a specific position. What is what you are going to insert? When is this inserted? What triggers the insertion? Can you review you original question and put there all the relevant information? – OnaBai Jul 24 '14 at 11:13
 
I want to addRow() to the grid for user todo inline editing. But at the moment by default the row gets added either at the top of the grid or there is a work around and it can be added at the bottom of the grid. What I want is for the row to be added not at the bottom or at the top but after or before a selected row already on the grid. In others words once row is added at a specific position for it to be in edit mode? – Simmy DhandaJul 24 '14 at 11:20
1 
See EDIT and the link to a new JSFiddle – OnaBai Jul 24 '14 at 11:46