ce icon indicating copy to clipboard operation
ce copied to clipboard

how to set maxlength to table cell, user should not enter more than given length in cell.

Open kallaguntaashok opened this issue 6 years ago • 5 comments

Hi,

could you please tell me, how do I set the max length to a table cell, so that the user should not enter more than a given length.

kallaguntaashok avatar Aug 18 '20 18:08 kallaguntaashok

You can use a custom column and create an input with the maxlenght attribute

 function InputMaxLenght(maxLength) {
        // Methods
        this.closeEditor = function (cell, save) {
            var value = cell.children[0].value;
            cell.innerHTML = value;
            return value;
        };
        this.openEditor = function (cell) {
            // Create input
            var element = document.createElement('input');
            element.maxLength = maxLength;
            // // Update cell
            cell.classList.add('editor');
            cell.innerHTML = '';
            cell.appendChild(element);
            // // Focus on the element
            element.focus();
        };
        this.getValue = function (cell) {
            return cell.innerHTML;
        };
        this.setValue = function (cell, value) {
            console.log(value);
            cell.innerHTML = value;
        };
    }

    jexcel(document.getElementById('spreadsheet'), {
        data: data,
        columns: [
            { type: 'text', title:'MaxLenght', width:100, editor: new InputMaxLenght(4) },
        ]
    });

JSFiddle

alfr3dosv avatar Aug 19 '20 00:08 alfr3dosv

Hi, custom login is working fine for max length, but the drag and drop option is not working after applying the above script.

drag and drop mean if I want to move information from one row to another that logic is not working in the column which has max length function.

it's will be a greater help if you can cross-check this issue.

kallaguntaashok avatar Aug 19 '20 11:08 kallaguntaashok

I think this will solve the problem

    function InputMaxLenght(maxLength) {
        // Methods
        this.closeEditor = function (cell, save) {
            var value = cell.children[0].value;
            cell.innerHTML = value;
            return value;
        },
        this.openEditor = function (cell) {
            console.log('cell', 1)
            // Create input
            var element = document.createElement('input');
            element.maxLength = maxLength;
            element.value = cell.innerHTML;
            // // Update cell
            cell.classList.add('editor');
            cell.innerHTML = '';
            cell.appendChild(element);
            // // Focus on the element
            element.focus();
        },
        this.getValue = function (cell) {
            console.log('obteniendo valor');
            return cell.innerHTML;
        }
        this.setValue = function (cell, value) {
            console.log('estableciendo valor', {cell, value });
            cell.innerHTML = value;
        }
        this.updateCell = function (cell, value) {
            let instance = cell.parentNode.parentNode.parentNode.parentNode.parentNode.jexcel;           
            let y = cell.dataset.y;
            let x = cell.dataset.x
            cell.innerHTML = value;
            instance.options.data[y][x] = value;
            return value;
        }
    }

alfr3dosv avatar Aug 19 '20 15:08 alfr3dosv

you're my savior bro, I was struggling to solve this problem, how do you know all these things, I could not able to fin this peace of update cell code anywhere.

thank you so much again for your help and support, I would like to connect with you in further if I need any help in this jexcel, you have very good understanding about this frame work.

kallaguntaashok avatar Aug 19 '20 19:08 kallaguntaashok

I have added a new event that will make similar process easier. You will be able to access and change the behavior of the editors without creating a new one.

<html>
<script src="https://bossanova.uk/jexcel/v4/jexcel.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jexcel/v4/jexcel.css" type="text/css" />

<script src="https://bossanova.uk/jsuites/v3/jsuites.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v3/jsuites.css" type="text/css" />

<div id="spreadsheet"></div>

<script>
var data = [
    ['Jazz', 'Honda', '2019-02-12', '', true, '$ 2.000,00', '#777700'],
    ['Civic', 'Honda', '2018-07-11', '', true, '$ 4.000,01', '#007777'],
];
jexcel(document.getElementById('spreadsheet'), {
        data: data,
        columns: [
            { type: 'text', title:'MaxLenght', width:100, maxlength:10 },
        ],
        oncreateeditor: function(el, cell, x, y) {
             if (x == 0) {
               var config = el.jexcel.options.columns[x].maxlength;
               cell.children[0].setAttribute('maxlength' , config);
             }
        }
    });
</script>

pphod avatar Aug 19 '20 20:08 pphod