实战Flask+BootstrapTable最简动态表头及数据(ajax方法)

话不多说,有图有源码

1.实现原理:通过ajax从后端获取表头及数据

前端页面内容

复制代码
<!DOCTYPE html>
{% from "common/_macro.html" import static %}
<html>
<meta charset="utf-8">
<head>
<!-- 引入bootstrap样式 -->
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" />
<!-- 引入bootstrap-table样式 -->
<link rel="stylesheet" href="/static/bootstrap-table-develop/bootstrap-table.min.css" />
<!-- layer -->
<link rel="stylesheet" href="/static/layer/2.4/skin/layer.css" />
<!-- jquery -->
<script type="text/javascript" src="/static/js/jquery-2.2.0.min.js" ></script>
<!-- bootstrap -->
<script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js" ></script>
<!-- bootstrap-table -->
<script type="text/javascript" src="/static/bootstrap-table-develop/bootstrap-table.min.js" ></script>
<!-- 引入中文语言包 -->
<script type="text/javascript" src="/static/bootstrap-table-develop/locale/bootstrap-table-zh-CN.min.js" ></script>
<!-- layer -->
<script type="text/javascript" src="/static/layer/2.4/layer.js" ></script>

</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content ">
		<div class="col-sm-12">
			<div class="ibox">
				<div class="ibox-body">
					<table id="exampleTable"></table>
					<button type="button" id="butSave" class="btn btn-primary col-sm-1 col-sm-offset-5" onclick="save()">保存</button>
				</div>
			</div>
		</div>
	</div>
	<div ></div>
</body>
</html>
<script>
$(function() {
	var $table = $('#exampleTable');
	var loadData = [];
	var tableColumns = [];
	initTable();
	ajaxTable('/helloeditrowssave/ajaxlist');

	function initTable() {
		$table.bootstrapTable({
			toolbar: '#toolbar',
			clickEdit: true,
			showToggle: true,
			pagination: true,       //显示分页条
			showColumns: true,
			showPaginationSwitch: true,     //显示切换分页按钮
			showRefresh: true,      //显示刷新按钮
			columns: tableColumns
		});
	}

    function ajaxTable(url){
		//debugger;
		$.ajax({
			url: url,//"/helloeditrowssave/list",
			type: "GET",
			dataType: "json",
			success: function(rs){
				console.log(rs)
				loadData = rs.data.rows;
				tableColumns = rs.data.columns;
				$table.bootstrapTable('refreshOptions',
				{
					columns: tableColumns,
					data: loadData,
				});
			},
			error: function(rs){
				console.log(rs)
			}
		});
	}
});

后端代码

复制代码
@app.route('/helloeditrowssave/ajaxlist', methods=['GET','POST'])
def helloeditrowssave_ajaxlist():
    # 将后端数据转换为JSON格式
    columns = [{'checkbox': True}, {'field': 'id','title': 'Item ID'}, {'field': 'name1','title': 'Item Name'}, {'field': 'price','title': 'Item Price'}, ]
    datas = [{ "id": 11, "name1": "gtj", "price": "¥11" },
             { "id": 22, "name1": "Ewangda", "price": "¥22" },
             { "id": 31, "name1": "小柘服务", "price": "¥32" }] #[{'customerName':"gtj"},{'customerName':"gtj1"}] #[{'id': user.id, 'name': user.name, 'email': user.email} for user in users]
    json_data = jsonify(datas)
    data = {
        'columns' : columns,
        'rows' : datas
    }
    totalNum = 3
    return restful.success(data=data)

2.结果图片

相关推荐
冷雨夜中漫步14 分钟前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴34 分钟前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再37 分钟前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手2 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934732 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy2 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威4 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ4 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha4 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy5 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法