EasyUI Forum
September 14, 2025, 04:45:12 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: Editable subgrid [Solved]  (Read 19234 times)
thecyberzone
Full Member
***
Posts: 176



View Profile Email
« on: January 06, 2015, 01:58:29 AM »

How can I make an editable subgrid ? Any example ?
« Last Edit: January 08, 2015, 01:48:13 AM by thecyberzone » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: January 06, 2015, 06:57:58 PM »

Please try this code:
Code:
$('#dg').datagrid({
title:'DataGrid',
width:500,
height:250,
    fitColumns:true,
    view:detailview,
    columns:[[
        {field:'company',title:'Company Name',width:200},
        {field:'contact',title:'Contact Name',width:200},
        {field:'country',title:'Country',width:200}
    ]],
    data:[
        {company:'Speed Info',contact:'Minna John',country:'Sweden'},
        {company:'And Info',contact:'Mini Adseng',country:'England'}
    ],
    detailFormatter:function(){
     return '<div style="padding:2px"><table class="ddv"></table></div>';
    },
    onExpandRow:function(index,row){
     var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
     ddv.edatagrid({
            fitColumns:true,
            idField:'id',
            columns:[[
                {field:'orderdate',title:'Order Date',width:200,editor:'datebox'},
                {field:'shippeddate',title:'Shipped Date',width:200,editor:'datebox'},
                {field:'freight',title:'Freight',width:200,editor:'numberbox'}
            ]],
            data:[
                {id:1,orderdate:'08/23/2012',shippeddate:'12/25/2012',freight:9734},
                {id:2,orderdate:'08/02/2012',shippeddate:'12/05/2012',freight:9735}
            ],
            onLoadSuccess:function(){
                setTimeout(function(){
                    $('#dg').datagrid('fixDetailRowHeight',index);
                },0);
            }
     });
     $('#dg').datagrid('fixDetailRowHeight',index);
    }
});
Logged
thecyberzone
Full Member
***
Posts: 176



View Profile Email
« Reply #2 on: January 06, 2015, 10:16:48 PM »

I have tried above code, it works fine with edatagrid. But in my project I have to update each subgird row data in the underlying table as soon as it changed or lost focus. So in my project I could not use edatagrid rather I have tried by implementing Cell Editing feature of subgrid. Just have a look at the following code snippet:

Code:
<script type="text/javascript">
$(function(){
$('#dg1').datagrid({
view: detailview,
detailFormatter:function(index,row){
return '<div style="padding:2px"><table id="dg2" class="ddv" ></table></div>';
},
onExpandRow: function(index,row){

var yr = $('#year').combobox('getValue');
var g = $('#cg').combogrid('grid');
var s = g.datagrid('getSelected');
var j = encodeURIComponent($('#jobpos').combobox('getValue'));

var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
ddv.datagrid({

method: 'get',
url:'rating.getSkills.php?yr='+yr+'&secode='+s.secode+'&jobpos='+j+'&tno='+row.tno,
onClickCell: onClickCell,

fitColumns:true,
singleSelect:true,
rownumbers:true,
loadMsg:'loading...',
height:'auto',

columns:[[
     {field:'year',title:'',width:0,hidden:true},  
     {field:'tno',title:'',width:0,hidden:true},
     {field:'skname',title:'Functional Competencies',width:250},
     {field:'sktype',title:'',width:0,hidden:true},
     {field:'mrating_ass',title:'Rating',width:60,
     editor:{
     type:'combobox',
     options:{
     valueField:'mrating_ass',
     textField:'mrating_ass',
data: [ {mrating_ass:'X', mrating_ass:'X'},
{mrating_ass:'Y', mrating_ass:'Y'},
{mrating_ass:'Z', mrating_ass:'Z'},
{mrating_ass:'NA', mrating_ass:'NA'}
],
panelHeight:90,
required:true
     }
     }
     },
     {field:'mapprog',title:'Associated Module',width:380},
     {field:'trgdate',title:'Plan Date',width:100,
     editor:{
     type:'combobox',
     options:{
     valueField:'trgdate',
     textField:'trgdate',
data: [{trgdate:'', trgdate:''}]
     }
     }
     }
     ]],

onResize:function(){
$('#dg1').datagrid('fixDetailRowHeight',index);
},
onLoadSuccess:function(){
setTimeout(function(){
$('#dg1').datagrid('fixDetailRowHeight',index);
},0);
}
});
$('#dg1').datagrid('fixDetailRowHeight',index);
}
});
});
</script>

Here editCell method has been implemented as follows. What I have seen that there may be some problem to reference the subgrid itself or each cell of the subgrid and that's why method endEdit is not functioning properly, after end of editing the focus does not goes anywhere if I click on the other cell. Cell Editing code is given below :

Code:
 	$.extend($.fn.datagrid.methods, {
editCell: function(jq,param){
return jq.each(function(){
var opts = $(this).datagrid('options');
var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
for(var i=0; i<fields.length; i++){
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor1 = col.editor;
if (fields[i] != param.field){
col.editor = null;
}
}
$(this).datagrid('beginEdit', param.index);
for(var i=0; i<fields.length; i++){
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor = col.editor1;
}
});
}
});

  var editIndex = undefined;
 
function endEditing(){

if (editIndex == undefined){return true}

var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');       // here I am tring to reference subgrid

if (ddv.datagrid('validateRow', editIndex)){                   // may be problem here to reference subgid

ddv.datagrid('endEdit', editIndex);                      // may be problem here to reference subgid
editIndex = undefined;

var yr = $('#year').combobox('getValue');
var rec = ddv.datagrid('getSelected');                // same way I have to reference subgrid row here

$.get("rating2.skUpdate.php?yr="+yr+"&tno="+rec.tno+"&skname="+encodeURIComponent(rec.skname)+"&mra="+rec.mrating_ass);

return true;
} else {
return false;
}
}

function onClickCell(index, field){
if (endEditing()){
$(this).datagrid('selectRow', index)
.datagrid('editCell', {index:index,field:field});
editIndex = index;
}
}



If you please rectify this cell editing code it would be very useful to me. Thanking you once again.

Complete code and screenshot you may view at http://www.jeasyui.com/forum/index.php?topic=4319.0
« Last Edit: January 06, 2015, 11:32:29 PM by thecyberzone » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 on: January 06, 2015, 11:55:51 PM »

To use the cell editing feature, please call the 'enableCellEditing' method that extended in http://www.jeasyui.com/demo/main/index.php?plugin=DataGrid&theme=default&dir=ltr&pitem=Cell%20Editing%20in%20DataGrid.

For more information please refer to this example http://jsfiddle.net/b044e3p6/
Logged
thecyberzone
Full Member
***
Posts: 176



View Profile Email
« Reply #4 on: January 07, 2015, 12:53:52 AM »

Everything has been solved!

What I have done in onClickCell function when I am calling endEditing function, just I am sending $(this) as current datagrid and in endEditing function parameter ddv is used to reference that subgrid. rectified code is given below :

Code:
 	var editIndex = undefined;
 
function endEditing(ddv){
if (editIndex == undefined){return true}
if (ddv.datagrid('validateRow', editIndex)){

ddv.datagrid('endEdit', editIndex);
editIndex = undefined;

var yr = $('#year').combobox('getValue');
var rec = ddv.datagrid('getSelected');
$.get("rating2.skUpdate.php?yr="+yr+"&tno="+rec.tno+"&skname="+encodeURIComponent(rec.skname)+"&mra="+rec.mrating_ass);

return true;
} else {
return false;
}
}

function onClickCell(index, field){
if (endEditing($(this))){
$(this).datagrid('selectRow', index)
.datagrid('editCell', {index:index,field:field});
editIndex = index;
}
}

And thanks everybody for helping me.
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!