-1

Thanks for this wonderful plugin.

I am using this plugin for grid functionality. For this I am trying to use sequence word creation on drag down.

I have implemented upto my knowledge. But I am facing one problem while select and drag multiple column.

I have created jsfiddle for this sample

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

    }
});

Appreciate for helping to solve this.

1 Answers1

0

I recommend opening the console and checking the errors since it becomes very clear what your problem is if you do so. The example that leads to the bug that you mentioned on your comment uses an empty row. In your code you are doing

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

However, selectedData[j][0] will be null. I would simply add a check for empty rows and handle it appropriately (toss it or default to some value)

ZekeDroid
  • 7,089
  • 5
  • 33
  • 59
  • Hi thanks for your reply. Sequence word creation is creating properly. My problem is when i select multiple column and row and drag, the column A is properly generating because I am generating to that particular column only. the other columns are not autofill properly. – Kirubaharan Jan 23 '16 at 02:57