最近遇到有一个需求,需要在Bootstrap table上使用右键。网上搜了半天没有找到,最后发现Bootstrap table不支持右键(官方文档给出的答案 https://github.com/wenzhixin/bootstrap-table/issues/241 )。
本文介绍使用contextMenu插件实现Bootstrap table右键功能。
代码(test.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" rel="nofollow noopener noreferrer" href="libs/bootstrap-table-v1.11.0/bootstrap.min.css" rel="external nofollow" >
<link rel="stylesheet" rel="nofollow noopener noreferrer" href="libs/bootstrap-table-v1.11.0/bootstrap-table.css" rel="external nofollow" >
<link rel="nofollow noopener noreferrer" href="libs/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="external nofollow" rel="stylesheet"/>
<script src="libs/jQuery/jquery-1.8.3.min.js"></script>
<script src="libs/bootstrap-table-v1.11.0/bootstrap.min.js"></script>
<script src="libs/bootstrap-table-v1.11.0/bootstrap-table.js"></script>
<script src="libs/bootstrap-table-v1.11.0/bootstrap-table-zh-CN.js"></script>
<script src="libs/jQuery-contextMenu/dist/jquery.contextMenu.js"></script>
</head>
<body>
<table id="item_table"></table>
<script>
$('#item_table').bootstrapTable({
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}],
data: [{
id: 1,
name: 'Item 1',
price: '$1'
}, {
id: 2,
name: 'Item 2',
price: '$2'
}]
});
$.contextMenu({
// define which elements trigger this menu
selector: "#item_table td",
// define the elements of the menu
items: {
foo: {name: "Foo", callback: function(key, opt){ alert("Foo!"); }},
bar: {name: "Bar", callback: function(key, opt){ alert("Bar!") }}
}
// there's more, have a look at the demos and docs...
});
</script>
</body>
</html>
效果图:
关于contextMenu的使用,可以参考网页中右键功能的实现— contextMenu的使用 。
关于Bootstrap table的使用,可以参考官方文档Bootstrap table。
本文Bootstrap table右键功能实现方法到此结束。这一生中,我们固执地以自己喜欢的方式存在着,并渴望与自己同行的人,也能按照这种方式来演绎生活的节奏。于是,为了生计、爱情和梦想,我们在无数次的摩擦与碰撞中,慢慢地改变着并湮没了原来的自己。来到这个世界上,我们谁都没有错,就是因为固守或者改变得太多,我们错过了很多烟花般的美丽。小编再次感谢大家对我们的支持!




