ExtJs之列表常用CRUD
前端代码:
Ext.onReady(function(){
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [{name: 'id',
type: 'int',
useNull: true
}, 'email', 'first', 'last'],
validations: [{ type: 'length',
field: 'email',
min: 1
}, {type: 'length',
field: 'first',
min: 1
}, {type: 'length',
field: 'last',
min: 1
}]
});
//构造store
var store = Ext.create('Ext.data.Store', {
//autoLoad: true,
autoSync: true,
model: 'Person',
proxy: {
type: 'ajax',
api: {
read: '<%=basePath %>/AdminServlet?param=read',//查询
create: '<%=basePath %>/AdminServlet?param=add',//创建
update: '<%=basePath %>/AdminServlet?param=update',//更新
destroy: '<%=basePath %>/AdminServlet?param=deletes'//删除
},
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json'
}
},
listeners: {
write: function(store, operation){
var record = operation.getRecords()[0],
name = Ext.String.capitalize(operation.action),
verb;
if (name == 'Destroy') {
record = operation.records[0];
verb = 'Destroyed';
} else {
verb = name + 'd';
}
Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
}
}
});
store.load({
params:{
start:0,
limit:20
}
});
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
id:'edit',
listeners: {
edit:function(rowEditing,context){
context.record.commit();
store.reload();//提交后重新加载 获取新数据 包括自动生成的id
},
cancelEdit: function(rowEditing, context) {
// Canceling editing of a locally added, unsaved record: remove it
if (context.record.phantom) {
store.remove(context.record);
}
}
}
});
//创建 panel
var grid = Ext.create('Ext.grid.Panel', {
renderTo: document.body,
plugins: [rowEditing],
width: 400,
height: 300,
frame: true,
title: 'Users',
store: store,
iconCls: 'icon-user',
columns: [{
text: 'ID',
width: 40,
sortable: true,
dataIndex: 'id'
}, {
text: 'Email',
flex: 1,
sortable: true,
dataIndex: 'email',
field: {
xtype: 'textfield'
}
}, {
header: 'First',
width: 80,
sortable: true,
dataIndex: 'first',
field: {
xtype: 'textfield'
}
}, {
text: 'Last',
width: 80,
sortable: true,
dataIndex: 'last',
field: {
xtype: 'textfield'
}
}],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function(){
// empty record
store.insert(0, new Person());//从指定索引处开始插入 插入Model实例 并触发add事件
rowEditing.startEdit(0, 0);//开始编辑,并显示编辑器
}
}, '-', {
itemId: 'delete',
text: 'Delete',
iconCls: 'icon-delete',
disabled: true,
handler: function(){
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
}]
}]
});
grid.getSelectionModel().on('selectionchange', function(selModel, selections){
grid.down('#delete').setDisabled(selections.length === 0);
});
});
后台代码:
/**
* @author Lucare(fcs)
*
* 2015年1月9日
*/
public class AdminServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con;
private List<Admin> admins;
private int count;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//根据参数param分发请求
String param = request.getParameter("param");
System.out.println(param);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ext","root","root");
Gson gson = new Gson();
response.setCharacterEncoding("UTF-8");
if(param.equals("read")){
String start = request.getParameter("start");
String limit = request.getParameter("limit");
admins = findAll(start, limit);
count = totalCount();
response.getWriter().print("{total:"+count+",data:"+gson.toJson(admins)+"}");
}else if(param.equals("add")){
//extjs 以流的形式传递数据(json类型)
String msg = request.getReader().readLine();
Admin admin = gson.fromJson(msg, Admin.class);
add(admin);
}else if(param.equals("update")){
String msg = request.getReader().readLine();
Admin admin = gson.fromJson(msg, Admin.class);
update(admin);
}else if(param.equals("deletes")){
String msg = request.getReader().readLine();
Admin admin = gson.fromJson(msg, Admin.class);
del(admin);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
closeCon();
}
}
public List<Admin> findAll(String start,String limit){
List<Admin> adminlist = new ArrayList<Admin>();
try {
PreparedStatement ps = con.prepareStatement("select * from admins limit "+start+","+limit);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Admin admin = new Admin();
admin.setId(rs.getInt(1));
admin.setEmail(rs.getString(2));;
admin.setFirst(rs.getString(3));
admin.setLast(rs.getString(4));
adminlist.add(admin);
}
} catch (SQLException e) {
e.printStackTrace();
}
return adminlist;
}
public void add(Admin admin){
try {
PreparedStatement ps = con.prepareStatement("insert into admins values(null,?,?,?)");
ps.setString(1, admin.getEmail());
ps.setString(2, admin.getFirst());
ps.setString(3, admin.getLast());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void del(Admin admin){
try {
PreparedStatement ps = con.prepareStatement("delete from admins where id=?");
ps.setInt(1, admin.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(Admin admin){
try {
PreparedStatement ps = con.prepareStatement("update admins set email=?,first=?,last=? where id=?");
ps.setString(1,admin.getEmail());
ps.setString(2,admin.getFirst());
ps.setString(3,admin.getLast());
ps.setInt(4, admin.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public int totalCount(){
int total = 0;
try {
PreparedStatement ps = con.prepareStatement("select count(*) from admins");
ResultSet rs = ps.executeQuery();
if(rs.next()){
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void closeCon(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
时间过得好快,虽然学了几天ExtJs,可是后来没有用上,在这里还是总结一下基本用法。ExtJs很强大,远不止我总结的这些,要想学好并熟练运用还是要花费一番功夫的。不过建议初学者学习时要针对版本,每个版本的差异还是比较大的。
ExtJs之列表常用CRUD的更多相关文章
- redis学习-散列表常用命令(hash)
redis学习-散列表常用命令(hash) hset,hmset:给指定散列表插入一个或者多个键值对 hget,hmget:获取指定散列表一个或者多个键值对的值 hgetall:获取所欲哦键值以及 ...
- python列表常用内建方法
python列表常用内建方法: abc = ['a',1,3,'a'] #abc.pop(1) #删除索引1的值.结果['a', 3] #abc.append([123]) #结果:['a', 1, ...
- Python语言学习:列表常用的方法
python 列表常用的方法 1.append( ):用于在列表末尾添加新的对象 list.appent(obj) #obj:添加到列表末尾的对象 #!/usr/bin/python aList = ...
- python3 字符串与列表常用功能
一.字符串常用功能 1. capitalize(),将字符串的首字母变成大写,其余全部置为小写:如果字符串中有多个单词,也只是将第一个单词的首字母置为大写:例: >>> name = ...
- python基础之列表常用操作及知识点小结
列表(list) List(列表) 是 Python 中使用最频繁的数据类型.列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(所谓嵌套).列表用[ ]标识,是pyth ...
- Python自动化开发(三):循环次数控制、常用数据类型、字符串格式化、列表常用操作、列表的后续操作
计数器的作用可以在死循环中,符合条件的情况下做自动退出中断 #!/usr/bin/env python # _*_ coding: utf-8 _*_ # @Time : 2017/3/14 11:2 ...
- Python_列表常用操作
%d 数字 %f 浮点 %s 字符串 字符串常用功能: .strip() 默认去掉字符串两边空格#或者在括号里注明去除什么 查看列表方法:dir(列表名) .append(元素): ...
- Extjs 项目中常用的小技巧,也许你用得着(3)
几天没写了,接着继续, 1.怎么获取表单是否验证通过: form.isValid()//通过验证为true 2.怎样隐藏列,并可勾选: hidden: true, 如果是动态隐藏的话: grid.ge ...
- list列表常用操作
1.创建列表.只要把逗号分隔的不同的数据项使用方括号括起来即可 List = ['wade','james','bosh','haslem'] 2.使用 range() 创建数字列表 numbers ...
随机推荐
- LightOJ 1009 二分图染色+BFS/种类并查集
题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...
- [Luogu 1168] 中位数
中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊.(笑) 功能十分简单的Treap即能满足需求--只需要插入与查找第大的功能. 插入第i个数时,如果i是奇数,随即询问当前排名第(i+ ...
- 【bzoj】1717 [Usaco2006 Dec]Milk Patterns 产奶的模式
[算法]后缀数组 [题解]后缀数组 由于m太大,先离散化. 然后处理SA和LCP. 最后用单调队列处理即可. 注意实际上队列头尾长度限制是K-1. 删队尾不要删过头 i≥K才能开始统计答案. #inc ...
- 【NOI】2004 郁闷的出纳员
[算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...
- 用 Docker 来构建 Jumpserver
说明: 项目从 [ Jumpserver 官方 ] fork 而来. 主要更新: OS: ubuntu:18.04 优化了 Dockerfile Jumpserver 版本: 1.4.0 redis ...
- adb端口被占用解决
解决ADB端口占用问题 方式一5037为adb默认端口,若5037端口被占用,查看占用端口的进程PIDC:\Users\wwx229495>netstat -aon|findstr 5037 ...
- spring mvc 提供的几个常用的扩展点
转载 :http://blog.csdn.net/gufachongyang02/article/details/43836105 这是spring3 mvc的核心流程图: SpirngMVC的第 ...
- 结合BeautyEye开源UI框架实现的较美观的Java桌面程序
BeautyJavaSwingRobot 结合BeautyEye开源UI框架实现的较美观的Java桌面程序,主要功能就是图灵机器人和一个2345网站万年历的抓取.... 挺简单而且实用的一个项目,实现 ...
- python中的argparse模块
argparse干什么用的? 答:参数设置,比如python demo.py -h 诸如此类的. 开始学习这个模块: parser = argparse.ArgumentParser() #使用这个模 ...
- yocto 离线编译
使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...