tabulator-tables使用总结(避坑大法)

1.快速开始

安装依赖

npm install tabulator-tables --save

构建实例

<template>
  <div id="example-table"></div>
</template>

<script>
import { TabulatorFull as Tabulator } from 'tabulator-tables'
export default {
  name: 'Tabulator',
  components: {},
  props: {},
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {
    var tabledata = [
      { id: 1, name: 'Oli Bob', age: '12', col: 'red', dob: '' }
    ]
    var table = new Tabulator('#example-table', {
      height: 205, 
      data: tabledata, 
      layout: 'fitColumns',
      columns: [
        { title: 'Name', field: 'name', width: 150 },
      ]
    })
    table.on('rowClick', function (e, row) {
      alert('Row ' + row.getData().id + ' Clicked!!!!')
    })
  },
  mounted() {},
  beforeDestroy() {}
}
</script>
<style lang="scss" scoped>
@import '~/tabulator-tables/dist/css/tabulator.min.css';
</style>

2. body单元格换行设置

组件默认会使用ellipsis行为

如果设置columnsformattertextarea时,单元格内容会自动换行;
示例如下:

const columns = [{title: "Name", field: "name", formatter: "textarea"}]

但是如果我们设置了formatter为自定义函数,需要实现单元格换行,官方建议我们设置columnsvariableHeight:true,但是效果是出不来的;所以我们需要在formatter中加入cell.getElement().style.whiteSpace = 'pre-wrap',示例如下:

const columns = [
    {
        title: "Name", 
        field: "name", 
        formatter: function (cell, formatterParams) {
          const value = cell.getValue()
          cell.getElement().style.whiteSpace = 'pre-wrap' // 换行
          return value
        }, 
        variableHeight: true // 设置这里其实是无效的
    }
]

3. header单元格换行设置

组件默认会使用ellipsis行为

组件默认是不提供header单元格换行的,所以我们需要手动加入css样式来实现换行,代码如下:

.tabulator .tabulator-header .tabulator-col .tabulator-col-content .tabulator-col-title {
  white-space: normal;
  text-overflow: clip;
}

如果想动态实现是否启用header单元格换行设置,我们可以使用动态className的方式来实现,代码如下:

<template>
   <div :class="['table-complex', { 'header-alter--row': true }]"></div>
</template>
.header-alter--row .tabulator-header .tabulator-col .tabulator-col-content .tabulator-col-title
{
  white-space: normal;
  text-overflow: clip;
}

4. 排序设置

组件默认点击排序后,只有两种状态,升序和降序,不能回到默认排序;如果需要支持回到默认排序,需要设置headerSortTristate: true,示例如下:

new Tabulator('#example-table', {
  height: 205, 
  data: [{ id: 1, name: 'Oli Bob', age: '12', col: 'red', dob: '' }], 
  layout: 'fitColumns',
  columns: [
    { title: 'Name', field: 'name', width: 150, headerSortTristate: true } // 支持三种状态的排序
  ]
})

默认排序规则如下:

new Tabulator('#example-table', {
  height: 205, 
  data: [{ id: 1, name: 'Oli Bob', age: '12', col: 'red', dob: '' }], 
  layout: 'fitColumns',
  columns: [
    { 
        title: 'Name', 
        field: 'name', 
        width: 150, 
        // 默认排序规则
        sorter: function(a, b){
            return String(a).toLowerCase().localeCompare(String(b).toLowerCase())
        }
    } 
  ]
})

5. layout完美兼容方案

如果想组件有个比较完美的显示方式,我们需要动态切换layout: 'fitData'layout: 'fitColumns'

一开始使用layout: 'fitData'去尝试渲染组件,看数据是否能够铺满表格,如果能铺满,证明这个时候出来的效果是ok的;如果不能铺满表格,也就是.tabulator-tableholder的宽度小于.tabulator-table的宽度,这个时候启用layout: 'fitColumns'即可;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Django中使用Tabulator加载pandas DataFrame数据并在前端展示,可以按照以下步骤进行: 1. 安装Tabulator和pandas库: ``` pip install tabulator pandas ``` 2. 在Django中创建一个视图函数来读取pandas DataFrame数据,并将其转换为Tabulator支持的JSON格式: ```python import pandas as pd from django.http import JsonResponse def load_data(request): # 读取pandas DataFrame数据 df = pd.read_csv('path/to/data.csv') # 将DataFrame数据转换为JSON格式 json_data = df.to_dict(orient='records') # 返回JSON数据 return JsonResponse(json_data, safe=False) ``` 3. 在前端页面中使用Tabulator来加载JSON数据并展示: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tabulator Demo</title> <link rel="stylesheet" href="https://unpkg.com/tabulator-tables@4.9.3/dist/css/tabulator.min.css"> <script src="https://unpkg.com/tabulator-tables@4.9.3/dist/js/tabulator.min.js"></script> </head> <body> <div id="example-table"></div> <script> // 创建Tabulator实例并加载数据 var table = new Tabulator("#example-table", { ajaxURL: "/load_data/", layout: "fitColumns", columns: [ { title: "Column 1", field: "col1" }, { title: "Column 2", field: "col2" }, { title: "Column 3", field: "col3" } ] }); </script> </body> </html> ``` 在上述代码中,Tabulator实例被创建并设置为从`/load_data/` URL加载数据。`layout`属性设置为`fitColumns`,使表格自适应列宽。`columns`属性设置表格列的标题和字段名。 注意,在Django项目中,需要在`urls.py`中添加一个URL路由来映射到`load_data`视图函数: ```python from django.urls import path from .views import load_data urlpatterns = [ path('load_data/', load_data, name='load_data'), ] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值