`
stworthy
  • 浏览: 521935 次
  • 来自: ...
社区版块
存档分类
最新评论

在easyui中利用DataGrid的行明细区域实现CRUD操作

阅读更多

在easyui中,DataGrid可以通过detailview实现行的展开和收缩,利用这个特性可以实现基本的CRUD操作。

 

 

展开行时,动态加载表单并装载数据,保存或取消表单操作时再收缩行。

首先建立表格基本框架:

<table id="dg" title="My Users" style="width:550px;height:250px" url="get_users.php" toolbar="#toolbar" 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="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newItem()">New</a>  
    <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destroyItem()">Destroy</a>  
</div> 

上面这段代码把DataGrid和工具栏都建好了,不用再编写JS代码。

 

接着,应用detailview并加载表单数据:

$('#dg').datagrid({  
    view: detailview,  
    detailFormatter:function(index,row){  
        return '<div class="ddv"></div>';  
    },  
    onExpandRow: function(index,row){  
        var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');  
        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);  
            }  
        });  
        $('#dg').datagrid('fixDetailRowHeight',index);  
    }  
}); 

表单的定义如下:

    <form method="post">  
        <table class="dv-table" style="width:100%;background:#fafafa;padding:5px;margin-top:5px;">  
            <tbody><tr>  
                <td>First Name</td>  
                <td><input name="firstname" class="easyui-validatebox" required="true"></td>  
                <td>Last Name</td>  
                <td><input name="lastname" class="easyui-validatebox" required="true"></td>  
            </tr>  
            <tr>  
                <td>Phone</td>  
                <td><input name="phone"></td>  
                <td>Email</td>  
                <td><input name="email" class="easyui-validatebox" validtype="email"></td>  
            </tr>  
        </tbody></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>  

保存表单的代码如下:

    function saveItem(index){  
        var row = $('#dg').datagrid('getRows')[index];  
        var url = row.isNewRecord ? 'save_user.php' : 'update_user.php?id='+row.id;  
        $('#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  
                });  
            }  
        });  
    }  

 

详细的说明及演示可以参考:http://www.jeasyui.com/tutorial/app/crud3.php

分享到:
评论
8 楼 329495244 2012-11-19  
引用

    [*]
7 楼 SummitlyLee 2012-08-20  
楼主你能解释一下: ddv.panel({   
9.            border:false,   
10.            cache:true,   
11.            href:'show_form.php?index='+index,   
12.            onLoad:function(){   
13.                $('#dg').datagrid('fixDetailRowHeight',index);   
14.                $('#dg').datagrid('selectRow',index);   
15.                $('#dg').datagrid('getRowDetail',index).find('form').form('load',row);   
16.            }   
17.        });   

href的 连接调用的是什么东西,那边是一个 表单吗? 还是 生成一个字符串发过来了....
我对这个php不熟,我现在用在jsp中,我想了解一下.
6 楼 accphc 2012-02-13  
希望EasyUI提供 datagrid动态绑定列名和数据
参考:
http://blog.csdn.net/qing2005/article/details/6511329

      
5 楼 xieguolun 2011-12-12  
如第一次一条记录都没有,就增加不了记录
4 楼 stevenxie 2011-11-20  
dataGrid工具栏 怎么添加搜索输入框?
3 楼 aolongxue 2011-10-11  
楼主,请教一个问题:
如果我的DataGrid在指定URL后,获取了数据,该数据JSON的格式如{name:"test",sex:"Man",Orders:[{id:1,product:"iphone"},{id:2,product:"iphone2"},{id:3,product:"iphone3"},{id:4,product:"iphone4"}]}

点击Grid的一行,在另一个Grid中加载Orders,为了提高性能,因为其实数据已经存在于主Datagrid的数据中了,只需要将点击的那行的record数据的Orders显示出来就行了

请问,有没有比较简便的方法。。。。
2 楼 njl_041x 2011-09-30  
楼主对easyui研究的很深啊!!!
1 楼 lm4242 2011-06-20  
第一次发现还可以这样用,多谢

相关推荐

Global site tag (gtag.js) - Google Analytics