自定义React Tabulator中子行格式化的行计数

huangapple go评论62阅读模式
英文:

Customize row counting on children rows formatter React Tabulator

问题

I'm using React Tabulator with Nested data to create data tree table. For example, I have a list of elements with parents and children and "rownum" formatters:

  1. Parent 1
  2. Parent 2

With expanded element in data tree, it will be:

  1. Parent 1
      1. Children 1
      1. Children 2
  2. Parent 2
      1. Children 1

It displays 0 as row count for the children.

Now I want to add decimal counting alignment to the children row numbers with custom formatter function.

How can I achieve this?

  1. Parent 1
    • 1.1. Children 1
    • 1.2. Children 2
  2. Parent 2
    • 2.2. Children 1

Image example: Example

I have tried to customize the row count but cannot handle it

英文:

I'm using React Tabulator with Nested data to create data tree table. For example, I have a list of elements with parents and children and "rownum" formatters:

  1. Parent 1
  2. Parent 2

With expanded element in data tree, it will be:

  1. Parent 1
      1. Children 1
      1. Children 2
  2. Parent 2
      1. Children 1

It displays 0 as row count for the children.

Now I want to add decimal counting alignment to the children row numbers with custom formatter function.

How can I achieve this ?

  1. Parent 1
    • 1.1. Children 1
    • 1.2. Children 2
  2. Parent 2
    • 2.2. Children 1

Image example: Example

I have tried to customize the row count but cannot handle it

答案1

得分: 0

我不确定在tabulator中是否有一种方法可以完成这个操作,但作为一种替代方案,您可以在将表格数据加载到tabulator之前通过添加行号来处理表格数据。这里是一个示例:

const tableData = [
  { name: 'Oli Bob', location: 'United Kingdom', gender: 'male', dob: '14/04/1984', _children: [
      { name: 'Mary May', location: 'Germany', gender: 'female', dob: '14/05/1982' },
      { name: 'Christine Lobowski', location: 'France', gender: 'female', dob: '22/05/1982' },
      { name: 'Brendon Philips', location: 'USA', gender: 'male', dob: '01/08/1980', _children: [
          { name: 'Margret Marmajuke', location: 'Canada', gender: 'female', dob: '31/01/1999' },
          { name: 'Frank Harbours', location: 'Russia', gender: 'male', dob: '12/05/1966' }
        ]
      }
    ]
  },
  { name: 'Jamie Newhart', location: 'India', gender: 'male', dob: '14/05/1985' },
  { name: 'Gemma Jane', location: 'China', gender: 'female', dob: '22/05/1982', _children: [
    { name: 'Emily Sykes', location: 'South Korea', gender: 'female', dob: '11/11/1970' }] },
    { name: 'James Newman', location: 'Japan', gender: 'male', dob: '22/03/1998' }
]

// 添加行号
const numberRows = (data, rowNum) => {
  data.forEach((row, index) => {
    row.rowNum = (rowNum ? rowNum + '.' : '') + (index + 1)

    if (row._children) {
      numberRows(row._children, row.rowNum)
    }
  })
}

numberRows(tableData)

// Tabulator表格
const table = new Tabulator('#table', {
  height: '300px',
  data: tableData,
  dataTree: true,
  dataTreeStartExpanded: true,
  columns: [
    { title: '', field: 'rowNum', width: 100, headerSort: false },
    { title: 'Name', field: 'name', width: 150 },
    { title: 'Location', field: 'location', width: 140 },
    { title: 'Gender', field: 'gender', width: 100 },
    { title: 'Date Of Birth', field: 'dob', width: 140 }
  ]
})
<link href="https://unpkg.com/tabulator-tables@5.5.0/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@5.5.0/dist/js/tabulator.min.js"></script>

<div id="table"></div>

请注意,这是一个示例代码,您需要确保在您的项目中正确引入Tabulator库和其他依赖项,以使此代码正常工作。

英文:

I am not sure if there is a way to do this within tabulator but, as an alternative, you can process the table data by adding row numbers before loading it into tabulator. Here is an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const tableData = [
{ name: &#39;Oli Bob&#39;, location: &#39;United Kingdom&#39;, gender: &#39;male&#39;, dob: &#39;14/04/1984&#39;, _children: [
{ name: &#39;Mary May&#39;, location: &#39;Germany&#39;, gender: &#39;female&#39;, dob: &#39;14/05/1982&#39; },
{ name: &#39;Christine Lobowski&#39;, location: &#39;France&#39;, gender: &#39;female&#39;, dob: &#39;22/05/1982&#39; },
{ name: &#39;Brendon Philips&#39;, location: &#39;USA&#39;, gender: &#39;male&#39;, dob: &#39;01/08/1980&#39;, _children: [
{ name: &#39;Margret Marmajuke&#39;, location: &#39;Canada&#39;, gender: &#39;female&#39;, dob: &#39;31/01/1999&#39; },
{ name: &#39;Frank Harbours&#39;, location: &#39;Russia&#39;, gender: &#39;male&#39;, dob: &#39;12/05/1966&#39; }
]
}
]
},
{ name: &#39;Jamie Newhart&#39;, location: &#39;India&#39;, gender: &#39;male&#39;, dob: &#39;14/05/1985&#39; },
{ name: &#39;Gemma Jane&#39;, location: &#39;China&#39;, gender: &#39;female&#39;, dob: &#39;22/05/1982&#39;, _children: [
{ name: &#39;Emily Sykes&#39;, location: &#39;South Korea&#39;, gender: &#39;female&#39;, dob: &#39;11/11/1970&#39; }] },
{ name: &#39;James Newman&#39;, location: &#39;Japan&#39;, gender: &#39;male&#39;, dob: &#39;22/03/1998&#39; }
]
// Add row numbers
const numberRows = (data, rowNum) =&gt; {
data.forEach((row, index) =&gt; {
row.rowNum = (rowNum ? rowNum + &#39;.&#39; : &#39;&#39;) + (index + 1)
if (row._children) {
numberRows(row._children, row.rowNum)
}
})
}
numberRows(tableData)
// Tabulator table
const table = new Tabulator(&#39;#table&#39;, {
height: &#39;300px&#39;,
data: tableData,
dataTree: true,
dataTreeStartExpanded: true,
columns: [
{ title: &#39;&#39;, field: &#39;rowNum&#39;, width: 100, headerSort: false },
{ title: &#39;Name&#39;, field: &#39;name&#39;, width: 150 },
{ title: &#39;Location&#39;, field: &#39;location&#39;, width: 140 },
{ title: &#39;Gender&#39;, field: &#39;gender&#39;, width: 100 },
{ title: &#39;Date Of Birth&#39;, field: &#39;dob&#39;, width: 140 }
]
})

<!-- language: lang-html -->

&lt;link href=&quot;https://unpkg.com/tabulator-tables@5.5.0/dist/css/tabulator.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;https://unpkg.com/tabulator-tables@5.5.0/dist/js/tabulator.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;table&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 11:16:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定