我试图在handsontable 的每一行末尾添加一个自定义保存按钮。 我在 Laravel 4 中使用 Handsontable 包。
按钮显示如下:
<button>Save</button>
尝试使用
htmlRenderer
演示:http://docs.handsontable.com/0.19.0/demo-custom-renderers.html
var actionRenderer = function (instance, td, row, col, prop, value, cellProperties) {
var $button = $('<button>');
$button.html(value)
$(td).empty().append($button); //empty is needed because you are rendering to an existing cell
};
var $container = $("#example1");
$container.handsontable({
/*....*/
columns: [
/*....*/
{data: "action", renderer: actionRenderer}
]
});
为了获得更好的性能,渲染器可以用纯 JavaScript 编写
我找到了我自己问题的答案.. 我在handsontable中使用“渲染器”将单元格渲染为HTML
columns: [
{data: "unique_no"},
{data: "title"},
{data: "subject"},
{data: "year"},
{data: "duration"},
{data: "color"},
{data: "language"},
{data: "synopsis"},
{data: "director"},
{data: "basic_format"},
{data: "created_at"},
{data: "updated_at"},
{data: "action", renderer: "html",readOnly: true}
],
这是我找到它的地方http://handsontable.com/demo/renderers_html.html#dropdown
您可以简单地执行此自定义渲染器。
columns: [
{ data: 'basicFormat', renderer: 'text'},
{ data: 'createdAt', renderer: 'text'},
{ data: 'updatedAt', renderer: 'text'},
{ data: 'action', renderer: 'html'},
]
document.addEventListener("DOMContentLoaded", function() {
var
data = [{}, {},
{
title: createNew()
}
],
container1,
hot1;
container1 = document.getElementById('example1');
hot1 = new Handsontable(container1, {
data: data,
colWidths: 220,
colHeaders: true,
columns: [
{data: "title", renderer: "html"}
]
});
function createNew(){
return "<div class='new'>button</div>"
};
var button = document.querySelector('.new');
button.addEventListener('click', function(){
alert('button')
})
});