EasyUI Forum
April 29, 2024, 03:21:41 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Insert a Row in a Datagrid without mentioning column names  (Read 14506 times)
aemalsayer
Newbie
*
Posts: 7


View Profile
« on: August 12, 2013, 10:17:43 PM »

I want to add a row into a datagrid without mentioning the Column names. Right now I can Insert a row by using the following syntax:

$('#dg').datagrid('insertRow',{
   index: 1,   // index start with 0
   row: {
      name: 'new name',
      age: 30,
      note: 'some messages'
   }
});


But I want it to be like the following:

$('#dg').datagrid('insertRow',{
   index: 1,   // index start with 0
   row: {
      'new name',
      30,
      'some messages'
   }
});

I want this feature to create a function which should be able to dynamically add sub totals to as many columns of a datagrid as I give it as a parameter.

Or, please show me how to add an array to a row of a datagrid like following:

var varRow = new Array ('new name', 30, 'some messages');

$('#dg').datagrid('insertRow',{
   index: 1,   // index start with 0
   row: { varRow }
});
« Last Edit: August 12, 2013, 10:24:04 PM by aemalsayer » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: August 15, 2013, 01:22:08 AM »

When insert a row to datagrid, the 'column-value' mapping must be specified. If you want to insert a raw array such as ('new name', 30, 'some messages'), this array must be converted to {name:'new name',age:30,note:'some messages'} before insert to datagrid. You could extend a new method to do so.
Code:
$.extend($.fn.datagrid.methods,{
insertRow2: function(jq,param){
return jq.each(function(){
var col1 = $(this).datagrid('getColumnFields',true);
var col2 = $(this).datagrid('getColumnFields',false);
var col = col1.concat(col2);
var row = {};
for(var i=0; i<param.row.length; i++){
row[col[i]] = param.row[i];
}
$(this).datagrid('insertRow', {index:param.index,row:row});
});
}
});
Now call 'insertRow2' method to do what you want.
Code:
var row = ['new name', 30, 'some messages'];
$('#dg').datagrid('insertRow2',{
index:1,
row:row
});
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!