ContentTools - 如何在表格中添加新行?

时间:2017-07-23 12:33:30

标签: javascript jquery html content-management-system wysiwyg

我想在行之间添加一个新行。目前,只能在最后一行之后添加新行。 参考:http://getcontenttools.com/

当前的JS代码

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); 

2 个答案:

答案 0 :(得分:0)

以下代码段为我工作。

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();
        }
      }
    };

答案 1 :(得分:0)

使用shift +输入当前行上方的新行:

TableCellText.prototype._keyReturn = function(ev) {
  ev.preventDefault();
  if (ev.shiftKey) {
    var cell, child, grandParent, newCell, newCellText, row, section, _i, _len, _ref;
    cell = this.parent();
    if (!this.can('spawn')) {
      return;
    }
    grandParent = cell.parent().parent();
    if (grandParent.tagName() === 'tbody') {
      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();
    }
  } else
    return this._keyTab({
      'shiftKey': false,
      'preventDefault': function() {}
    });
};