Search code examples
tabulator

How to get all groups from tabulator table


I need an array of "group" objects from a multi-level-grouped tabulator table where

group = { field: String, name: String }

but when I try to parse the payload of tabulator.getGroups(), I'm missing one of the groups. Is there a tabulator api function for retrieving all the groups from a table? tabulator.getGroups() was the only function I could find in the docs.

https://codepen.io/awcastellano/pen/vMpydW

let data = [
  {
    "partType": "Disc Brake Pad",
    "vehicle": "2019 chevy silverado",
    "brand": "Cardone",
    "partApplication": "Left Front/MKD794"
  },
  {
    "partType": "Disc Brake Pad",
    "vehicle": "2019 ford F150",
    "brand": "STS",
    "partApplication": "Left Front/MKD794"
  },
  {
    "partType": "Disc Brake Rotor",
    "vehicle": "2019 chevy silverado",
    "brand": "Cardone",
    "partApplication": "Left Front/MKD795"
  },
  {
    "partType": "Disc Brake Pad",
    "vehicle": "2019 chevy silverado",
    "brand": "Cardone",
    "partApplication": "Left Front/MKD795"
  },
  {
    "partType": "Disc Brake Caliper",
    "vehicle": "2019 chevy silverado",
    "brand": "Cardone",
    "partApplication": "Left Front/MKD796"
  }
]

let columns = [{
    title: "Part",
    field: "partApplication"
}, ]

let table = new Tabulator('#tabulator', {
                    data: data,
                    columns: columns,
                    groupBy: ["vehicle", "partType", "brand"]
                })

function groups() {
  let groups = []
  let groupComponents = table.getGroups()
  if (groupComponents.length == 0) return groups
  for (var i = 0; i < groupComponents.length; i++) {
    groups.push({ field: groupComponents[i]._group.field, name: groupComponents[i]._group.key })
  }
  let groupList = groupComponents[0]._group.groupList
  while (groupList.length != 0) {
    for (var i = 0; i < groupList.length; i++) {
      groups.push({ field: groupList[i].field, name: groupList[i].key })
    }
    groupList = groupList[0].groupList
  }
  return groups
}

Call groups() in the codepen console. { field: "brand", name: "STS" } is missing from the array, yet is displayed in the table. I tried to paste the result here but couldn't figure out how to copy the result from codepen.

Thanks in advance!


Solution

  • Check Console

    https://codepen.io/dota2pro/pen/NmVzmv

       const getAllGroups = function(input){
      for(let i=0; i< input.length; i++){
    
        console.log(allRows[i].getGroup());
        // Use this or 
            console.log(allRows[i].getGroup()._group.key 
    
    ,allRows[i].getGroup()._group.field);
    
    // Use this 
          console.log('field:  ',  input[i]._row.modules.group.field,'key:',input[i]._row.modules.group.key );
    
        }}