Tabulatore: è possibile aggiungere una colonna personalizzata con un valore calcolato per ogni riga?

Aug 24 2020

È possibile aggiungere una colonna personalizzata con un valore calcolato per ogni riga? Quindi, se col 1 ha valore "3" e col 2 ha valore "5", allora voglio col 3 dinamico con valore somma di 8.

Sto usando Vue Tabulator. Ecco il mio componente Table.vue.

<template>
    <div v-if="tabledata" class="w-full p-3 border border-gray-200 shadow-xl rounded-lg  mt-10">
        <VueTabulator id="usersData" v-model="tabledata" :options="options" />
    </div>
</template>
<script>
export default {
    data(){
        return{
            tabledata: [
                {
                    "name": "Hron",
                    "lvl2": 5,
                    "lvl3": 0,
                    "score": 5
                },
                {
                    "name": "Zhr",
                    "lvl2": 2,
                    "lvl3": 1,
                    "score": null
                },
                {
                    "name": "Mang",
                    "lvl2": 4,
                    "lvl3": 1,
                    "score": null
                }
            ],
            options: {
                layout:"fitColumns",
                columns: [
                    {title:"S#", formatter:"rownum", width:70, align:"center", responsive:3},
                    {title:"Name", field:"name", minWidth:150},
                    {title:"Lvl 2", field:"lvl2", sorter:"number", align:"center", formatter:"money", formatterParams:{precision:false}, bottomCalc:"sum", minWidth:80},
                    {title:"Lvl 3", field:"lvl3", sorter:"number", align:"center", formatter:"money", formatterParams:{precision:false}, bottomCalc:"sum", minWidth:80},
                    {title:"Score", field:"score", sorter:"number", align:"center", formatterParams:{precision:true}, bottomCalc:"avg"}
                ]
            }
        }
    }
}
</script>

E voglio che l'output dei dati assomigli a questo tavolo interno.

[
  {
    "name": "Hron",
    "lvl2": 5,
    "lvl3": 0,
    "score": 5
  },
  {
    "name": "Zhr",
    "lvl2": 2,
    "lvl3": 1,
    "score": 3
  },
  {
    "name": "Mang",
    "lvl2": 4,
    "lvl3": 1,
    "score": 5
  }
]

Ho cercato nella documentazione ma non sono riuscito a trovare nulla.

Grazie

Risposte

1 OliFolkerd Aug 25 2020 at 01:56

Ti consigliamo di utilizzare un mutatore per raggiungere questo obiettivo.

Se iniziamo con alcuni dati di esempio contenenti le due colonne con i valori

var data = [
    {col1:27, col2:55}
];

Quindi definisci una funzione mutator personalizzata che calcola la somma di queste due righe

var customMutator = function(value, data, type, params, component){
    //value - original value of the cell
    //data - the data for the row
    //type - the type of mutation occurring  (data|edit)
    //params - the mutatorParams object from the column definition
    //component - when the "type" argument is "edit", this contains the cell component for the edited cell, otherwise it is the column component for the column

    return data.col1 + data.col2 ; //return the sum of the other two columns.
}

Quindi nella nostra definizione di colonna aggiungiamo la nostra terza colonna con un nome di campo inventato e impostiamo la funzione mutator sulla proprietà mutator :

columns:[
    {title:"Column 1", field:"col1"},
    {title:"Column 2", field:"col2"},
    {title:"Sum Column", field:"sumCol", mutator:customMutator}, //make column the sum of the other two columns
]

I dettagli completi possono essere trovati nella documentazione di Mutator