根据服务代码对数据进行分组,并使用Jquery Datatable显示数据

时间:2019-03-14 09:49:31

标签: javascript jquery datatable

我想要的是,我想基于来自数据表的Service code按数据分组。我想根据datatable中的选定值进行分组。

下面是我的数据表屏幕截图

Datatable

在上图中,我已经选中了四个复选框。因此,我想基于groupBy Service Code这4列数据。

假设前两个已检查行的服务代码为3297392,而其他两个已检查行的服务代码为3293490,那么它应该在下面显示我的数据表

Display Data

通过分组,它还会添加SAP ID列和“资产计数”。

我无法获得确切的逻辑,但是我尝试过的方法如下所示

function ViewGroupBySummaryData() {

var tableToViewSummary = $('#confirmToFECertify');
tableToViewSummary.empty();

var table = $(dtCertifyEFO).DataTable();
var data = table.rows('.selected').data();   
}

data中,我获得了选定行但没有列的值。

数据如下所示,在控制台中

0:(14)[
"", "<a class=" actionIcon" id="discripancy"><i class="fa fa-flag"></i></a>", "I-AP-APRR-ENB-B002", "INAPAPRRIDETTW6001", "GANESH NAGAR_AT2001", "", "3297392", "MAINTENANCE OF IP COLO ENODEB SITE / IP Colo", "ACTIVE", "12/11/2014", "", "Mahindra &amp; Mahindra Limited - 168888", "", "" ]1:(14)[
"", "<a class=" actionIcon" id="discripancy"><i class="fa fa-flag"></i></a>", "I-AP-APRR-ENB-G001", "INAPAPRRYRNLNB6001", "VENUGOPAL NAGAR", "", "3297392", "MAINTENANCE OF IP COLO ENODEB SITE / IP Colo", "ACTIVE", "12/11/2014", "", "Mahindra &amp; Mahindra Limited - 168888", "", "" ]2:(14)[
"", "<a class=" actionIcon" id="discripancy"><i class="fa fa-flag"></i></a>", "I-AP-BRYS-ENB-6000", "INAPBRYSBRYSTW6001", "Bukkarayasamudram", "", "3293490", "MAINTENANCE OF RCOM COLO ENODEB SITE / R-Colo", "ACTIVE", "12/11/2014", "", "Mahindra &amp; Mahindra Limited - 168888", "", "" ]3:(14)[
"", "<a class=" actionIcon" id="discripancy"><i class="fa fa-flag"></i></a>", "I-AP-BRYS-ENB-6001", "INAPBRYSXXXXTW6003", "CHENNAMPALLE", "", "3293490", "MAINTENANCE OF RCOM COLO ENODEB SITE / R-Colo", "ACTIVE", "12/11/2014", "", "Mahindra &amp; Mahindra Limited - 168888", "", "" ]

请帮助我使用jquery数据表构建逻辑。

下面是我的HTML

<table id="confirmToFECertify" class="myTable table table-striped table-bordered nowrap" cellspacing="0"></table>

1 个答案:

答案 0 :(得分:1)

这是方法:

显示一个新的数据表,并用代码和所选元素的总和填充其行。该代码段并未真正模拟click事件,因此未对其进行测试。 r[5](分别为r[6])对应于SAP ID和assets count的行。在您提供的console.log信息中,我真的看不到这些字段的值。您可以更改56以满足表字段的位置。

function ViewGroupBySummaryData() {

    var tableToViewSummary = $('#confirmToFECertify');
    tableToViewSummary.empty();

    console.log($(dtCertifyEFO));

    var table = $(dtCertifyEFO).DataTable();
    var data = table.rows('.selected').data().toArray();

    var newData = {};
    data.forEach(r => {
        var code = newData[r[6]]
        if (code) {
            // if the code is already present sum the value of each row
            newData[code] = [newData[code][0] + parsefloat(r[5]), newData[code][1] + parsefloat(r[7])]
        } else {
            // add new row corresponding to the code
            newData[code] = [parsefloat(r[5]), parsefloat(r[7])]
        }
    })
    // create new DataTable
    var t = $('#example').DataTable();

    // add rows to the new datatable
    Object.keys(newData).forEach(function(k) {
        var values = [];
        values.push(k);
        for ( var i = 0; i < newData(k).length; i++) {
          values.push(i)
        }
        t.row.add(values)
    })

    // display the new datatable

}