Handsontable序列单词创建

时间:2016-01-14 09:42:49

标签: javascript sequence spreadsheet drag handsontable

感谢这个精彩的插件。

我正在使用此插件进行网格功能。为此,我试图在下拉时使用序列字创建。

我已经实现了我的知识。但是在选择并拖动多列时我遇到了一个问题。

我为此示例创建了jsfiddle

var myData = [
    ["WIRE-001", 10, 11, 12, 13],
    ["WIRE-002", 20, 11, 14, 13],
    ["WIRE-003", 30, 15, 12, 13]
];

$("#exampleGrid").handsontable({
    data: myData,
    startRows: 5,
    startCols: 5,
    //minSpareCols: 1, //always keep at least 1 spare row at the right
    minSpareRows: 10, //always keep at least 1 spare row at the bottom,
    rowHeaders: true,
    colHeaders: true,
    contextMenu: true,
    currentRowClassName: 'currentRow',
    currentColClassName: 'currentCol',
    outsideClickDeselects: false,
    fillHandle: true,
    beforeAutofill: function(start, end, data) {
        console.log(arguments);
        console.log(start);
        console.log(end);
        console.log(data);
        var selectedVal = this.getSelected();
        var selectedData = this.getData(selectedVal[0], selectedVal[1], selectedVal[2], selectedVal[3]);
        var sequenceNum = [];
        var sequenceWord = [];
        var numberFormat = 1;
        if (start.col == 0) {
            for (var j = 0; j < selectedData.length; j++) {
                var numbers = selectedData[j][0].match(/[0-9]+$/g);
                if (numbers && !isNaN(numbers[0])) {
                    numberFormat = numbers[0].length;
                    sequenceNum.push(Number(numbers[0]));
                }
                var words = selectedData[j][0].match(/[A-Za-z\-]+/g);
                if (words && isNaN(words[0])) {
                    sequenceWord.push(words[0]);
                }
            }
            var prefix = sequenceWord.length > 0 ? sequenceWord[0] : "";
            var lastValue = sequenceNum[sequenceNum.length - 1]
            var diff = sequenceNum.length > 1 ? (sequenceNum[sequenceNum.length - 1] - sequenceNum[sequenceNum.length - 2]) : 1;
            for (var i = 0; i < end.row; i++) {
                if (!data[i]) { data[i] = []; }
                data[i][0] = prefix + pad((lastValue + diff), numberFormat);
                diff++;
            }
        }
    },
    afterChange: function(changes, source) {

    }
});

感谢您帮助解决这个问题。

1 个答案:

答案 0 :(得分:0)

我建议打开控制台并检查错误,因为如果您这样做,问题就变得非常清楚了。导致您在评论中提到的错误的示例使用空行。在您正在执行的代码中

var words = selectedData[j][0].match(/[A-Za-z\-]+/g);

但是,selectedData[j][0]将是null。我只是添加一个空行检查并适当处理它(抛出它或默认为某个值)