Praveen Dhonukshe-Patil Praveen Dhonukshe-Patil - 1 year ago 69
HTML Question

ContentTools - How do I add new row in table?

I want to add a new row in between rows. Currently new row can be added only after last row.
reference: http://getcontenttools.com/

Current JS code

row = new ContentEdit.TableRow();
_ref = cell.parent().children;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
newCell = new ContentEdit.TableCell(child.tagName(), child._attributes);
newCellText = new ContentEdit.TableCellText('');
newCell.attach(newCellText);
row.attach(newCell);
}
section = this.closest(function (node) {
return node.type() === 'TableRow';
});
section.attach(row);

Answer Source

Below code snippet worked for me.

TableCellText.prototype._isLastCell = function () {
        var cell, row, section, table;
        debugger;
        cell = this.parent();
        row = cell.parent();
        section = row.parent();
        table = section.parent();
        if (cell !== row.children[row.children.length - 1]) {
            return false;
        }
        return cell === row.children[row.children.length - 1];
    };

TableCellText.prototype._keyTab = function(ev) {
      var cell, child, grandParent, newCell, newCellText, row, section, _i, _len, _ref;
      ev.preventDefault();
      cell = this.parent();
      if (ev.shiftKey) {
        if (this._isInFirstRow() && cell.parent().children[0] === cell) {
          return;
        }
        return this.previousContent().focus();
      } else {
        if (!this.can('spawn')) {
          return;
        }
        grandParent = cell.parent().parent();
        if (grandParent.tagName() === 'tbody' && this._isLastInSection()) {
          row = new ContentEdit.TableRow();
          _ref = cell.parent().children;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            child = _ref[_i];
            newCell = new ContentEdit.TableCell(child.tagName(), child._attributes);
            newCellText = new ContentEdit.TableCellText('');
            newCell.attach(newCellText);
            row.attach(newCell);
          }
          section = this.closest(function(node) {
            return node.type() === 'TableSection';
          });
          section.attach(row);
          return row.children[0].tableCellText().focus();
        }

    else if (grandParent.tagName() === 'tbody' && this._isLastCell()) {
               row = new ContentEdit.TableRow();
                _ref = cell.parent().children;
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    child = _ref[_i];
                    newCell = new ContentEdit.TableCell(child.tagName(), child._attributes);
                    newCellText = new ContentEdit.TableCellText('');
                    newCell.attach(newCellText);
                    row.attach(newCell);
                }
                section = this.closest(function (node) {
                    return node.type() === 'TableRow';
                });           
                section.parent().attach(row, grandParent.children.indexOf(section));            
                return row.children[0].tableCellText().focus();
            }

        else {
          return this.nextContent().focus();
        }
      }
    };
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download