从 YUI DataTable 导出数据

发布于 2024-08-25 06:53:43 字数 196 浏览 5 评论 0原文

从 YUI DataTable 中获取数据并将其转换为单个 CSV 或 TSV 字符串的最简单/最快的方法是什么?我基本上只是想实现一种一键式方法,将整个 DataTable(它应该保留当前应用的排序)转换为用户可以粘贴到电子表格中的表单。

我的 DataTable 可能会变得相当大 - 5000 到 10000 行、5 到 10 列 - 因此效率确实很重要。

What's the easiest/fastest way to grab data out of a YUI DataTable and turn it into a single CSV or TSV string? I basically just want to implement a one-click way to get the entire DataTable (it should preserve the currently-applied sorting) into a form that users can paste into a spreadsheet.

My DataTable can get pretty big - 5000 to 10000 rows, 5 to 10 columns - so efficiency does matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-09-01 06:53:43

像这样怎么样:

function dataTableAsCSV (myDataTable) {

    var i, j, oData, newWin = window.open(),
        aRecs = myDataTable.getRecordSet().getRecords(),
        aCols = myDataTable.getColumnSet().keys;

    newWin.document.write("<pre>");

    for (i=0; i<aRecs.length; i++) {
        oData = aRecs[i].getData();

        for (j=0; j<aCols.length; j++) {
            newWin.document.write( oData[aCols[j].key] + "\t");

        }
        newWin.document.write("\n");

    }

    newWin.document.write("</pre>n");
    newWin.document.close();
}

它将数据表内容作为 TSV 呈现到新窗口中。它不处理其中包含选项卡的数据,但这只是 oData[aCols[j].key] 上的一些额外替换。

How about something like this:

function dataTableAsCSV (myDataTable) {

    var i, j, oData, newWin = window.open(),
        aRecs = myDataTable.getRecordSet().getRecords(),
        aCols = myDataTable.getColumnSet().keys;

    newWin.document.write("<pre>");

    for (i=0; i<aRecs.length; i++) {
        oData = aRecs[i].getData();

        for (j=0; j<aCols.length; j++) {
            newWin.document.write( oData[aCols[j].key] + "\t");

        }
        newWin.document.write("\n");

    }

    newWin.document.write("</pre>n");
    newWin.document.close();
}

It will render the data table content as TSV into a new window. It doesn't handle data with tabs in it, but that would just be some extra substitutions on oData[aCols[j].key].

情话已封尘 2024-09-01 06:53:43

上面的答案适用于 YUI 3.4 版本之前的版本。然而,从 3.5 版本开始,数据表被重构。我的转换器将单元格值括在双引号中,转义单元格值中的双引号,并处理一级列嵌套(如果存在)。

这是一个演示我的转换器的小提琴: http://jsfiddle.net/geocolumbus/AFB3h/3/< /a>

// Function to convert a DataTable with zero or one nested columns to CSV
function convertToCSV(myDataTable) {
    var col,
    colIndex = 0,
        colKey,
        rowString,
        ret,
        cell,
        headerString = "";

    while (col = myDataTable.getColumn(colIndex++)) {
        if (col.children == null) {
            headerString += '"' + col.key + '",';
        } else {
            Y.Array.each(col.children, function (child) {
                headerString += '"' + child.key + '",';
            });
        }
    }
    ret = headerString.replace(/,$/, '\n');

    Y.Array.each(myDataTable.data.toJSON(), function (item) {
        colIndex = 0;
        rowString = "";
        while (col = myDataTable.getColumn(colIndex++)) {
            if (col.children == null) {
                cell = item[col.key].replace(/"/g, "\\\"");
                rowString += '"' + cell + '",';
            } else {
                Y.Array.each(col.children, function (child) {
                    cell = item[child.key].replace(/"/g, "\\\"");
                    rowString += '"' + cell + '",';
                });
            }
        }
        ret += rowString.replace(/,$/, '') + "\n";
    });

    return ret;
}

The above answer works great for YUI up to version 3.4. However, the data-table was refactored beginning with version 3.5. My converter encloses cell values in double quotes, escapes double quotes in cell values and handles one level of column nesting, if it exists.

Here is a fiddle that demonstrates my converter: http://jsfiddle.net/geocolumbus/AFB3h/3/

// Function to convert a DataTable with zero or one nested columns to CSV
function convertToCSV(myDataTable) {
    var col,
    colIndex = 0,
        colKey,
        rowString,
        ret,
        cell,
        headerString = "";

    while (col = myDataTable.getColumn(colIndex++)) {
        if (col.children == null) {
            headerString += '"' + col.key + '",';
        } else {
            Y.Array.each(col.children, function (child) {
                headerString += '"' + child.key + '",';
            });
        }
    }
    ret = headerString.replace(/,$/, '\n');

    Y.Array.each(myDataTable.data.toJSON(), function (item) {
        colIndex = 0;
        rowString = "";
        while (col = myDataTable.getColumn(colIndex++)) {
            if (col.children == null) {
                cell = item[col.key].replace(/"/g, "\\\"");
                rowString += '"' + cell + '",';
            } else {
                Y.Array.each(col.children, function (child) {
                    cell = item[child.key].replace(/"/g, "\\\"");
                    rowString += '"' + cell + '",';
                });
            }
        }
        ret += rowString.replace(/,$/, '') + "\n";
    });

    return ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文