Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

deleteRow functionality

  • Loading branch information...
commit dd588d6ab3235d48bff8ccfeefc83dd242ee60a0 1 parent 4bc9afd
Nathan Reed authored
Showing with 40 additions and 4 deletions.
  1. +24 −3 Source/jsTable.js
  2. +16 −1 Test/test.html
27 Source/jsTable.js
View
@@ -13,8 +13,10 @@ var jsTable = new Class({
this.tbody = null;
this.table_id = (Math.random() * 1000).round();
- this.column_list = [];
+
this.data = [];
+ this.column_list = [];
+ this.row_list = []; // maps row_indexes to row_ids
this._createTable();
},
@@ -28,6 +30,10 @@ var jsTable = new Class({
return 'jst-' + this.table_id + '-' + column_name + '-' + row_id;
},
+ _getRowElementId: function(row_id) {
+ return 'jst-' + this.table_id + '-' + row_id;
+ },
+
_getColumnIndex: function(column_name){
for(var i=0; i < this.column_list.length; i++) {
@@ -85,7 +91,8 @@ var jsTable = new Class({
},
addRow: function() {
- var new_row = $e('tr');
+ var tr_id = this._getRowElementId(this.data.length);
+ var new_row = $e('tr', {'id': tr_id});
var row_data = [];
for(var i=0; i < arguments.length; i++) {
@@ -105,10 +112,24 @@ var jsTable = new Class({
}
this.tbody.grab(new_row);
+ this.row_list.push(this.data.length);
this.data.push(row_data);
+
+ },
+
+ deleteRow: function(row_index) {
+ var row_data = this.data[row_index];
+ var row_id = this.row_list[row_index];
+ var tr_id = this._getRowElementId(row_id);
+
+ this.row_list.erase(row_id);
+ this.data.erase(row_data);
+ $(tr_id).dispose();
+
},
- deleteRow: function(row_id) {
+ deleteRowById: function(row_id) {
+ this.deleteRow(this.row_list.indexOf(row_id));
},
getCell: function(row_id, column_id) {
17 Test/test.html
View
@@ -18,7 +18,9 @@
var Ui = {
init: function() {
- var jst = new jsTable('t1');
+ this.jst = new jsTable('t1');
+ var jst = this.jst;
+
jst.addColumn('id', 'id');
jst.addColumn('desc', 'Description');
@@ -34,6 +36,14 @@
jst.setCell(2, 0, 'TEST2');
$('t2').innerHTML = jst.toJson();
+ },
+
+ delete_row: function() {
+ this.jst.deleteRow(3);
+ },
+
+ delete_last: function() {
+ this.jst.deleteRow(this.jst.rowCount()-1);
}
}
@@ -46,6 +56,11 @@
<div id='t-body'>
<div>
+ <input type='button' value='delete' onclick='Ui.delete_row.bind(Ui)()'>
+ <input type='button' value='delete last' onclick='Ui.delete_last.bind(Ui)()'>
+ </div>
+
+ <div>
<span>get cell by name:</span>
<span id='r1'> </span>
</div>
Please sign in to comment.
Something went wrong with that request. Please try again.