Bootstrap table右键功能的实现

最近遇到有一个需求,需要在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" href="libs/bootstrap-table-v1.11.0/bootstrap.min.css">
<link rel="stylesheet" href="libs/bootstrap-table-v1.11.0/bootstrap-table.css">
<link href="libs/jQuery-contextMenu/dist/jquery.contextMenu.css" 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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Bootstrap Table 提供了一个扩展插件 `bootstrap-table-import` 来实现前端 Excel 导入功能。下面是具体实现步骤: 1. 在页面中引入 `xlsx.full.min.js` 库和 `bootstrap-table-import.js` 插件。 ```html <script src="path/to/xlsx.full.min.js"></script> <script src="path/to/bootstrap-table-import.js"></script> ``` 2. 在 Bootstrap Table 的初始化中加入 `importOptions` 配置项。 ```javascript $('#table').bootstrapTable({ // 其他配置项... importOptions: { fileTypes: ['xlsx'], // 可选的文件类型 fieldName: 'excelFile', // 上传文件的字段名 onImportSuccess: function (res) { console.log(res); // 导入成功后的回调函数 }, onImportError: function (err) { console.log(err); // 导入失败后的回调函数 } } }); ``` 这里的 `fileTypes` 表示可选的文件类型,`fieldName` 表示上传文件的字段名,`onImportSuccess` 和 `onImportError` 分别表示导入成功和导入失败后的回调函数。 3. 在页面中添加一个上传文件的按钮,并绑定 `importTable` 方法。 ```html <input type="file" id="input-excel-file"> <button type="button" id="btn-import-table">导入表格</button> ``` ```javascript $('#btn-import-table').click(function () { var file = $('#input-excel-file')[0].files[0]; // 获取上传的文件 $('#table').bootstrapTable('importTable', file); // 调用 importTable 方法导入表格 }); ``` 这里的 `importTable` 方法就是 `bootstrap-table-import` 插件提供的方法,它可以将上传的 Excel 文件解析成 JSON 数据,并将其渲染到 Bootstrap Table 中。 注:为了避免跨域问题,建议将上传文件的接口和页面部署在同一个服务器上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值