EasyUI Forum

General Category => General Discussion => Topic started by: aemalsayer on August 12, 2013, 10:17:43 PM



Title: Insert a Row in a Datagrid without mentioning column names
Post by: aemalsayer 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 }
});


Title: Re: Insert a Row in a Datagrid without mentioning column names
Post by: stworthy 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
});