vue-easytable

repository·master·Indexed 26 days ago

https://github.com/happy-coding-clans/vue-easytable

A feature-rich data table component for Vue 2.x designed to handle large datasets of up to 300,000 rows using virtual scrolling. It provides spreadsheet-like capabilities including cell editing, autofill, column resizing, filtering, sorting, and keyboard-based cell selection. The library includes base components for loading, pagination, and context menus, and supports internationalization and theme customization via CSS variables.

Tokens
165.1K
Snippets
190
Records
381
Agent score
86%

What's inside vue-easytable

  1. Overview of vue-easytable features

    master

    vue-easytable

    vue-easytable is a collection of flexible table components based on Vue 2.x. It is designed to handle large datasets and provides a wide range of built-in table functionalities.

    Key Capabilities

    • Large Data Handling: Supports up to 300,000 rows of data using virtual scroll.
    • Base Components: Includes Loading, Pagination, Contextmenu, Icon, and Locale components.
    • Table Features:
      • Layout: Column/Header fixing, Header grouping, Column resize, Cell Span, and Row Expand.
      • Data Interaction: Filter, Sort, Cell Edit, Cell Autofill, and Cell Selection (keyboard operation).
      • Styling & Display: Internationalization, Theme Customization, Cell Style, Cell Ellipsis, and Footer Summary.
      • Selection: Row Radio, Row Checkbox, and Clipboard support.
  2. vue-easytable features overview

    master

    vue-easytable is a powerful data table for Vue 2.x that supports large datasets (up to 300,000 rows) via virtual scroll and provides Excel-like functionality.

    Key Table Features:

    • Virtual Scroll
    • Column Fixed, Hidden, and Resize
    • Header Fixed and Grouping
    • Filter and Sort
    • Cell Edit, Autofill, and Span
    • Cell Selection (keyboard), Style, and Customization
    • Row Radio, Checkbox, Expand, and Style
    • Footer Summary
    • Clipboard and Contextmenu support
    • Internationalization and Theme Customization

    Base Components:

    • Loading, Pagination, Contextmenu, Icon, and Locale components.
  3. Use automatic table width in ve-table

    master

    If you do not explicitly set a width for the ve-table component, it defaults to style="width:100%;". This allows the table to automatically expand to fill its container.

    <template>
        <ve-table :columns="columns" :table-data="tableData" />
    </template>
    
    <script>
        export default {
            data() {
                return {
                    columns: [
                        { field: "name", key: "a", title: "Name", width: 100 },
                        { field: "date", key: "b", title: "Tel", width: 200 },
                        { field: "hobby", key: "c", title: "Hobby", width: 300 },
                        { field: "address", key: "d", title: "Address", width: 300 },
                    ],
                    tableData: [
                        {
                            name: "John",
                            date: "1900-05-20",
                            hobby: "coding and coding repeat",
                            address: "No.1 Century Avenue, Shanghai",
                        },
                        {
                            name: "Dickerson",
                            date: "1910-06-20",
                            hobby: "coding and coding repeat",
                            address: "No.1 Century Avenue, Beijing",
                        },
                        {
                            name: "Larsen",
                            date: "2000-07-20",
                            hobby: "coding and coding repeat",
                            address: "No.1 Century Avenue, Chongqing",
                        },
                        {
                            name: "Geneva",
                            date: "2010-08-20",
                            hobby: "coding and coding repeat",
                            address: "No.1 Century Avenue, Xiamen",
                        },
                        {
                            name: "Jami",
                            date: "2020-09-20",
                            hobby: "coding and coding repeat",
                            address: "No.1 Century Avenue, Shenzhen",
                        },
                    ],
                };
            },
        };
    </script>
  4. Configure column filtering in ve-table

    master

    You can implement filtering for specific columns in ve-table by using the filter property within a column object.

    Key configuration options include:

    • filter: Sets the filtering function for the column.
    • Filterlist: An array of objects defining the filter criteria. Each object must contain label, value, and selected attributes.
    • isMultiple: A boolean that enables multiple selection of filter items (defaults to false).
    • filterConfirm: A callback function triggered when the filter is confirmed.
    • filterReset: A callback function triggered when the filter is reset.
  5. Calculate real row indices with virtual scrolling

    master

    When using virtual-scroll-option in ve-table, the rowIndex provided by the renderBodyCell method refers to the index within the currently rendered chunk, not the absolute index in the entire dataset. To display the correct, continuous row number (especially when filtering or using server-side data), you must combine the rowIndex from renderBodyCell with the startRowIndex captured from the scrolling event.

    Formula for real sequence number: rowIndex + startRowIndex + 1

    Steps to implement:

    1. Enable virtual scrolling in :virtual-scroll-option.
    2. Define a scrolling callback to capture the current startRowIndex.
    3. Use renderBodyCell in your column definition to calculate and return the real index.
    <template>
        <div>
            <el-input style="width:250px" v-model="searchValue" placeholder="search name"></el-input>
            <el-button type="primary" @click="search">Search</el-button>
            <br />
            <br />
            <ve-table
                :max-height="500"
                :virtual-scroll-option="virtualScrollOption"
                :columns="columns"
                :table-data="tableData"
                row-key-field-name="rowKey"
            />
        </div
    </template>
    
    <script>
        import Mock from "mockjs";
        export default {
            data() {
                return {
                    searchValue: "",
                    startRowIndex: 0,
                    virtualScrollOption: {
                        enable: true,
                        scrolling: this.scrolling,
                    },
                    columns: [
                        {
                            field: "index",
                            key: "index",
                            title: "#",
                            width: 200,
                            align: "left",
                            renderBodyCell: this.renderRowIndex,
                        },
                        // ... other columns
                    ],
                    tableData: [],
                    sourceData: [],
                };
            },
            methods: {
                // Capture the start index of the currently rendered block
                scrolling({
                    startRowIndex,
                    visibleStartIndex,
                    visibleEndIndex,
                    visibleAboveCount,
                    visibleBelowCount,
                }) {
                    this.startRowIndex = startRowIndex;
                },
                // Calculate the real index using the captured startRowIndex
                renderRowIndex({ row, column, rowIndex }) {
                    return (
                        <span class="text-bold" style="color:#1890ff;">
                            {rowIndex + this.startRowIndex + 1}
                        </span>
                    );
                },
                // ... other methods
            },
            created() {
                this.initData();
            },
        };
    </script>
  6. Enable Cell Selection in ve-table

    master

    You can enable cell selection in ve-table to allow users to select multiple rows, multiple columns, or select all, similar to Excel functionality. This feature is particularly useful when combined with cell editing.

    Requirements and Constraints:

    • rowKeyFieldName: You must specify the rowKeyFieldName property for selection to work correctly.
    • Text Selection: When cell selection is enabled, standard text selection is disabled via user-select:none to prevent conflicts with the selection mechanism.
  7. Customize cell styles in ve-table

    master

    You can customize the appearance of cells in ve-table using several methods:

    1. Global Configuration: Use the cellStyleOption object to set styles for cells.
    2. Class Callbacks: Use callback functions to dynamically apply CSS classes to specific cells based on their context:
      • bodyCellClass({ row, column, rowIndex }): Sets the class for table body cells.
      • headerCellClass({ column, rowIndex }): Sets the class for table header cells.
      • footerCellClass({ row, column, rowIndex }): Sets the class for table footer cells.
    3. Custom Renderers: Use renderBodyCell, renderHeaderCell, or renderFooterCell for full control over cell content and styling.

    Important Note: When using <style> tags to target these cells, do not use the scoped attribute, as it may prevent styles from applying to the dynamically generated table elements.

  8. Configure default expanded rows in ve-table

    master

    You can control which rows are expanded by default in a ve-table using the expand-option prop.

    • Use defaultExpandedRowKeys (an array of keys) to specify specific rows that should be expanded on load.
    • Use defaultExpandAllRows: true to expand all rows by default.

    Note: Ensure you have set the row-key-field-name prop on the ve-table component to match the unique identifier field in your data (e.g., rowKey). You must also include a column with type: 'expand' in your columns array to display the expansion icons.

    <template>
        <ve-table
            style="width:100%"
            :columns="columns"
            :table-data="tableData"
            :expand-option="expandOption"
            row-key-field-name="rowKey"
        />
    </template>
    
    <script>
        export default {
            data() {
                return {
                    expandOption: {
                        defaultExpandAllRows: false,
                        // Specify keys of rows to be expanded by default
                        defaultExpandedRowKeys: [1001, 1003],
                        // Function to determine if a row is expandable
                        expandable: ({ row, column, rowIndex }) => {
                            if (row["rowKey"] === 1002) {
                                return false;
                            }
                        },
                        // Render function for the expanded content
                        render: ({ row, column, rowIndex }, h) => {
                            return (
                                <p>
                                    My name is <span style="color:#1890ff;">{row.name}</span>
                                    ,I'm living in {row.address}
                                </p>
                            );
                        },
                    },
                    columns: [
                        {
                            field: "",
                            key: "a",
                            // Column type must be 'expand' to show the icon
                            type: "expand",
                            title: "",
                            width: 50,
                            align: "center",
                        },
                        // ... other columns
                    ],
                    tableData: [
                        { rowKey: 1001, name: "John", address: "No.1 Century Avenue, Shanghai" },
                        { rowKey: 1002, name: "Dickerson", address: "No.1 Century Avenue, Beijing" },
                        // ... other data
                    ],
                };
            },
        };
    </script>