ITeye博客

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

stworthy · 2011-06-20 17:42 · jQuery EasyUI · Web前端 · Markdown

在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)

  1. #8 楼 · 329495244 · 2012-11-19 15:56
    
    
  2. #7 楼 · SummitlyLee · 2012-08-20 23: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中,我想了解一下.

  3. #6 楼 · accphc · 2012-02-13 22:49

    希望EasyUI提供 datagrid动态绑定列名和数据 参考: http://blog.csdn.net/qing2005/article/details/6511329

    :!: :!: :!: :!: :!: :!: :!:

  4. #5 楼 · xieguolun · 2011-12-12 14:35

    如第一次一条记录都没有,就增加不了记录

  5. #4 楼 · stevenxie · 2011-11-20 00:25

    dataGrid工具栏 怎么添加搜索输入框?

  6. #3 楼 · aolongxue · 2011-10-11 14:37

    楼主,请教一个问题: 如果我的DataGrid在指定URL后,获取了数据,该数据JSON的格式如{name:"test",sex:"Man",Orders:}

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

    请问,有没有比较简便的方法。。。。

  7. #2 楼 · njl_041x · 2011-09-30 13:42

    楼主对easyui研究的很深啊!!!

  8. #1 楼 · lm4242 · 2011-06-20 19:03

    第一次发现还可以这样用,多谢

评论功能维护中,当前仅展示历史评论。