タビュレーター:各行の計算値を使用してカスタム列を追加することは可能ですか?

Aug 24 2020

各行の計算値を使用してカスタム列を追加することは可能ですか?したがって、列1の値が「3」で列2の値が「5」の場合、合計値が8の動的列3が必要です。

VueTabulatorを使用しています。これが私の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>

そして、私はデータ出力をテーブル内でこのように見せたいです。

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

ドキュメントを調べていましたが、何も見つかりませんでした。

ありがとう

回答

1 OliFolkerd Aug 25 2020 at 01:56

これを実現するには、ミューテーターを使用する必要があります。

値を持つ2つの列を含むサンプルデータから始める場合

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

次に、これら2つの行の合計を計算するカスタムミューテーター関数を定義します

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.
}

次に、列の定義で、作成されたフィールド名を使用して3番目の列を追加し、mutatorプロパティに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
]

詳細については、Mutatorのドキュメントをご覧ください。