这一篇介绍在一个展开行内进行的增删操作,如果一行很长有很多的数据,在一个展开行内进行编辑将更加方便。

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—在展开行内的增删改操作的更多相关文章

  1. Mysql学习笔记(三)对表数据的增删改查。

    正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...

  2. Jquery easyui开启行编辑模式增删改操作

    Jquery easyui开启行编辑模式增删改操作 Jquery easyui开启行编辑模式增删改操作先上图 Html代码: <table id="dd"> </ ...

  3. [转]Jquery easyui开启行编辑模式增删改操作

    本文转自:http://www.cnblogs.com/nyzhai/archive/2013/05/14/3077152.html Jquery easyui开启行编辑模式增删改操作先上图 Html ...

  4. 【EF6学习笔记】(二)操练 CRUD 增删改查

    本篇原文链接: Implementing Basic CRUD Functionality 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根 ...

  5. EF6 学习笔记(二):操练 CRUD 增删改查

    EF6学习笔记总目录 ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 接上篇: EF6 学习笔记(一):Code First 方式生成数据库及初始化数据库实际操作 本篇原文链接: I ...

  6. mongodb的学习笔记一(集合和文档的增删改查)

    1数据库的增删改查 一.增加一个数据库: use blog-----切换到指定的数据库,如果数据库不存在,则自动创建该数据库(新建的数据库,如果没有存储对应的集合,是不会显示出来的) 二.删除一个数据 ...

  7. Mysql学习笔记(二)对表结构的增删改查

    有将近一个星期都没有更新mysql了.相反linux的东西倒是学习不少.可能我个人情感上对linux更感兴趣一点.但mysql我也不烦,只是一旦将精力投入到了一样事情上去,就很难将精力分散去搞其他的东 ...

  8. oracle学习笔记系列------oracle 基本操作之表的增删改查

    --创建一个表 CREATE TABLE employee_souvc( id ), name ), gender ), birth DATE, salary ,), job ), deptno ) ...

  9. EasyUI----DataGrid行明细增删改操作

    http://blog.csdn.net/huchiwei/article/details/7787947   本文实现的是EasyUI-DataGrid行明细的增删改操作.具体参考来自以下文章: 官 ...

随机推荐

  1. selenium+python(数据驱动测试)

    自动化领域的两种驱动,对象驱动与数据驱动 数据驱动:测试数据的改变引起执行结果的改变 叫 数据驱动 关键字驱动:测试对象名字的改变起引起测试结果的改变 叫 关键字驱动 1 .读取文件参数化   以百度 ...

  2. 【随笔】win7下安装Apache服务器

    1.首先下载64位的apache服务器httpd-2.4.10-win64.zip 2.解压,可以是默认路径,也可以自定义路径,我这里是E:/ 3.打开E:/Apache24/conf文件夹 4.随便 ...

  3. UML 依赖\泛化\关联\实现\聚合\组合的 Java实现

    在类图中,类与类之间的关系主要有一下几种: 泛化关系:(就是继承) public class Employee { } public class SaleEmployee extends Employ ...

  4. python风味之大杂烩

    判断语句复制 >>> a = 3 >>> b = 3 if a == 2 else 4 >>> b 4 >>>

  5. Android OpenGL教程-第四课【转】

    第四课 旋转: 在这一课里,我将教会你如何旋转三角形和四边形.左图中的三角形沿Y轴旋转,四边形沿着X轴旋转. 我们增加两个变量来控制这两个对象的旋转.这两个变量加在程序的开始处其他变量的后面.它们是浮 ...

  6. hosts文件配置参数介绍

    hosts文件配置参数介绍 1, ansible_ssh_host : 指定主机别名对应的真实 IP,如:100 ansible_ssh_host=192.168.1.100,随后连接该主机无须指定完 ...

  7. 「Sdchr 的邀请赛」题解

    骗个访问量.. A:取石子 将点 x 与点 x / prime 连边,那么这个图可以由指数之和的奇偶性来划分成一个二分图. 接下来考虑推广阶梯 NIM (或者这原本就是阶梯 NIM ?),必胜当且仅当 ...

  8. awk中引用变量使用单引号''

    举例如下 who命令输出第一列 (1)第一种情况不使用引号 # i=1;who | awk '{print $${i}}' 输出如下: awk: {print $${i}} awk:          ...

  9. JavaScript的六种数据类型

      JavaScript数据类型有六种:number.string.boolean.null.undefined.object

  10. [shell]管理 Sphinx 启动|停止|重新生成索引的脚本

    对于启动sphinx的服务,可以直接输入如下命令 /usr/bin/searchd -c /etc/sphinx/sphinx.conf <!-- /usr/local/bin/searchd  ...