代码之家  ›  专栏  ›  技术社区  ›  plusvictoria

如何修改csv ajax请求中的datatables列?

  •  0
  • plusvictoria  · 技术社区  · 6 年前

    我正在用csv文件中的数据填充数据表。文件中有一些宏逻辑(即column2=column1*3,column4=column3*3…)。

    我正在发出ajax请求以从csv文件获取数据,并在填充表之前使用jquery csv库解析文件。

    如何将乘法逻辑应用于表中的某些列?

    $.ajax({
        url: "../data/data.csv",
        dataType: "text",
        cache: false,
        success: function(csvs){
            data = $.csv.toObjects(csvs);
            table.rows.add(data).draw();
        }
    });
    
    var table = $('#totals-table').DataTable({
    dom: '<"top"Bf>rt<"bottom"lp>',
    buttons: [
        'copy', 'csv', 'excel'
    ],
    columns: [
        {
            "title": "col1",
            "data": "col1"
        },
        {
            "title": "col2",
            "data": "col2"
        },
        {
            "title": "col3",
            "data": "col3"
        },
        {
            "title": "col4",
            "data": "col4"
        }
    ]
    

    });

    1 回复  |  直到 6 年前
        1
  •  0
  •   Sachi Tekina    6 年前

    你可以用 columnDefs 在数据表上,如下所示:

    $('#totals-table').DataTable({
      columnDefs: [{
        "targets": 4,// index of the column where the result should display
        "render": function(data, type, column) {
          return column[4] * column[7]; //target the column you want to multiply by its index
        }
      }]
    })
    

    Fiddle here.