Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <br>
  <div class="btn-group float-right mt-2" role="group">
    <a href="javascript:void(0)" class="btn btn-sm btn-primary active">Basic example</a>
    <a href="https://codepen.io/opznhaarlems/full/EEZpOL" target="_blank" class="btn btn-sm btn-primary">Large table example</a>
  </div>
  
  <h2 class="mb-4">Table selection</h2>

  <table class="table table-striped table-selection">
    <thead>
      <tr>
        <th>ID</th>
        <th>First name</th>
        <th>Last name</th>
        <th>Email</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>John</td>
        <td>Doe</td>
        <td><a href="mailto:john.doe@mail.com">john.doe@mail.com</a></td>
        <td>2018-01-01</td>
      </tr>
      <tr>
        <td>2</td>
        <td>Jane</td>
        <td>Doe</td>
        <td><a href="mailto:jane.doe@mail.com">jane.doe@mail.com</a></td>
        <td>2018-01-02</td>
      </tr>
      <tr>
        <td>3</td>
        <td>John</td>
        <td>Appleseed</td>
        <td><a href="mailto:j.appleseed@apple.com">j.appleseed@apple.com</a></td>
        <td>2018-01-03</td>
      </tr>
      <tr>
        <td>4</td>
        <td>Joe</td>
        <td>Bloggs</td>
        <td><a href="mailto:joe.bloggs@gmail.com">joe.bloggs@gmail.com</a></td>
        <td>2018-01-04</td>
      </tr>
      <tr>
        <td>5</td>
        <td>Mister</td>
        <td>Smith</td>
        <td><a href="mailto:m.smith@thematrix.com">m.smith@thematrix.com</a></td>
        <td>2018-01-05</td>
      </tr>
    </tbody>
  </table>
</div>

<div class="container">
  <div class="card">
    <div class="card-body">
      <ul>
        <li>Just add <code>.table-selection</code> class to a table and include the code from the Javascript and CSS panels in your project.</li>
        <li>Supports horizontal, vertical and rectangular selections.</li>
        <li>Includes clipboard support with table formatting for Excel or other spreadsheet software!</li>
        <li>It even works on mobile!</li>
        <li>Tested and working in Chrome, Firefox, Edge, Mobile Chrome and Samsung Browser</li>
      </ul>
    </div>
  </div>
  <br>
  
  <!-- External links -->
  <p class="text-center">
    <a href="https://www.npmjs.com/package/@pxlwidgets/table-selection" target="_blank" class="btn text-danger" title="NPM package...">
      <span class="fab fa-2x fa-fw fa-npm"></span>
    </a>
    <a href="https://github.com/PXLWidgets/table-selection" target="_blank" class="btn text-dark" title="GitHub repository...">
      <span class="fab fa-2x fa-fw fa-github"></span>
    </a>
    <a href="https://packagist.org/packages/widgets-nl/table-selection" target="_blank" class="btn text-warning" title="Packagist package...">
      <span class="fa fa-2x fa-fw fa-cubes"></span>
    </a>
</div>
<br>
              
            
!

CSS

              
                .table-selection *::selection {
  background: transparent;
  color: inherit;
}

.table-selection *::-moz-selection {
  background: transparent;
  color: inherit;
}

.table-selection td.selected {
  background: rgba(0, 123, 255, .25);
}
              
            
!

JS

              
                /*!
 * TableSelection library v0.9.1 (https://github.com/WouterWidgets/table-selection)
 * Copyright (c) 2018 Wouter Smit
 * Licensed under MIT (https://github.com/WouterWidgets/table-selection/blob/master/LICENSE)
*/

class TableSelection {

    constructor(selector = '.table-selection', selectedClass = 'selected') {
        this.selector = selector;
        this.selectedClass = selectedClass;

        this.selection = null;
        this.nativeSelection = null;

        this.setEventHandlers();
    }

    static initialize(selector = '.table-selection', selectedClass = 'selected') {
        new TableSelection(selector, selectedClass);
    }

    setEventHandlers() {
        document.addEventListener('selectionchange', this.selectionChangeHandler.bind(this));
        document.addEventListener('copy', this.copyHandler.bind(this));
    }

    selectionChangeHandler() {
        this.deselect();
        this.nativeSelection = window.getSelection ? getSelection() : null;

        if (!this.nativeSelection) {
            return;
        }

        this.getSelection();
        this.showSelection();
    }

    getSelection() {
        const tds = this.getSelectionTds();

        if (!tds || !tds.start.closest(this.selector)) {
            return;
        }

        const trs = this.getSelectionTrs(tds);

        this.selection = {
            tds: tds,
            trs: trs,
        };

        this.selection.cells = this.getCellsInSelectionRange(this.selection);

        return this.selection;
    }

    getCellsInSelectionRange(selection) {

        const tbody = selection.trs.start.parentElement;
        const hasThead = tbody.previousElementSibling && tbody.previousElementSibling.matches('thead');

        const trStartIndex = selection.trs.start.rowIndex - (hasThead ? 1 : 0);
        const trEndIndex = selection.trs.end.rowIndex - (hasThead ? 1 : 0);

        const tdStartIndex = selection.tds.start.cellIndex;
        const tdEndIndex = selection.tds.end.cellIndex;

        const trs = Array
            .from(tbody.rows)
            .slice(trStartIndex, trEndIndex + 1)
        ;

        let cells = [];
        trs.forEach(tr => {
            const tds = Array
                .from(tr.cells)
                .slice(tdStartIndex, tdEndIndex + 1);

            cells = cells.concat(tds);
        });

        return cells;
    }

    getSelectionTds() {
        let start = this.nativeSelection.anchorNode;
        let end = this.nativeSelection.focusNode;

        if (!start || !end) {
            return;
        }

        if (start.nodeType !== 1) {
            start = start.parentElement;
        }

        if (end.nodeType !== 1) {
            end = end.parentElement;
        }

        start = start.closest('td');
        end = end.closest('td');

        if (!start || !end) {
            return;
        }

        if (start.cellIndex > end.cellIndex) {
            [end, start] = [start, end];
        }

        return {start, end}
    }

    getSelectionTrs(tds) {
        if (!tds.start || !tds.end) {
            return;
        }

        let start = tds.start.closest('tr');
        let end = tds.end.closest('tr');

        if (start.rowIndex > end.rowIndex) {
            [end, start] = [start, end];
        }

        return {start, end}
    }

    showSelection() {
        this.selection && this.selection.cells.forEach(cell => {
            cell.classList.add(this.selectedClass);
        });
    }

    hideSelection() {
        this.selection && this.selection.cells.forEach(cell => {
            cell.classList.remove(this.selectedClass);
        });
    }

    deselect() {
        if (!this.selection) {
            return;
        }

        this.hideSelection();
        this.selection = null;
        this.nativeSelection = null;
    }

    getSelectionText() {
        let rowData = {},
            data = [];

        this.selection.cells.forEach(cell => {
            const rowIndex = cell.parentElement.rowIndex;
            rowData[rowIndex] = rowData[rowIndex] || [];
            rowData[rowIndex].push(cell.innerText);
        });

        for (const i in rowData) {
            if (!rowData.hasOwnProperty(i)) {
                continue;
            }
            data.push(rowData[i].join("\t"));
        }

        return data.join("\n");
    }

    copyHandler(e) {
        if (!this.selection) {
            return;
        }
        e.clipboardData.setData('text/plain', this.getSelectionText());
        e.preventDefault();
    }

}

TableSelection.initialize();

              
            
!
999px

Console