Skip to content

Instantly share code, notes, and snippets.

@csbuja
Last active August 29, 2015 14:06
Show Gist options
  • Save csbuja/7e025c523013800059a8 to your computer and use it in GitHub Desktop.
Save csbuja/7e025c523013800059a8 to your computer and use it in GitHub Desktop.
A flawed delete function in dynatable
$(document).ready(function(){
TableBuilder = function(username, tabledata){
this.username = username;
this.tabledata = tabledata;
}
TableBuilder.prototype.populateTable = function() {
var self = this;
var tableId = 'my-table';
this.dynatable = $('#'+tableId).dynatable({
dataset: {
records: self.tabledata
}
});
}
TableBuilder.prototype.handleSubmit = function(submitId, op, tableId){
var self = this;
if(op==='delete'){
$('#' + submitId).click(function(){
var title = $('#' + op + 'Box').val();
var shouldRemove = false;
_.each(self.tabledata, function(v, index){
if(v['title'] === title){
if (index > -1) {
self.tabledata.splice(index, 1);
}
shouldRemove = true;
}
});
console.log(self.tabledata)
if(shouldRemove) self.dynatable.records.updateFromJson(self.tabledata);
// $('#dynatable-search-my-table').remove();
// $('#dynatable-processing-my-table').remove()
// $('.dynatable-per-page').remove()
// $('#dynatable-pagination-links-my-table').remove();
// $('#'+tableId + ' tbody').children().remove();
});
}
else if(op==='add'){
$('#' + submitId).click(function(){
var title = $('#' + op + 'Box').val();
$.post("/albums/edit", { 'op': op, 'title': title, 'username': username}, function( data ) {
self.tabledata = data;
self.populateTable();
}, "json");
});
}
else{
console.log('Needs a valid operation.')
}
}
var tabledata = TABLEDATA;
//process strings
while( true ) {
var temp = tabledata.replace('"', '"');
if(temp === tabledata) {
break;
}
else {
tabledata = temp;
}
}
tabledata = JSON.parse(tabledata)
var theTableBuilder = new TableBuilder(USERNAME, tabledata);
var tableId = 'my-table';
theTableBuilder.populateTable();
theTableBuilder.handleSubmit('deleteSubmit','delete',tableId);
theTableBuilder.handleSubmit('addSubmit','add',tableId);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment