简体   繁体   English

向 bootstrap-vue 表中的数字列添加百分号

[英]Add percentage sign to column of numbers in bootstrap-vue table

Suppose I have this table created using bootstrap-vue .假设我使用bootstrap-vue创建了这个表。

Table looks like this;表看起来像这样; 在此处输入图像描述

The code for this table is as follows;该表的代码如下;

<template>
  <div>
    <b-table striped hover :items="items" :fields="fields"></b-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // Note 'isActive' is left out and will not appear in the rendered table
        fields: [
          {
            key: 'last_name',
            sortable: true
          },
          {
            key: 'first_name',
            sortable: false
          },
          {
            key: 'age',
            label: 'Person age',
            sortable: true,
            // Variant applies to the whole column, including the header and footer
            variant: 'danger'
          }
        ],
        items: [
          { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
          { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
        ]
      }
    }
  }
</script>

I would like to add percentage sign as a suffix to the numbers in the Age column but the column is to remain sortable according to the number value.我想添加百分号作为Age列中数字的后缀,但该列仍可根据数值进行排序。 How can the code be modified to do this?如何修改代码来做到这一点?

The code example is extracted from link below;代码示例是从下面的链接中提取的; https://bootstrap-vue.org/docs/components/table https://bootstrap-vue.org/docs/components/table

I am using vue.js v2.6我正在使用 vue.js v2.6

The straightforward implementation is to use the formatted callback function provided in BootstrapVue to add the percentage sign.直接的实现方式就是使用BootstrapVue中提供的格式化回调function来添加百分号。 Next, set the field property sortByFormatted to false so that sorting is done according to the unformatted original number values.接下来,将字段属性sortByFormatted设置为 false,以便根据未格式化的原始数值进行排序。

Relevant documentation links;相关文档链接; https://bootstrap-vue.org/docs/components/table#formatter-callback https://bootstrap-vue.org/docs/components/table#field-definition-reference https://bootstrap-vue.org/docs/components/table#formatter-callback https://bootstrap-vue.org/docs/components/table#field-definition-reference

Here's the code change needed;这是需要更改的代码;

<template>
  <div>
    <b-table striped hover :items="items" :fields="fields"></b-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // Note 'isActive' is left out and will not appear in the rendered table
        fields: [
          {
            key: 'last_name',
            sortable: true
          },
          {
            key: 'first_name',
            sortable: false
          },
          {
            key: 'age',
            label: 'Person age',
            sortable: true,
            // Variant applies to the whole column, including the header and footer
            variant: 'danger',
            //Code modification needed. Add the 2 properties below;
            "sortByFormatted": false,
            formatter: value => {
                return (value + '%')
            },          
          }
        ],
        items: [
          { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
          { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
        ]
      }
    }
  }
</script>

If you can directly modify your data, you can simply use Array.prototype.map :如果你可以直接修改你的数据,你可以简单地使用Array.prototype.map

export default {
  data() {
    return {
      items: [
        { age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
        { age: 21, first_name: 'Larsen', last_name: 'Shaw' },
        { age: 89, first_name: 'Geneva', last_name: 'Wilson' },
        { age: 38, first_name: 'Jami', last_name: 'Carney' }
      ].map(item => (
        item.age = item.age + "%",
        item
      )),
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM