Skip to content

Instantly share code, notes, and snippets.

@xgqfrms
Created May 6, 2018 11:19
Show Gist options
  • Save xgqfrms/6a6d8c77f05e23fdbe0a87dde97693b8 to your computer and use it in GitHub Desktop.
Save xgqfrms/6a6d8c77f05e23fdbe0a87dde97693b8 to your computer and use it in GitHub Desktop.
multi table thead cols

multi table thead cols

bug

image

const cols = [
    {
        "key": "name",
        "title": "Name",
        "colspan": 1,
        "rowspan": 2,
        "children": null,
    },
    {
        "key": "age",
        "title": "Age",
        "colspan": 1,
        "rowspan": 2,
        "children": null,
    },
    {
        "key": "addreess",
        "title": "Addreess",
        "colspan": 2,
        "rowspan": 1,
        "children": [
            {
                "key": "country",
                "title": "Country",
                "colspan": 1,
                "rowspan": 1,
                "children": null,
            },
            {
                "key": "city",
                "title": "City",
                "colspan": 1,
                "rowspan": 1,
                "children": null,
            },
        ],
    },
];

const datas = [
    {
        "key": "001",
        "name": "xgqfrms",
        "age": 23,
        "country": "China",
        "city": "shanghai"
    },
    {
        "key": "002",
        "name": "Jackson",
        "age": 17,
        "country": "USA",
        "city": "LA"
    },
    {
        "key": "003",
        "name": "king",
        "age": 27,
        "country": "Korea",
        "city": "Seoul"
    },
];


const recursiveTrs = (cols = []) => {
    let trs = ``,
        newTr = ``,
        ths = ``;
    const delayTest = (children = []) => {
        newTr += recursiveTrs(children);
    };
    cols.forEach(
        (col, i) => {
            let {
                key,
                title,
                children,
                colspan,
                rowspan
            } = {...col};
            ths += `
                <th colspan="${colspan}" rowspan="${rowspan}" data-key="key">
                    ${title}
                </th>
            `;
            console.log(`${i} ths =`, ths);
            if(children !== null) {
                // console.log(`${i} new trs =`, trs);
                // trs += recursiveTrs(children);
                delayTest(children);
            }
        }
    );
    trs += `
        <tr>
            ${ths}
        </tr>
    `;
    trs += newTr;
    console.log(`return trs =`, trs);
    return trs;
};



const cols = [
    {
        "key": "name",
        "title": "Name",
        "colspan": 1,
        "rowspan": 3,
        "children": null,
    },
    {
        "key": "addreess",
        "title": "Addreess",
        "colspan": 4,
        "rowspan": 1,
        "children": [
            {
                "key": "country",
                "title": "Country",
                "colspan": 2,
                "rowspan": 1,
                "children": [
                    {
                        "key": "language",
                        "title": "Language",
                        "colspan": 1,
                        "rowspan": 1,
                        "children": null,
                    },
                    {
                        "key": "population",
                        "title": "Population",
                        "colspan": 1,
                        "rowspan": 1,
                        "children": null,
                    },
                ],
            },
            {
                "key": "city",
                "title": "City",
                "colspan": 2,
                "rowspan": 1,
                "children": [
                    {
                        "key": "code",
                        "title": "Code",
                        "colspan": 1,
                        "rowspan": 1,
                        "children": null,
                    },
                    {
                        "key": "rank",
                        "title": "Rank",
                        "colspan": 1,
                        "rowspan": 1,
                        "children": null,
                    },
                ],
            },
        ],
    },
    {
        "key": "age",
        "title": "Age",
        "colspan": 1,
        "rowspan": 3,
        "children": null,
    }
];
@xgqfrms
Copy link
Author

xgqfrms commented Feb 21, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment