Skip to content

Instantly share code, notes, and snippets.

@stenius
Created June 7, 2011 21:05
Show Gist options
  • Save stenius/1013163 to your computer and use it in GitHub Desktop.
Save stenius/1013163 to your computer and use it in GitHub Desktop.
AJAX JQuery table editor
('table#bal_out tr').delegate('td','click', (function(){
cell = jQuery(this);
row = $(this).closest('tr');
oldValue = cell.html();
rowId = jQuery(row).attr('id');
headerId= cell.closest('table').find('th').eq(cell.index()).attr('id');
if (oldValue.indexOf('button') == -1)
{
if ( isNaN(oldValue))
{
oldValue = '';
}
cell.html('<input type="textfield" value="'+oldValue+'"></input><button class="EditCell">Change</button>')
$('.EditCell').button().click(function(){
cell = $(this).parent();
row = $(this).closest('tr');
rowId = row.attr('id');
headerId= cell.closest('table').find('th').eq(cell.index()).attr('id');
value = $(this).parent().children('input').attr('value');
payload = {'value': value,'field':headerId, 'id':rowId};
$.ajaxSetup({
type: 'POST',
data: payload,
success: function(data){
cell.html(data);
}
});
$.ajax('/biogas/api/1.0/cell/lu/bal_tank_out/');
});
}
/*alert(oldValue + ' lives on row ' + rowId + ' with the header ' + headerId);*/
}));
@stenius
Copy link
Author

stenius commented Jun 7, 2011

just to clarify, each table has id in the table headers used to specify the field in the post request. The rowId is gotten from the id or the row like so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment