Matable
Matable
是一个高效便利的 XLSX文件转网页
的开源工具。
Matable
支持自定义配置,支持各种检索模式,使用简单方便。
Matable
是一个开源的项目,其使用了如下的开源项目
![demo](https://pic.imgdb.cn/item/610e0dfc5132923bf8e69301.jpg)
快速开始
git clone https://github.com/shawroger/Matable.git
或者直接在一个目录准备一个data.xlsx
和index.html
文件,并在index.html
中加入如下内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link href="/matable.css" rel="stylesheet" />
<title>Matable Demo</title>
</head>
<body>
<div id="root"></div>
<script src="/matable.js"></script>
<script>
const Matable = window.Matable;
const { createConf } = Matable;
const table01 = createConf(["data-01.xlsx", "Table 01"], {
name: "",
age: 0,
});
Matable.init({ copyright: true, title: "My Demo" })
.add(table01)
.render("#root");
</script>
</body>
</html>
API
所有的实例方法都支持链式操作
init
创建一个Matable
实例
config
type ValidSearchMode = { key: string; val: string; weight: number }[];
type SearchConfig = {
mode: string | null | ValidSearchMode | string[];
able: boolean;
label: string;
$$val?: string;
sort: boolean;
};
interface Config {
data: string;
title: string;
index: boolean;
footer: boolean;
config: SearchConfig[];
removeFirstLine: boolean;
onLoadData: (data: Array<{ [p: string]: string }>) => void;
onChangePage: (page: number) => void;
onSortData: (
key?: string,
order?: 1 | -1
) => void | ((a: string, b: string) => number);
injectJson: (config: Config) => Record<string, string | number>;
meta: {
[key: string]: any;
};
}
Config
是合法的Matable
配置的数据,Matable
用其来生成动态表单。
完整的Config
类型比较复杂,因此一般使用createConf
函数来创建一个Config
类型数据。
render
渲染到指定的节点
createConf
export function createConf(
info: string | [string, string] | { [key: string]: string },
row: { [key: string]: RowData },
mergeConfig?: Partial<Config> | Record<string, number | string>
): Config;
创建一个合法的Matable
数据配置
const { createConf } = Matable;
const table = createConf(
["$FILE.xlsx", "Table Title"],
{
},
{
onSortData(key) {
if (key === "age") {
return (a, b) => a - b;
}
},
}
);
createMode
export function createMode(row: {
[key: string]: RowData;
}): Partial<GlobalConfig>;
创建合法的Matable
数据检索模式
即生成createConf
的第二个函数参数。
const { createMode, Select } = Matable;
const mode = createMode({
姓名X: false,
姓名0: null,
姓名1: "=",
姓名2: "",
姓名3: ">=",
姓名4: ">",
姓名5: 0,
姓名6: ["甲", "乙"],
姓名7: Select.from(["甲", "乙"]),
});
parseMode
export function parseMode(label: string, mode: RowData): SearchConfig;
生成一列的数据检索模式
const { parseMode } = Matable;
const mode = parseMode("姓名", ">=");
实例方法 resolveData
resolveData
是实例上的一个运行时方法,允许在页面挂载后读取从config
获取的数据。
export type ITableData = Array<{ [p: string]: string }>;
export function resolveData(config: Config): null | ITableData;
const matable = Matable.init({
})
.config( [conf_1])
.render("#root");
const data_01 = matable.resolveData(conf_1);
Select
Select
是一个静态类,提供了两个静态函数
export type SelectMapper = (
key: string,
index: number
) => {
key: string;
val: string;
weight: number;
};
export class Select {
static defaultMap: SelectMapper;
static from(arr: Array<string>, mapper: SelectMapper): ValidSearchMode;
static range(
fromTo: [number, number],
mapper: (n: number) => string
): ValidSearchMode;
}
Select.from
将一个 string[]
转为一个 ValidSearchMode
。
其最直接的应用是实现数据分类 + 排序
的检索模式。
const { createMode, Select } = Matable;
const mode = createMode({
DEMO: Select.from(["A", "B", "C"]),
});
Select.range
提供一个快速生成序列的函数。
const { createMode, Select } = Matable;
const mode = createMode({
班级: Select.range([1, 17], (i) => i + " 班"),
});
鸣谢
Open Source
再次感谢以下的开源项目,没有这些优秀的开源项目,也不可能有Matable
的诞生
打赏
如果您对这个项目感兴趣的话,可以打赏来支持我,反正随缘。
![微信收款](https://pic.downk.cc/item/5ea67b7fc2a9a83be512b52d.png)