handsontable_cells.js 8.68 KB
Newer Older
Felipe Escala Torres committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
/* ------------------------------------------------------------------------------
*
*  # Handsontable - Excel-like tables with extensive funtionality
*
*  Specific JS code additions for handsontable_cells.html page
*
*  Version: 1.0
*  Latest update: Nov 1, 2015
*
* ---------------------------------------------------------------------------- */

$(function() {


    // Data validation
    // ------------------------------

    // Add sample data
    var people = [
        {id: 1, name: {first: 'Joe', last: 'Fabiano'}, ip: '0.0.0.1', email: 'Joe.Fabiano@ex.com'},
        {id: 2, name: {first: 'Fred', last: 'Wecler'}, ip: '0.0.0.1', email: 'Fred.Wecler@ex.com'},
        {id: 3, name: {first: 'Steve', last: 'Wilson'}, ip: '0.0.0.1', email: 'Steve.Wilson@ex.com'},
        {id: 4, name: {first: 'Maria', last: 'Fernandez'}, ip: '0.0.0.1', email: 'M.Fernandez@ex.com'},
        {id: 5, name: {first: 'Pierre', last: 'Barbault'}, ip: '0.0.0.1', email: 'Pierre.Barbault@ex.com'},
        {id: 6, name: {first: 'Nancy', last: 'Moore'}, ip: '0.0.0.1', email: 'Nancy.Moore@ex.com'},
        {id: 7, name: {first: 'Barbara', last: 'MacDonald'}, ip: '0.0.0.1', email: 'B.MacDonald@ex.com'},
        {id: 8, name: {first: 'Wilma', last: 'Williams'}, ip: '0.0.0.1', email: 'Wilma.Williams@ex.com'},
        {id: 9, name: {first: 'Sasha', last: 'Silver'}, ip: '0.0.0.1', email: 'Sasha.Silver@ex.com'},
        {id: 10, name: {first: 'Don', last: 'Pérignon'}, ip: '0.0.0.1', email: 'Don.Pérignon@ex.com'},
        {id: 11, name: {first: 'Aaron', last: 'Kinley'}, ip: '0.0.0.1', email: 'Aaron.Kinley@ex.com'}
    ];



    // Define element
    var hot_validation = document.getElementById('hot_validation'),

    // Define output element
    hot_validation_console = document.getElementById('hot_validation_console');

    // Email validator
    var ipValidatorRegexp = /^(?:\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|null)$/;
    var emailValidator = function (value, callback) {
        setTimeout(function() {
            if (/.+@.+/.test(value)) {
                callback(true);
            }
            else {
                callback(false);
            }
        }, 1000);
    };

    // Initialize with options
    var hot_validation_init = new Handsontable(hot_validation, {
        data: people,
        rowHeaders: true,
        stretchH: 'all',
        beforeChange: function (changes, source) {
            for (var i = changes.length - 1; i >= 0; i--) {

                // Gently don't accept the word "foo" (remove the change at index i)
                if (changes[i][3] === 'foo') {
                    changes.splice(i, 1);
                }

                // If any of pasted cells contains the word "nuke", reject the whole paste
                else if (changes[i][3] === 'nuke') {
                    return false;
                }

                // Capitalise first letter in column 1 and 2
                else if ((changes[i][1] === 'name.first' || changes[i][1] === 'name.last') && changes[i][3].charAt(0)) {
                    changes[i][3] = changes[i][3].charAt(0).toUpperCase() + changes[i][3].slice(1);
                }
            }
        },
        afterChange: function (changes, source) {
            if (source !== 'loadData') {
                hot_validation_console.innerText = JSON.stringify(changes);
                Prism.highlightElement(hot_validation_console);
            }
        },
        colHeaders: ['ID', 'First name', 'Last name', 'IP', 'E-mail'],
        columns: [
            {data: 'id', type: 'numeric', className: 'htLeft'},
            {data: 'name.first'},
            {data: 'name.last'},
            {data: 'ip', validator: ipValidatorRegexp, allowInvalid: true},
            {data: 'email', validator: emailValidator, allowInvalid: false}
        ]
    });



    // Drag down
    // ------------------------------

    // Add sample data
    var hot_drag_data = [
        ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
        ['2008', 45833, 12293, 12894, 78859, 47729, 43054],
        ['2009', 34234, 48902, 49950, 58823, 57882, 89954],
        ['2010', 85943, 90449, 38882, 34928, 45397, 23487],
        ['2011', 44950, 90092, 89932, 89945, 89003, 58943],
        ['2012', 23486, 83394, 47729, 23945, 99001, 48995],
        ['2013', 90392, 58282, 48852, 17789, 32984, 23498],
        ['2014', 47382, 88457, 90029, 58875, 45398, 48995],
        ['2015', '', '', '', '', '', ''],
        ['2016', '', '', '', '', '', ''],
        ['2017', '', '', '', '', '', '']
    ];

    // Define element
    var hot_drag = document.getElementById('hot_drag');

    // Initialize with options
    var hot_drag_init = new Handsontable(hot_drag, {
        data: hot_drag_data,
        rowHeaders: true,
        colHeaders: true,
        stretchH: 'all',
        fillHandle: true // possible values: true, false, "horizontal", "vertical"
    });



    // Merge cells
    // ------------------------------

    // Generate sample data
    var hot_merge_data = Handsontable.helper.createSpreadsheetData(12, 10);

    // Define element
    var hot_merge = document.getElementById('hot_merge');

    // Initialize with options
    var hot_merge_init = new Handsontable(hot_merge, {
        data: hot_merge_data,
        rowHeaders: true,
        colHeaders: true,
        contextMenu: true,
        stretchH: 'all',
        mergeCells: [
            {row: 1, col: 1, rowspan: 3, colspan: 3},
            {row: 3, col: 4, rowspan: 2, colspan: 2},
            {row: 5, col: 6, rowspan: 3, colspan: 3}
        ]
    });



    // Alignment
    // ------------------------------

    // Generate sample data
    var hot_alignment_data = Handsontable.helper.createSpreadsheetData(12, 10);

    // Define element
    var hot_alignment = document.getElementById('hot_alignment');

    // Initialize with options
    var hot_alignment_init = new Handsontable(hot_alignment, {
        data: hot_alignment_data,
        rowHeaders: true,
        colHeaders: true,
        contextMenu: true,
        stretchH: 'all',
        mergeCells: [
            {row: 1, col: 1, rowspan: 3, colspan: 3},
            {row: 3, col: 4, rowspan: 2, colspan: 2}
        ],
        className: "htCenter",
        cell: [
            {row: 0, col: 0, className: "htRight"},
            {row: 1, col: 1, className: "htLeft htMiddle"},
            {row: 3, col: 4, className: "htLeft htBottom"}
        ]
    });



    // Readonly
    // ------------------------------

    // Add dsample data. Used in readonly and disabled examples
    var car_data = [
        {car: 'Toyota', year: 2008, chassis: 'white', bumper: 'white'},
        {car: 'Chevrolet', year: 2010, chassis: 'grey', bumper: 'black'},
        {car: 'Lexus', year: 2006, chassis: 'red', bumper: 'black'},
        {car: 'Nissan', year: 2013, chassis: 'purple', bumper: 'purple'},
        {car: 'Volvo', year: 2012, chassis: 'red', bumper: 'red'},
        {car: 'Nissan', year: 2011, chassis: 'black', bumper: 'grey'},
        {car: 'Nissan', year: 2012, chassis: 'black', bumper: 'black'},
        {car: 'Nissan', year: 2013, chassis: 'blue', bumper: 'blue'},
        {car: 'Chrysler', year: 2014, chassis: 'yellow', bumper: 'black'},
        {car: 'Volvo', year: 2015, chassis: 'white', bumper: 'grey'}
    ];

    // Define element
    var hot_readonly = document.getElementById('hot_readonly');

    // Initialize with options
    hot_readonly_init = new Handsontable(hot_readonly, {
        data: car_data,
        stretchH: 'all',
        colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color']
    });

    // Update settings on init
    hot_readonly_init.updateSettings({
        cells: function (row, col, prop) {
            var cellProperties = {};

            if (hot_readonly_init.getSourceData()[row][prop] === 'Nissan') {
                cellProperties.readOnly = true;
            }

            return cellProperties;
        }
    });



    // Disable cell editing
    // ------------------------------

    // Define element
    var hot_non_editable = document.getElementById('hot_non_editable');

    // Initialize with options
    hot_non_editable_init = new Handsontable(hot_non_editable, {
        data: car_data,
        stretchH: 'all',
        colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color']
    });

    // Update settings on init
    hot_non_editable_init.updateSettings({
        cells: function (row, col, prop) {
            var cellProperties = {};

            if (hot_non_editable_init.getSourceData()[row][prop] === 'Nissan') {
                cellProperties.editor = false;
                cellProperties.className = "disabled";
            }
            else {
                cellProperties.editor = 'text';
            }

            return cellProperties;
        }
    });

});