zoukankan      html  css  js  c++  java
  • jquery easyui DataGrid

    Easyui Demo网站:

    http://www.jeasyui.com/  英文

    http://www.phptogether.com/juidoc/  中文

    datagrip的基本属性方法:http://www.phptogether.com/juidoc/datagrid.html

    推荐:http://www.cnblogs.com/Philoo/tag/jQuery/

    冻结列 (2013-01-17 )

    复制代码
    $('#tbList').datagrid({ pagination: true,
                frozenColumns: [[
                { field: 'BId',checkbox:'true',30},
                { field: 'BNo', title: '牌号',  100 },
                { field: 'FNo', title: '班号',  100 }
              ]], 
           fitColumns:false //禁止自适应宽度、可以水平滚动
            });
    复制代码

    去掉分页(2013-02-20)

    $('#tbList').datagrid({pagination: true});

    更改为

    $('#tbList').datagrid();

    $('#tbList').datagrid({pagination: false});

    注意:同时需要设置table的高度,而且不能为auto

    复选框以及单选(2013-02-20)

    复制代码
    <table id="tbList" style="height: 330px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit" 
    checkbox="true" idfield="Id" url="@Url.Action("ListData")"> <thead> <tr>   <th field="Id" checkbox="true" width="150">   </th>
        </tr> </thead> </table>
    复制代码

    变为单选(添加singleSelect="true"  )

    <table id="tbList" style="height: 330px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit" singleSelect="true" checkbox="true"  idfield="Id" url="@Url.Action("ListData")">

    加载数据后默认全选:

     $(document).ready(function () {
            $('#tbList').datagrid({ 
                onLoadSuccess: function (data) {
                    $('#tbList').datagrid('selectAll');
                } 
            });

    获取行数(2013-02-20)

    $('#tbList').datagrid("getRows").length;

    隐藏列(2013-02-20)

    <th field="Dept" width="100" hidden="true">名称</th>

     清空原有数据(2013-02-20)

    方法1:

                var item = $('#filegrid').datagrid('getRows');
                if (item) {
                    for (var i = item.length - 1; i >= 0; i--) {
                        var index = $('#filegrid').datagrid('getRowIndex', item[i]);
                        $('#filegrid').datagrid('deleteRow', index);
                    }
                }

    方法2:(测试过)

    $('#filegrid').datagrid('loadData', { total: 0, rows: [] });

    解析:loadData:载入本地数据,旧记录将被移除。

    事件(2013-02-20):

     $('#tbList').datagrid({ onClickRow: function () {//代码  } });

    datagrip单击行的时候,将单选按钮设置为选中(2013-02-20):

    复制代码
    <script type="text/javascript">
        var List = {};
        List.RadioFormatter = function (value, rec, index) {
            return "<input id='radio_id' name='radio_name' type='radio' value='" + rec.Id + "'/>";
        };
    
     $(document).ready( function(){ //呈现列表数据
      $('#tbList').datagrid({ onClickRow:
                function () {
                    //单击行的时候,将单选按钮设置为选中
                    var id = $('#tbList').datagrid("getSelected");
                    $("input[name='name']").each(function () {
                        if ($(this).val() == id.Id) {
                            $(this).attr("checked", true);
                        }
                    });
                }
            });
    });
    </script>
    <table id="tbList" style="height: 300px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit"
             singleSelect="true" checkbox="true" idfield="Id" url="@Url.Action("ListData")">
                <thead>
                    <tr>
                        <th field="Id" width="30" formatter="PickupList.RadioFormatter">
                        </th>
                    </tr>
                </thead>
            </table>
    复制代码

    table中td的时间格式问题(2013-03-14)

    1.页面

     <th field="Test" formatter="Common.TimeFormatter" width="50" ></th>

    2.js

    复制代码
    var Common = {
        //EasyUI用DataGrid用日期格式化
        TimeFormatter: function (value, rec, index) {
            if (value == undefined) {
                return "";
            }
            /*json格式时间转js时间格式*/
            value = value.substr(1, value.length - 2);
            var obj = eval('(' + "{Date: new " + value + "}" + ')');
            var dateValue = obj["Date"];
            if (dateValue.getFullYear() < 1900) {
                return "";
            }
            var val = dateValue.format("yyyy-mm-dd HH:MM");//控制格式
            return val.substr(11, 5);
        }
    
    };
    复制代码

    table中td内容太长自动换行(2013-03-18)

     添加属性 nowrap="false"

    行和复选框的分离(2013-03-25)

    方法一:(1.3版本才能用)

    checkOnSelect="false" selectOnCheck="false"

    注意:当使用$("#tbList").datagrid("getSelections");时候,只有行被选中,才能取到该行。一般情况,选中行时候,行为黄色背景。

      eg.<table checkOnSelect="false"> </table>

    复制代码
    var selected = $("#tbList").datagrid("getSelections");
            if (selected.length == 0) {
                alert("请选择!");
                return;
            }
    
            var idString = "";
            $.each(selected, function (index, item) {
                idString += item.Id + ",";
            });
    复制代码

     方法二(1.3版本之前的解决方法)

    复制代码
    var IsCheckFlag = true;
            $('#tbList').datagrid({
                pagination: true,
                onClickCell: function (rowIndex, field, value) {
                    IsCheckFlag = false;
                },
                onSelect: function (rowIndex, rowData) {
                    if (!IsCheckFlag) {
                        IsCheckFlag = true;
                        $("#tbList").datagrid("unselectRow", rowIndex);
                    }
                },
                onUnselect: function (rowIndex, rowData) {
                    if (!IsCheckFlag) {
                        IsCheckFlag = true;
                        $("#tbList").datagrid("selectRow", rowIndex);
                    }
                }
            });
    复制代码

    设置数据列表的样式

    复制代码
     $(document).ready(function () {
            //呈现列表数据
            $('#tbList').datagrid({ pagination: true,
                rowStyler: function(index,row){
                        if (row.ID< 10) {//那么数据的id字段小于10的,将显示为灰色字体
                            return 'color:#999;';//和一般的样式写法一样
                        }
                    }
                });
        });
    复制代码

     条件查询

    复选框的bug:使用参数查询时候,在查询之前选中的选项 ,在查询之后,使用getSelections方法去获取,依旧存在该选项

    解决方案:通过unselectAll在查询之前清空复选框即可

    $("#btnSearch").click(function () {
                $('#tbList').datagrid("unselectAll");
                $('#tbList').datagrid({ queryParams: { SearchName: $("#SearchName").val() } });
            });
  • 相关阅读:
    SSH深度历险(五) 深入浅出-----IOC AND AOP
    Hbuilder X下载及安装教程
    如何用Prometheus监控十万container的Kubernetes集群
    使用并部署Flutter Web的步骤实例
    回顾 Android 11 中的存储机制更新
    移动端UI一致性解决方案
    使用 tail 结合 grep 查找日志关键字并高亮及显示所在行上下文
    Nginx PHP 报504 Gateway time-out错误的解决方法
    SPSS 24 安装详细教程及下载
    CoRL 2020奖项公布,斯坦福获最佳论文奖,华为等摘得最佳系统论文奖
  • 原文地址:https://www.cnblogs.com/zzw1986/p/4858011.html
Copyright © 2011-2022 走看看