制表符/以编程方式更正 table 数据(从 HTML <table> 加载)
Tabulator / programmatically correct table data (loaded from HTML <table>)
Tabulator
是从 unknown HTML <table>
(具有 TH headers,因此所有列都有名称)创建的。
因此,无法触发任何 mutator
或 callback
... 除了 htmlImported:function()
.
因为 HTML <table>
包含格式错误的 DATE 值,我需要访问所有 rows
中的所有 fields
来发现并更正这些。
整个代码为:
<!DOCTYPE html>
<html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/tabulator-tables@4.3.0/dist/css/">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.3.0/dist/js/tabulator.min.js"></script>
</head><body>
<table id="tablexxx">
<tr>
<th>Name</th><th>Favorite Color</th><th>date</th></tr>
<tr><td>Bob</td><td>Yellow</td><td>1/1/2001</td>
</tr>
<tr><td>Michelle</td><td>Purple</td><td>2/1/2000</td>
</tr>
</table>
<div id="tablexxx"></div>
<script>
var tablexxx = new Tabulator("#tablexxx", {});
var rows = tablexxx.getRows();
rows.forEach(function(row)
{ var rowData = row.getData();
rowData.forEach(function(cell) // <<< HOW TO DO IT RIGHT???
{
// get row field NAME
var field = cell.name; // <<< HOW TO DO IT RIGHT???
if(field.indexOf("date") >= 0)
{
// get colum field VALUE
var date = cell.value; // <<< HOW TO DO IT RIGHT???
// make new value using proper format
var ndate = date[0] + date[1] + date[2]; // TODO
// update row with {"date":"01/02/2000"}
row.update("{" + field + ":" + ndate + "}");
}
});
});
</script></body></html>
有人可以告诉我如何 "DO IT RIGHT"
上面指定的 <script>
部分滞后吗?
我希望我明白你想要完成什么...
如果 < 10,则用 0 填充日和月。
首先获取包含数据字段的列,然后遍历该列中的每个单元格,拆分日期并更新单元格。
我实际上不知道为什么 table 中有一个空行(这就是 if (value)
的用途)。是tabulator插入的东西
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
var tablexxx = new Tabulator("#tablexxx", {});
var dateColumn = tablexxx.getColumn("date");
for (var cell of dateColumn.getCells()) {
var value = cell.getValue();
if (value) {
value = value.split("/")
cell.setValue(pad(value[0], 2) + "/" + pad(value[1], 2) + "/" + value[2]);
}
}
<link rel="stylesheet" href="https://unpkg.com/tabulator-tables@4.3.0/dist/css/">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.3.0/dist/js/tabulator.min.js"></script>
<table id="tablexxx">
<tr>
<th>Name</th><th>Favorite Color</th><th>date</th></tr>
<tr><td>Bob</td><td>Yellow</td><td>1/1/2001</td>
</tr>
<tr><td>Michelle</td><td>Purple</td><td>2/1/2000</td>
</tr>
</table>
Tabulator
是从 unknown HTML <table>
(具有 TH headers,因此所有列都有名称)创建的。
因此,无法触发任何 mutator
或 callback
... 除了 htmlImported:function()
.
因为 HTML <table>
包含格式错误的 DATE 值,我需要访问所有 rows
中的所有 fields
来发现并更正这些。
整个代码为:
<!DOCTYPE html>
<html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/tabulator-tables@4.3.0/dist/css/">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.3.0/dist/js/tabulator.min.js"></script>
</head><body>
<table id="tablexxx">
<tr>
<th>Name</th><th>Favorite Color</th><th>date</th></tr>
<tr><td>Bob</td><td>Yellow</td><td>1/1/2001</td>
</tr>
<tr><td>Michelle</td><td>Purple</td><td>2/1/2000</td>
</tr>
</table>
<div id="tablexxx"></div>
<script>
var tablexxx = new Tabulator("#tablexxx", {});
var rows = tablexxx.getRows();
rows.forEach(function(row)
{ var rowData = row.getData();
rowData.forEach(function(cell) // <<< HOW TO DO IT RIGHT???
{
// get row field NAME
var field = cell.name; // <<< HOW TO DO IT RIGHT???
if(field.indexOf("date") >= 0)
{
// get colum field VALUE
var date = cell.value; // <<< HOW TO DO IT RIGHT???
// make new value using proper format
var ndate = date[0] + date[1] + date[2]; // TODO
// update row with {"date":"01/02/2000"}
row.update("{" + field + ":" + ndate + "}");
}
});
});
</script></body></html>
有人可以告诉我如何 "DO IT RIGHT"
上面指定的 <script>
部分滞后吗?
我希望我明白你想要完成什么...
如果 < 10,则用 0 填充日和月。
首先获取包含数据字段的列,然后遍历该列中的每个单元格,拆分日期并更新单元格。
我实际上不知道为什么 table 中有一个空行(这就是 if (value)
的用途)。是tabulator插入的东西
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
var tablexxx = new Tabulator("#tablexxx", {});
var dateColumn = tablexxx.getColumn("date");
for (var cell of dateColumn.getCells()) {
var value = cell.getValue();
if (value) {
value = value.split("/")
cell.setValue(pad(value[0], 2) + "/" + pad(value[1], 2) + "/" + value[2]);
}
}
<link rel="stylesheet" href="https://unpkg.com/tabulator-tables@4.3.0/dist/css/">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.3.0/dist/js/tabulator.min.js"></script>
<table id="tablexxx">
<tr>
<th>Name</th><th>Favorite Color</th><th>date</th></tr>
<tr><td>Bob</td><td>Yellow</td><td>1/1/2001</td>
</tr>
<tr><td>Michelle</td><td>Purple</td><td>2/1/2000</td>
</tr>
</table>