Tabulator.js 使用图标列作为行标题

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

Tabulator.js Using Icon Column as Row header

问题

我刚开始使用tabulator.js。我创建了一个表格,其中第一列是一个图标列,当高亮显示一行时,应该显示一个右箭头。也就是说,我想要做的是只在高亮显示的行上显示箭头,使用rowMouseEnter(显示箭头)和rowMouseLeave(隐藏箭头),如图片所示。

Tabulator.js 使用图标列作为行标题

代码:

$(function(){
    table.on("rowMouseEnter", function(e, row){
        // 这里是显示行的第一个单元格中的箭头的代码。
    });

    table.on("rowMouseLeave", function(e, row){
        // 这里是清除行的第一个单元格中的代码。
    });
});

var table = new Tabulator("#table", {
    data: dataTable,
    layout:"fitColumns",
    addRowPos: "bottom",
    tabEndNewRow: true,
    columns:[
        {headerSort:false, frozen:true, width:30, minWidth:30},
        {title:"Name", field:"name"},
        {title:"Progress", field:"progress", sorter:"number"},
        {title:"Gender", field:"gender"},
        {title:"Rating", field:"rating"},
        {title:"Favourite Color", field:"col"},
        {title:"Date Of Birth", field:"dob"}
    ]
});
英文:

I just started using tabulator.js. I've created a table with the first column as an icon column that should show a right arrow when a row is highlighted. That is, what I want to do is show the arrow only on the highlighted row using rowMouseEnter (show arrow) and rowMouseLeave (hide arrow) as shown in the picture.

Tabulator.js 使用图标列作为行标题

Code:

	$(function(){
		table.on("rowMouseEnter", function(e, row){
			// Here code to show arrow in the first cell of the row.
		});

		table.on("rowMouseLeave", function(e, row){
			// Here code clear the first cell of the row.
		});
	});

	var table = new Tabulator("#table", {
		data: dataTable,
		layout:"fitColumns",
		addRowPos: "bottom",
		tabEndNewRow: true,
		columns:[
			{headerSort:false, frozen:true, width:30, minWidth:30},
            {title:"Name", field:"name"},
            {title:"Progress", field:"progress", sorter:"number"},
            {title:"Gender", field:"gender"},
            {title:"Rating", field:"rating"},
            {title:"Favourite Color", field:"col"},
            {title:"Date Of Birth", field:"dob"}
		]
	});

答案1

得分: 2

我看到你在我输入时已发布了你自己的答案 Tabulator.js 使用图标列作为行标题 很高兴你找到了答案!我会将我的看法作为另一种选择留在这里:

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

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

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: true,
  columns: [
    { headerSort:false, frozen:true, width:30, minWidth:30},
    { title: 'Name', field: 'name' },
    { title: 'Age', field: 'age' },
    { title: 'Gender', field: 'gender' },
    { title: 'Height', field: 'height' },
    { title: 'Favourite Color', field: 'col' }
  ]
})

function rowArrow(row, arrow) {
  let cells = row.getCells()
  let el = cells[0].getElement()

  el.innerHTML = arrow ? '<div class="arrow-right"></div>' : ''
}

table.on("rowMouseEnter", (e, row) => rowArrow(row, true))
table.on("rowMouseLeave", (e, row) => rowArrow(row, false))

&lt;!-- language: lang-css --&gt;

.arrow-right {
  width: 0; 
  height: 0; 
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 5px solid #333;
  margin: 3px auto;
}

&lt;!-- language: lang-html --&gt;

<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>

&lt;!-- end snippet --&gt;

请注意,这是您提供的原始代码,没有进行任何翻译。

英文:

I see you posted your own answer while I was typing Tabulator.js 使用图标列作为行标题 Glad you found the answer! I will leave my take on it here as an alternative:

<!-- 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: true,
  columns: [
    { headerSort:false, frozen:true, width:30, minWidth:30},
    { 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; }
  ]
})

function rowArrow(row, arrow) {
  let cells = row.getCells()
  let el = cells[0].getElement()

  el.innerHTML = arrow ? &#39;&lt;div class=&quot;arrow-right&quot;&gt;&lt;/div&gt;&#39; : &#39;&#39;
}

table.on(&quot;rowMouseEnter&quot;, (e, row) =&gt; rowArrow(row, true))
table.on(&quot;rowMouseLeave&quot;, (e, row) =&gt; rowArrow(row, false))

<!-- language: lang-css -->

.arrow-right {
  width: 0; 
  height: 0; 
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 5px solid #333;
  margin: 3px auto;
}

<!-- 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 -->

答案2

得分: 0

已解决!!!

Tabulator.js文档非常棒。

以下是代码:

table.on("rowMouseEnter", function(e, row){
    let cells = row.getCells();
    let el = cells[0].getElement();
    el.innerHTML = "<i class='material-icons'>arrow_right</i>";
});

table.on("rowMouseLeave", function(e, row){
    let cells = row.getCells();
    cells[0].setValue("");
});
英文:

Solved !!!

Tabulator.js documentation is great.

Here the code:

table.on(&quot;rowMouseEnter&quot;, function(e, row){
	let cells = row.getCells();
	let el = cells[0].getElement();
	el.innerHTML = &quot;&lt;i class=&#39;material-icons&#39;&gt;arrow_right&lt;/i&gt;&quot;;
});

table.on(&quot;rowMouseLeave&quot;, function(e, row){
	let cells = row.getCells();
	cells[0].setValue(&quot;&quot;);
});

huangapple
  • 本文由 发表于 2023年7月14日 05:27:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683345.html
匿名

发表评论

匿名网友

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

确定