EasyUI Forum
April 24, 2024, 10:14:33 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Datagrid problem on adding row multiple times  (Read 4051 times)
iLLuSia
Newbie
*
Posts: 12


View Profile Email
« on: November 01, 2021, 04:58:34 PM »

Hello,

I have a datagrid:
Code:
$('#dg').datagrid({
columns: [[
{field: 'func', title: '', sortable: false,
formatter: function(value, row, index) {
var s = '<button class="icon-add" onclick="addDatagridRow('+index+')" style="width:16px; height:16px; border:0"></button>';
return s;
}
},
//...

And the function to add a row. It's supposed to add one right below the row, that has been clicked.
Code:
function addDatagridRow(index) {
var row = $('#dg).datagrid('getRows')[index];
$('#dg).datagrid('insertRow', {index:index+1, row:{setup: row.setup, setupid: row.setupid}});
}
When I add one row, everything is fine. But when I add another, the indexes aren't correct anymore. It looks like the value was fixed when the datagrid was initialized.

For example I have 3 rows with indexes:
1, 2, 3
And I add a row in the middle, the indexes are:
1, 2, 3, 3
The last row, although being in the 4th position, the formatter returns the value it has been initialized with.

I'm sorry, I don't know how to better explain this. Is there a way to update the formatter, or the column? Or am I doing something wrong? Thank you in advance for any help.
Logged
jarry
Administrator
Hero Member
*****
Posts: 2262


View Profile Email
« Reply #1 on: November 01, 2021, 07:05:30 PM »

Please follow these steps to solve this issue.
1. Define the formatter function as:
Code:
formatter: function(value, row, index) {
var s = '<button class="icon-add" onclick="addDatagridRow(event)" style="width:16px; height:16px; border:0"></button>';
return s;
}

2. Retrieve the 'index' value before insert a row.
Code:
function addDatagridRow(event) {
var index = $(event.target).closest('.datagrid-row').attr('datagrid-row-index');
index = parseInt(index, 10);
var row = $('#dg).datagrid('getRows')[index];
$('#dg).datagrid('insertRow', {index:index+1, row:{setup: row.setup, setupid: row.setupid}});
}
Logged
iLLuSia
Newbie
*
Posts: 12


View Profile Email
« Reply #2 on: November 01, 2021, 08:47:40 PM »

Thank you very much. This works perfectly.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!