保持选定项在使用 Tabulator 单击选定行时。

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

Keep selection when selected row is clicked with tabulator

问题

在Tabulator中,是否有一个选项,可以在点击行时保持行的选择状态?

例如 - 假设我们有一个包含3行的表格。当我点击第二行时,它会被选中。我希望再次点击第二行时不会取消选择它(但在使用selectable: 1时,点击其他行会取消选择它)。

英文:

Is there an option in tabulator to keep the selection of a row if it is clicked?

For example - assume we have a table of 3 rows. When I click the second row it selected. I would like that click on the second row again will not deselect it (but clicking other rows will deselect it when using selectable: 1).

答案1

得分: 1

您可以在rowClick事件上调用row.select()方法来实现您想要的功能。这将确保在单击时始终选择行,但仍然在单击另一行时取消选择。以下是一个示例:

const data = [
  { id: 1, name: 'Billy Bob', age: '12', gender: 'male', height: 1, col: 'red', dob: '', cheese: 1 },
  { id: 2, name: 'Mary May', age: '1', gender: 'female', height: 2, col: 'blue', dob: '14/05/1982', cheese: true },
  { id: 10, name: 'Margret Marmajuke', age: '16', gender: 'female', height: 5, col: 'yellow', dob: '31/01/1999' }
]

const table = new Tabulator('#example-table', {
  data: data,
  selectable: 1,
  columns: [
    { title: 'Name', field: 'name' },
    { title: 'Age', field: 'age' },
    { title: 'Gender', field: 'gender' },
    { title: 'Height', field: 'height' },
    { title: 'Favourite Color', field: 'col' }
  ]
})

table.on('rowClick', (e, row) => {
  row.select()
})
<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="example-table"></div>

这段代码允许您在单击表格行时选择行,并在单击另一行时取消选择行。

英文:

You can call row.select() method on the rowClick event to do what you are looking for. This will ensure the row is always selected on click but still deselected when another row is clicked. Here is an example:

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

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

const data = [
  { id: 1, name: &#39;Billy Bob&#39;, age: &#39;12&#39;, gender: &#39;male&#39;, height: 1, col: &#39;red&#39;, dob: &#39;&#39;, cheese: 1 },
  { id: 2, name: &#39;Mary May&#39;, age: &#39;1&#39;, gender: &#39;female&#39;, height: 2, col: &#39;blue&#39;, dob: &#39;14/05/1982&#39;, cheese: true },
  { id: 10, name: &#39;Margret Marmajuke&#39;, age: &#39;16&#39;, gender: &#39;female&#39;, height: 5, col: &#39;yellow&#39;, dob: &#39;31/01/1999&#39; }
]

const table = new Tabulator(&#39;#example-table&#39;, {
  data: data,
  selectable: 1,
  columns: [
    { title: &#39;Name&#39;, field: &#39;name&#39; },
    { title: &#39;Age&#39;, field: &#39;age&#39; },
    { title: &#39;Gender&#39;, field: &#39;gender&#39; },
    { title: &#39;Height&#39;, field: &#39;height&#39; },
    { title: &#39;Favourite Color&#39;, field: &#39;col&#39; }
  ]
})

table.on(&#39;rowClick&#39;, (e, row) =&gt; {
  row.select()
})

<!-- 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;example-table&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 14:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350195.html
匿名

发表评论

匿名网友

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

确定