iT邦幫忙

0

JS DataTable 如何從Json file 拿到data??

  • 分享至 

  • xImage

主要是看這個部分,我想在return data中拿到textOne 和 textTwo的DATA然後DISPLAY出來,下邊這個寫法拿不到2個data

columns: [
        { data: "id" },
        {
            "data": "textOne",
            "data2": "textTwo", //我想
            "render": function(data, type, data2, row, meta) {
                if (type === 'display') {
        data = '<div class="' + data + '">' + data + '</div> + 
                <div class="' + data + '">' + data2 + '</div>'
                }
                return data;
            },
        },

    ],

以下是拿到一個textOne data的代碼
JSON FILE

{
    "data": [{
            "id": "1",
            "textOne": "第一",
            "textTwo": "第二",
        }
    ]
}

JS FILE (dataTable)

$("#Table").DataTable({
    ajax: "data/getData.json",
    sScrollY: jsGetDataTableHeightPx() + "px",
    bPaginate: true,
    bScrollCollapse: true,
    bFilter: false,
    bInfo: true,
    bSort: true,
    bDestroy: true,
    bRetrieve: true,
    dom: "<'row'<'col-sm-12'tr>>" +
        "<'row'<'col-sm-12 col-md-4'i><'col-sm-12 col-md-4 bottom-tool-bar'<'pageLengthList'>><'col-sm-12 col-md-4 controlPageArea'<'bottom-tool'p<'jumpPageBtn'>>>>",
    pageLength: 25,
    columns: [
        { data: "id" },
        {
            "data": "textOne",
            "render": function(data, type, row, meta) {
                if (type === 'display') {
                    data = '<div class="' + data + '">' + data + '</div>'
                }
                return data;
            },
        },

    ],
    columnDefs: [
        { targets: 0, visible: false, searchable: false },
        { targets: 1, width: "100" },
        { targets: 2, width: "150" },
     
    ],
    pagingType: "full_numbers",
    language: {
        info: "_START_ - _END_ of _TOTAL_ items",
        paginate: {
            first: "<i class='fas fa-step-backward'></i>",
            previous: "<i class='fas fa-caret-left'></i>",
            next: "<i class='fas fa-caret-right'></i>",
            last: "<i class='fas fa-step-forward'></i>",
        },
    },
});
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答