【问题标题】:ContentTools - How do I add new row in table?ContentTools - 如何在表格中添加新行?
【发布时间】:2017-12-29 03:04:18
【问题描述】:

我想在行之间添加一个新行。目前只能在最后一行之后添加新行。 参考: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); 

【问题讨论】:

  • 你能告诉我们你到目前为止做了什么吗? :)
  • row = new ContentEdit.TableRow(); _ref = cell.parent().children;对于 (_i = 0, _len = _ref.length; _i
  • section.attach(row) - 在行中添加新行。我想在所选行之后添加新行。

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


【解决方案1】:

下面的代码 sn-p 为我工作。

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

【讨论】:

    【解决方案2】:

    用 shift + enter 在当前行上方新建一行:

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

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 2022-01-10
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多