easyui学习笔记3—在展开行内的增删改操作
这一篇介绍在一个展开行内进行的增删操作,如果一行很长有很多的数据,在一个展开行内进行编辑将更加方便。
1.先看引用的资源
<link rel="stylesheet" href="jquery-easyui-1.3.5/themes/default/easyui.css" />
<link rel="stylesheet" href="jquery-easyui-1.3.5/themes/icon.css" />
<link rel="stylesheet" href="jquery-easyui-1.3.5/demo/demo.css"/>
<script type="text/javascript" src="jquery-easyui-1.3.5/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.5/plugins/datagrid-detailview.js"></script>
这里又要吐槽一下了,在我下载的easyui源文件D:\Serious\phpdev\easyui\jquery-easyui-1.3.5中根本就没有datagrid-detailview.js这个文件,这是作死呢,没办法只能在官网上找这个文件,复制路径http://www.jeasyui.com/easyui/datagrid-detailview.js从IE浏览器中现在这个文件保存到自己目录中。这个不知道是不是因为我下载的是一个免费版本,收费版本里面才有这个文件,很想问一下这个团队的人。
2.在看表格的定义
<table id="dg" title="My User" style="width:700px;height:250px"
url="get_users.php" toolbar="#toolbar" pagination="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<th field="firstname" width="50">First Name</th>
<th field="lastname" width="50">Last Name</th>
<th field="phone" width="50">Phone</th>
<th field="email" width="50">Email</th>
</tr>
</thead>
</table>
<div id="toolbar">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newItem()">New</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyItem()">Destory</a>
</div>
和前面几篇的差不多,这里也给表格定义了工具栏。注意这里没有给表格定义class="easyui-datagrid",不知何解。url="get_users.php"这句可以用来查找数据。
3.看js定义
<script type="text/javascript">
//创建一个匿名方法并立即执行
$(function(){
$("#dg").datagrid({
view:detailview,
detailFormatter:function(index,row){ //返回一个空的div,展开行的时候将展开内容放到这个div中
return "<div class='ddv'></div>";
},
onExpandRow:function(index,row){
var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv'); //在这一行中找到class="ddv"的div
ddv.panel({
border:false,
cache:true,
href:'show_form.php?index='+index, //展开行访问的路径及传递的参数
onLoad:function(){
$("#dg").datagrid('fixDetailRowHeight',index);//固定高度
$('#dg').datagrid('selectRow',index);
$('#dg').datagrid('getRowDetail',index).find('form').form('load',row); //将行的数据加载,这里可能要把列名和show_form.php文件中的name对应起来
}
});
$('#db').datagrid('fixDetailRowHeight',index);//加载之后固定高度
}
});
}); //保存
function saveItem(index){
var row = $("#dg").datagrid('getRows')[index]; //找到当前行
var url = row.isNewRecord?'save_user.php':'update_user.php?id='+row.id;//根据条件设置访问url
$('#dg').datagrid('getRowDetail',index).find('form').form('submit',{ //发送数据
url:url,
onSubmit:function(){
return $(this).form('validate'); //发送请求数据之前验证
},
success:function(data){
data = eval('('+data+')');
data.isNewRecord = false;
$('#dg').datagrid('collapseRow',index); //收缩
$('#dg').datagrid('updateRow',{ //用请求数据更新编辑的哪一行的数据
index:index,
row:data
});
}
})
} //取消
function cancelItem(index){
var row = $('#dg').datagrid('getRows')[index];
if(row.isNewRecord){ //如果是新增直接删除这一行,包括展开内容,否则是更新则收缩
$('#dg').datagrid('deleteRow',index);
}
else{
$('#dg').datagrid('collapseRow',index);
}
} //删除
function destroyItem(){
var row = $('#dg').datagrid('getSelected');
if(row){
$.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){
if(r){
var index = $('#dg').datagrid('getRowIndex',row); //为destory_user.php传递参数id
$.post('destory_user.php',{id:row.id},function(){
$('#dg').datagrid('deleteRow',index);
}); }
});
}
} //点击新加
function newItem(index){
$('#dg').datagrid('appendRow',{isNewRecord:true});
var index = $('#dg').datagrid('getRows').length-1;
$('#dg').datagrid('expandRow',index);
$('#dg').datagrid('selecteRow',index);
}
</script>
这个js有点复杂,我在每个方法中都注释了。我在这里犯了一个错误将 $('#dg').datagrid('getRows')错误的写成了 $("#db").datagrid('getRows') 会报错TypeError: e is undefined,笔误。
4.最后还有一个文件show_form.php如下:
<form method="post">
<table class="dv-table" style="width:100%;border:1px solid #ccc;background:#fafafa;padding:5px;margin-top:5px;">
<tr>
<td>First Name</td>
<td><input name="firstname" class="easyui-validatebox" required="true"></input></td>
<td>Last Name</td>
<td><input name="lastname" class="easyui-validatebox" required="true"></input></td>
</tr>
<tr>
<td>Phone</td>
<td><input name="phone"></input></td>
<td>Email</td>
<td><input name="email" class="easyui-validatebox" validType="email"></input></td>
</tr>
</table>
<div style="padding:5px 0;text-align:right;padding-right:30px">
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="saveItem(<?php echo $_REQUEST['index'];?>)">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancelItem(<?php echo $_REQUEST['index'];?>)">Cancel</a>
</div>
</form>
这是一个php文件,可以看到使用<?php echo $_REQUEST['index'];?>接受参数。
好了就写到这里。
easyui学习笔记3—在展开行内的增删改操作的更多相关文章
- Mysql学习笔记(三)对表数据的增删改查。
正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...
- Jquery easyui开启行编辑模式增删改操作
Jquery easyui开启行编辑模式增删改操作 Jquery easyui开启行编辑模式增删改操作先上图 Html代码: <table id="dd"> </ ...
- [转]Jquery easyui开启行编辑模式增删改操作
本文转自:http://www.cnblogs.com/nyzhai/archive/2013/05/14/3077152.html Jquery easyui开启行编辑模式增删改操作先上图 Html ...
- 【EF6学习笔记】(二)操练 CRUD 增删改查
本篇原文链接: Implementing Basic CRUD Functionality 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根 ...
- EF6 学习笔记(二):操练 CRUD 增删改查
EF6学习笔记总目录 ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 接上篇: EF6 学习笔记(一):Code First 方式生成数据库及初始化数据库实际操作 本篇原文链接: I ...
- mongodb的学习笔记一(集合和文档的增删改查)
1数据库的增删改查 一.增加一个数据库: use blog-----切换到指定的数据库,如果数据库不存在,则自动创建该数据库(新建的数据库,如果没有存储对应的集合,是不会显示出来的) 二.删除一个数据 ...
- Mysql学习笔记(二)对表结构的增删改查
有将近一个星期都没有更新mysql了.相反linux的东西倒是学习不少.可能我个人情感上对linux更感兴趣一点.但mysql我也不烦,只是一旦将精力投入到了一样事情上去,就很难将精力分散去搞其他的东 ...
- oracle学习笔记系列------oracle 基本操作之表的增删改查
--创建一个表 CREATE TABLE employee_souvc( id ), name ), gender ), birth DATE, salary ,), job ), deptno ) ...
- EasyUI----DataGrid行明细增删改操作
http://blog.csdn.net/huchiwei/article/details/7787947 本文实现的是EasyUI-DataGrid行明细的增删改操作.具体参考来自以下文章: 官 ...
随机推荐
- KOA 与 CO 实现浅析
KOA 与 CO 的实现都非常的短小精悍,只需要花费很短的时间就可以将源代码通读一遍.以下是一些浅要的分析. 如何用 node 实现一个 web 服务器 既然 KOA 实现了 web 服务器,那我们就 ...
- SQL Cookbook—数字、日期
1.计算不包含最大值和最小值的均值2.把字母数字串转换为数值3.更改累计和中的值–显示存款或取款后的值4.加减日.月.年5.计算两个日期之间的天数6.确定两个日期之间的工作日数目表EMP中,计算BLA ...
- Spark中自定义累加器Accumulator
1. 自定义累加器 自定义累加器需要继承AccumulatorParam,实现addInPlace和zero方法. 例1:实现Long类型的累加器 object LongAccumulatorPara ...
- IT自由职业者的第一个月(下)——为什么放弃5年嵌入式驱动开发转到WEB开发?
如果单从兴趣来看,其实我对Linux内核,Android中间件的兴趣要高于WEB,何况还有这么多年的经验积累,何必从头探索一个新的技术方向呢? 这里面原因是很多的,最核心的大概是以下4 ...
- DIV固定宽度和动态拉伸混合水平排列
1.效果图 2.源代码 html <h2>1.头部固定,尾部拉伸</h2> <div class="container" id="div1& ...
- JavaScript(JS)计算某年某月的天数(月末)
方法1 /** * 获取某年月的天数 * @param year 年 * @param month 月(0-11) * @returns {number} 天数 */ var getDaysOfMon ...
- [转]Shared——React Native与原生关系理解与对比
零.关系理解 这个是我对RN和原生关系的理解.简单解释下这个图. RN js编写完业务代码后,通过react-native bundle命令,将代码分别编译成一个index.ios.bundle和in ...
- java 输出菱形
package com.demo01; public class Triangle { /** * @param args */ /* * 第一步:规定输出的行数 * 第二步:输出空格 再输出一个星, ...
- jquery插件一直报错:xx is not a function
当然像js文件未引用或者js插件使用方法不对这样的解决办法想必大家都已经试过了. 那么在放弃前请最后看一下是不是引用了两个jquery文件. 引用了两个jquery文件! 引用了两个jquery文件! ...
- Unity --- 关节组件
一.简介 Unity提供了下面的关节组件:铰链关节(Hinge Joint).固定关节(Fixed Joint).弹簧关节(Spring Joint).角色关节(Character Joint).可配 ...