EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on May 14, 2013, 09:05:17 PM



Title: Datagrid Append getting data from previous row [solved]
Post by: devnull on May 14, 2013, 09:05:17 PM
Hi;

For certain columns, I need to get the column.value from the previous row when appending a new row.

Any suggestions on how this can be done from within data-options > columns > field ?

Thanks


Title: Re: Datagrid Append getting data from previous row
Post by: stworthy on May 14, 2013, 11:34:44 PM
Call 'getColumnOption' method to get a specified column's option which contains your customized value.

Code:
<th data-options="field:'name',value:'abc',...">
  ...
</th>
Code:
var col = $('#dg').datagrid('getColumnOption', 'name');
var value = col.value;  // get the value defined in 'data-options'
alert(value);


Title: Re: Datagrid Append getting data from previous row
Post by: devnull on May 15, 2013, 12:18:04 AM
Thanks so much for helping, appreciate your patience !

I didn't explain very weel, is it possible to do this using code inside the column definition, maybe by using a custom editor something like this:

Code:
{field:'SEQUENCE_NO',title:'Sequence',editor: {type:text, function(){return $(this).datagrid('getRows')[index -1].value;}} },

I want to pull the value from the previous row and have the option to overwrite the value.


Title: Re: Datagrid Append getting data from previous row
Post by: stworthy on May 15, 2013, 08:12:20 PM
Don't clearly know what you want to do. If want to set the column's customized value when end editing a row, try the code below:
Code:
// extend your custom text editor
$.extend($.fn.datagrid.defaults.editors,{
    customtext:{
        init: function(container, options){ 
            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
    input.data('customtext',{options:options});
            return input; 
        }, 
        getValue: function(target){
var value = $(target).val();
var opts = $(target).data('customtext').options;
if (opts.callback){
opts.callback.call(target, value);
}
            return value; 
        }, 
        setValue: function(target, value){ 
            $(target).val(value);
        }, 
        resize: function(target, width){ 
            $(target)._outerWidth(width);
        } 
    }
});

Apply the 'customtext' editor to a column.
Code:
<th data-options="field:'attr1',width:250,editor:{
type:'customtext',
options:{
callback:function(value){
var col = $('#dg').datagrid('getColumnOption','attr1');
col.value = value;
}
}
}">Attribute</th>


Title: Re: Datagrid Append getting data from previous row
Post by: devnull on May 15, 2013, 10:04:45 PM
Hi;

See attached screen shot.

When a new row is inserted, I want to get the "Seq No" value from the previous row and add 10 to it.

The previous Seq No was 60, and so this row would default to 70 (previous + 10), however the user could override this default if they wanted to.

But the problem is that I need to be able to do this from the table column definition and not by using a custom on-insert function.


Title: Re: Datagrid Append getting data from previous row
Post by: stworthy on May 16, 2013, 12:30:07 AM
Try the code below:
Code:
var dg = $('#dg');
var rows = dg.datagrid('getRows');
var lastIndex = rows.length - 1;
dg.datagrid('appendRow',{seq: rows[lastIndex].seq+10});
dg.datagrid('selectRow', lastIndex+1).datagrid('beginEdit', lastIndex+1);


Title: Re: Datagrid Append getting data from previous row
Post by: devnull on May 16, 2013, 12:47:24 AM
Thanks, but I needed for this to be done inside the datagrid column definition, this now works:

Code:
$.extend($.fn.datagrid.defaults.editors, {  
    xtext: { 
        init: function(container, options){ 
            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container); 
            input.data('xtext',{options:options});
            return input; 
        },
        getValue: function(target){ 
            return $(target).val(); 
        }, 
        setValue: function(target, value){ 
            var opts = $(target).data('xtext').options;
            if (opts.callback) value = opts.callback.call(target, value);
            $(target).val(value); 
        }

}});

Code:
editor:{
type:'xtext',
options:{ callback: function(target, value) {
var lrow = $('#t_dg_op').datagrid('getRows').length - 2;
return parseInt($('#t_dg_op').datagrid('getRows')[lrow].SEQUENCE_NO)+10;
}}
}},