如何通过DataTable中的特定列进行迭代?

时间:2018-04-03 03:27:16

标签: javascript json datatable datatables

我有一个类似

的JSON文件
[
    {
        "Title":    "Something",
        "FileName": "2014/mycollege/myvolume.pdf",
        "Course":   "01 - Dangerous Book for Boys",
        "Year":     "2014",
        "Category": "Ethics",
        "Group":    "East Campus"
    },
function getMyEdMaterials(result) {
        $.getJSON("/materials/assets/json/materials.json", function(data) {
            for (var key in data) {
                datatableObject[data[key].Group] = datatableObject[data[key].Group] || [];
                datatableObject[data[key].Group].push(data[key]);

                datatableObject["all"] = datatableObject["all"] || [];
                datatableObject["all"].push(data[key]);
            }

            for (var key in datatableObject) {
                console.log(key.toLowerCase().replace(/\s/g, "-"));
                console.log(datatableObject[key]);

                var tableInstance = $('#' + key.toLowerCase().replace(/\s/g, "-") + '-table').DataTable({
                    bInfo: true,
                    bFilter: true,
                    columns: [{
                            title: "Title",
                            width: "30%",
                            render: function(data, type, row) {
                                return "<a href=\"" + row.FileName + "\" target=\"_blank\">" + row.Title + "</a>";
                            }
                        },
                        {
                            title: "Course",
                            width: "30%",
                            data: "Course"
                        },
                        {
                            title: "Year",
                            width: "10%",
                            //data: "Year"
                            render: function(data, type, row) {
                                //year = year.push(row.Year);
                                return row.Year;
                            }
                        },
                        {
                            title: "Category",
                            width: "5%",
                            data: "Category"
                        },
                        {
                            title: "FileName",
                            width: "5%",
                            data: "FileName"
                        },
                        {
                            title: "Group",
                            width: "5%",
                            data: "Group"
                        }
                    ],
                    columnDefs: [{
                        visible: false,
                        targets: [3, 4, 5]
                    }],
                    data: datatableObject[key],
                    deferRender: true,
                    dom: "<'toolbar'frltip >",
                    initComplete: function() {
                        this.api().columns(2).every(function() {
                            var column = this;
->>> having trouble getting the data from the column <<<---
                            if ((column == undefined) || (column == null)) {
                                console.log("data is null / undefined");
                            } else {
                                column.data().unique().sort().each(function(d, j) {
                                    if ((d !== undefined) || (d !== null)) {
                                        console.log(d + " is the year");
                                    }
                                });
                            }
                        })
                    },
                    lengthMenu: [
                        [25, 50, 100, -1],
                        [25, 50, 100, "All"]
                    ],
                    order: [
                        [2, 'desc'],
                        [3, 'asc'],
                        [1, 'asc'],
                        [0, 'asc']
                    ],
                    orderMulti: true,
                    responsive: true,
                });
            }
        })
    };

从DataTable获取第3列数据的最佳方法是什么?

由于

1 个答案:

答案 0 :(得分:2)

要获取数据,只需使用

即可

tableInstance.column(3).data()

或者,如果你想在一个数组中,

tableInstance.column(3).data().toArray()