1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Vue.jsでTabulator(5系)を使う①

Last updated at Posted at 2023-03-19

Vue.jsでTabulator(5系)を使う①(備忘録)

Vue3.2+TypeScript , Tabulator(5系)

Tabulator とは?

業務系アプリケーションで多用される〇〇リストを扱うためのライブラリ。
これを使うと、たぶんみんな幸せになれる(あくまでも個人の感想)
詳しくは >> https://tabulator.info/
image.png

①最低限のコードで使ってみる

インストール

npm install tabulator-tables --save
npm install @types/tabulator-tables -D

リストに表示するデータの定義

interface State {
  list: TableDataRow[];
}
type TableDataRow = {
  id: number;
  value: number;
  update: string;
};
const state = reactive<State>({
  list: [
    { id: 1, value: 1, update: dayjs().format('x') },
    { id: 2, value: 2, update: dayjs().format('x') },
  ],
});

各カラム設定

const columns: any[] = [
  { field: 'id', title: 'id' },
  { field: 'value', title: 'value' },
  { field: 'update', title: 'update' },
];

Tabulatorを挿入するタグを用意

const tableElm = ref<HTMLElement | null>(null); 
<div ref="tableElm"></div>

Tabulatorの初期化

const columns: any[] = [
  { field: 'id', title: 'id' },
  { field: 'value', title: 'value' },
  { field: 'update', title: 'update' },
];
//htmlタグがマウントさたらTabulatorを生成してもらう
onMounted(() => {
  if (tableElm.value === null) return;
  new Tabulator(tableElm.value, {
    columns: columns,
    data: state.list,
    reactiveData: true,
  });
});

結果

image.png

ちなみにSFCファイル全体

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { onMounted } from 'vue';
import { TabulatorFull as Tabulator } from 'tabulator-tables';
import dayjs from 'dayjs';
interface State {
  list: TableDataRow[];
}
type TableDataRow = {
  id: number;
  value: number;
  update: string;
};

const state = reactive<State>({
  list: [
    { id: 1, value: 1, update: dayjs().format('x') },
    { id: 2, value: 2, update: dayjs().format('x') },
  ],
});

const columns: any[] = [
  { field: 'id', title: 'id' },
  { field: 'value', title: 'value' },
  { field: 'update', title: 'update' },
];
//--[tabulator]
const tableElm = ref<HTMLElement | null>(null); 
onMounted(() => {
  if (tableElm.value === null) return;
  new Tabulator(tableElm.value, {
    columns: columns,
    data: state.list,
    reactiveData: true,
  });
});
</script>
<template>
  <div class="border border-danger p-2 rounded mb-2">
    ① 最低限のコードで使ってみる
    <div ref="tableElm"></div>
  </div>
</template>

Tabulator4系からは・・・

イベントの扱い、HeaderSortなどプロパティに変更が見られます。
ブラウザの初期化プロパティで使えないやつが含まれていると警告がConsole出力されると思う。多分。

関連記事

GitHubにサンプル上げてます

ついでにGitHubにデモページも・・・

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?