How to make HTML table “Excel-like” editable for multiple cells, i.e. simultaneous copy-paste features?

后端 未结 2 1578
死守一世寂寞
死守一世寂寞 2021-02-03 11:14

I need my HTML table to be editable so that user inserted data could be sent to the server. However due to the table\'s size (50 rows) it would not be convenient for users to in

相关标签:
2条回答
  • 2021-02-03 11:47

    You can add a listener to the input event of each cell, and if the cell contains multiple data items split them across the next cells.

    function init()
    {
        var tables = document.getElementsByClassName("editabletable");
        var i;
        for (i = 0; i < tables.length; i++)
        {
            makeTableEditable(tables[i]);
        }
    }
    
    function makeTableEditable(table)
    {
        var rows = table.rows;
        var r;
        for (r = 0; r < rows.length; r++)
        {
            var cols = rows[r].cells;
            var c;
            for (c = 0; c < cols.length; c++)
            {
                var cell = cols[c];
                var listener = makeEditListener(table, r, c);
                cell.addEventListener("input", listener, false);
            }
        }
    }
    
    function makeEditListener(table, row, col)
    {
        return function(event)
        {
            var cell = getCellElement(table, row, col);
            var text = cell.innerHTML.replace(/<br>$/, '');
            var items = split(text);
    
            if (items.length === 1)
            {
                // Text is a single element, so do nothing.
                // Without this each keypress resets the focus.
                return;
            }
    
            var i;
            var r = row;
            var c = col;
            for (i = 0; i < items.length && r < table.rows.length; i++)
            {
                cell = getCellElement(table, r, c);
                cell.innerHTML = items[i]; // doesn't escape HTML
    
                c++;
                if (c === table.rows[r].cells.length)
                {
                    r++;
                    c = 0;
                }
            }
            cell.focus();
        };
    }
    
    function getCellElement(table, row, col)
    {
        // assume each cell contains a div with the text
        return table.rows[row].cells[col].firstChild;
    }
    
    function split(str)
    {
        // use comma and whitespace as delimiters
        return str.split(/,|\s|<br>/);
    }
    
    window.onload = init;
    

    Demo: http://jsfiddle.net/yRnkF/

    0 讨论(0)
  • 2021-02-03 11:58

    you can achieve this easily by adding handsontable

    change any div to a excel table

    var $container = $("#divid");
    $container.handsontable({
    data: getData(),
    rowHeaders: true,
    colHeaders: true,
    contextMenu: true  //forces dom to use custom right click functionality like add row delete row add column etc 
    });
    

    jsFiddle : http://jsfiddle.net/jwtskyLa/

    0 讨论(0)
提交回复
热议问题