1 var removeRow = function() {
2 var cnt = rowCount(this);
3 var table = $(this).closest('table');
4 console.log("row count = " + cnt);
6 var target = $(this).closest('tr');
12 var rowCount = function(row) {
13 var top = $(row).closest('table');
14 var trs = $(top).find('tr');
15 return trs.length - 1; // exclude th section header row
18 var reorder = function(table) {
19 var trs = $(table).find('tr').has('td');
21 $(trs).find('a').hide();
23 $(trs).each(function(i, tr) {
24 console.log("ROW " + i);
25 $(tr).find('a.remove_row').show();
26 replaceIndex(rowIndex(tr), i);
29 $(trs).last().find('a.add_row').show();
31 if (trs.length == 1) {
32 $(trs).find('a.remove_row').hide();
37 var rowIndex = function(elem) {
38 var idStr = $(elem).find('div').attr('id');
39 var id = idStr.match(/\d+/);
40 console.log("id = " + id);
43 var replaceIndex = function(elem, oldIndex, newIndex) {
44 $(elem).find('*').each(function() {
45 $.each(this.attributes, function(i, attrib) {
46 var regexp = /extprofile-.*-\d.*/;
47 var value = attrib.value;
48 var match = value.match(regexp);
50 attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
51 console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
57 var resetRow = function(elem) {
58 $(elem).find('input').attr('value', '');
59 $(elem).find("select option[value='office']").attr("selected", true);
62 var addRow = function() {
63 var divId = $(this).closest('div').attr('id');
64 var index = divId.match(/\d+/);
65 console.log("Current row = " + index);
66 var tr = $(this).closest('tr');
67 var newtr = $(tr).clone();
68 var newIndex = parseInt(index) + 1;
69 replaceIndex(newtr, index, newIndex);
72 console.log("number of rows: " + rowCount(tr));
73 reorder($(this).closest('table'));
79 $('.add_row').live('click', addRow);
80 $('.remove_row').live('click', removeRow);