Bootstrap 实现CRUD示例及代码
https://github.com/wenzhixin/bootstrap-table-examples/blob/master/crud/index.html
<!DOCTYPE html>
<html>
<head>
<title>CRUD Table</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/bootstrap-table/src/bootstrap-table.css">
<link rel="stylesheet" href="../assets/examples.css">
<style>
.update {
color: #333;
margin-right: 5px;
}
.remove {
color: red;
margin-left: 5px;
}
.alert {
padding: 0 14px;
margin-bottom: 0;
display: inline-block;
}
</style>
<script src="../assets/jquery.min.js"></script>
<script src="../assets/bootstrap/js/bootstrap.min.js"></script>
<script src="../assets/bootstrap-table/src/bootstrap-table.js"></script>
<script src="../ga.js"></script>
</head>
<body>
<div class="container">
<h1>CRUD Table</h1>
<p class="toolbar">
<a class="create btn btn-default" href="javascript:">Create Item</a>
<span class="alert"></span>
</p>
<table id="table"
data-show-refresh="true"
data-show-columns="true"
data-search="true"
data-query-params="queryParams"
data-toolbar=".toolbar">
<thead>
<tr>
<th data-field="name">Name</th>
<th data-field="stargazers_count">Stars</th>
<th data-field="forks_count">Forks</th>
<th data-field="description">Description</th>
<th data-field="action"
data-align="center"
data-formatter="actionFormatter"
data-events="actionEvents">Action</th>
</tr>
</thead>
</table>
</div>
<div id="modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group">
<label>Stars</label>
<input type="number" class="form-control" name="stargazers_count" placeholder="Stars">
</div>
<div class="form-group">
<label>Forks</label>
<input type="number" class="form-control" name="forks_count" placeholder="Forks">
</div>
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" name="description" placeholder="Description">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary submit">Submit</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
var API_URL = 'http://' + location.host + ':3001/list/';
var $table = $('#table').bootstrapTable({url: API_URL}),
$modal = $('#modal').modal({show: false}),
$alert = $('.alert').hide();
$(function () {
// create event
$('.create').click(function () {
showModal($(this).text());
});
$modal.find('.submit').click(function () {
var row = {};
$modal.find('input[name]').each(function () {
row[$(this).attr('name')] = $(this).val();
});
$.ajax({
url: API_URL + ($modal.data('id') || ''),
type: $modal.data('id') ? 'put' : 'post',
contentType: 'application/json',
data: JSON.stringify(row),
success: function () {
$modal.modal('hide');
$table.bootstrapTable('refresh');
showAlert(($modal.data('id') ? 'Update' : 'Create') + ' item successful!', 'success');
},
error: function () {
$modal.modal('hide');
showAlert(($modal.data('id') ? 'Update' : 'Create') + ' item error!', 'danger');
}
});
});
});
function queryParams(params) {
return {};
}
function actionFormatter(value) {
return [
'<a class="update" href="javascript:" title="Update Item"><i class="glyphicon glyphicon-edit"></i></a>',
'<a class="remove" href="javascript:" title="Delete Item"><i class="glyphicon glyphicon-remove-circle"></i></a>',
].join('');
}
// update and delete events
window.actionEvents = {
'click .update': function (e, value, row) {
showModal($(this).attr('title'), row);
},
'click .remove': function (e, value, row) {
if (confirm('Are you sure to delete this item?')) {
$.ajax({
url: API_URL + row.id,
type: 'delete',
success: function () {
$table.bootstrapTable('refresh');
showAlert('Delete item successful!', 'success');
},
error: function () {
showAlert('Delete item error!', 'danger');
}
})
}
}
};
function showModal(title, row) {
row = row || {
id: '',
name: '',
stargazers_count: 0,
forks_count: 0,
description: ''
}; // default row value
$modal.data('id', row.id);
$modal.find('.modal-title').text(title);
for (var name in row) {
$modal.find('input[name="' + name + '"]').val(row[name]);
}
$modal.modal('show');
}
function showAlert(title, type) {
$alert.attr('class', 'alert alert-' + type || 'success')
.html('<i class="glyphicon glyphicon-check"></i> ' + title).show();
setTimeout(function () {
$alert.hide();
}, 3000);
}
</script>
</body>
</html>
Bootstrap 实现CRUD示例及代码的更多相关文章
- Bootstrap Table 使用示例及代码
http://issues.wenzhixin.net.cn/bootstrap-table/ <!DOCTYPE html> <html> <head> < ...
- bootstrap 基本页面元素,代码,列表
bootstrap 基本页面元素,代码,列表 <!DOCTYPE html> <html> <head> <title></title> & ...
- [示例] 用代码设置 ListView 颜色 (只适用 Win 平台,无需修改官方源码)
如果可以使用代码随意设置 ListView 的颜色,而不用加载额外的 Style 及修改官方的源码,那该有多好?! 其实 Style 提供了很强了扩充性及可塑性,可以很容易的去操作它. 下面以 Lis ...
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- java框架之SpringBoot(6)-Restful风格的CRUD示例
准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...
- SpringBoot 整合 MyBatis,实现 CRUD 示例
目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...
- jQueryMobile示例页面代码
这是一个jQueryMobile示例页面 示例效果:http://hovertree.com/texiao/jquerymobile/ 可以在手机或者触屏浏览器查看效果. 以下是HTML代码: < ...
- Bootstrap页面布局10 - BS代码
网页中标记代码内容使用code和pre标签 要在HTML中显示标签本来的样子需要转化为实体 在此附上百度的实体字符:http://baike.baidu.com/view/4757776.htm#3 ...
- Bootstrap方法之--排版、代码
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
随机推荐
- HNOI 2016 地图
[题目描述] Hoshizora Rin是个特别好动的少女. 一天Rin来到了一个遥远的都市.这个都市有N个建筑,编号从1到N,其中市中心编号为1,这个都市有M条双向通行的街道,每条街道连接着两个建筑 ...
- poj 2541 Binary Witch
Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS Memory Limit: 65536K Descript ...
- 2015/9/9 Python基础(10):文件和输入输出
文件对象文件对象不仅可以用来访问普通的磁盘文件,而且也可以访问其它任何类型抽象层面上的“文件”.一旦设置了合适的“钩子”,你就可以访问文件类型接口的其它对象,就好像访问的是普通文件一样.文件对象的处理 ...
- wiki文档书写格式
文档基本规范 标题 标题:标明需求的简短语句.或模块名称,目录是由标题生成,一份目录结构清晰的需求文档与标题的划分是密不可分. 正文 正文:有规范格式和生效标志的正式文本,正文包括 文字.表格.图片. ...
- HDU 1465 不容易系列之一 (错排公式+容斥)
题目链接 Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上 ...
- vue实现微信对话
因为项目中需要实现仿微信对话功能,于是抽空实现了下,主要是h5的canvas的把图片和文字绘制到画布上 原文来自我的个人博客:http://lvhww.com/index.php/archives/6 ...
- jQuery mobile 滑动打开面板
一.首先在<head></head>里面引入jQuery库.jQuery mobile库以及jQuery mobile样式 <link rel="stylesh ...
- Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总
In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...
- I题 hdu 1234 开门人和关门人
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1234 开门人和关门人 Time Limit: 2000/1000 MS (Java/Others) ...
- Java多线程学习(一)Java多线程入门
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79640870 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...