Extjs4 GridPanel 的几种样式详解

2024-04-21 15:32

本文主要是介绍Extjs4 GridPanel 的几种样式详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简单表格

排序,显示某列,读取本地数据

//本地数据
            var datas = [
            ['1', 'gao', 'man'], ['2', 'gao', 'man'], ['3', 'gao', 'man']
                    ];
            //创建面板
            Ext.create('Ext.grid.Panel', {
                title: 'easy grid',
                width: 400,
                height: 300,
                renderTo: Ext.getBody(),
                frame: true,
                viewConfig: {
                    forceFit: true,
                    stripRows: true
                },
                store: {//配置数据代理

                    fields: ['id', 'name', 'gender'],
                    proxy: {
                        type: 'memory',
                        data: datas,
                        reader: 'array'   //数据读取器为 数据读取

                    },
                    autoLoad: true   
                },
                columns: [{  //自定义列信息
                    header: 'id',
                    width: 30,
                    dataIndex: 'id',  //绑定fields中得字段
                    sortable: true
                }, {
                    header: 'name',
                    width: 80,
                    dataIndex: 'name',
                    sortable: true
                }, {
                    header: 'gender',
                    width: 80,
                    dataIndex: 'gender',
                    sortable: true
                }

                        ]

            })

表格列:

行号,   bool行转成是否,日期格式化输出(date,top day), number数据类型格式化输出(change,volume),Action列(操作列)

代码;

  Ext.tip.QuickTipManager.init();

            Ext.create('Ext.data.Store', {
                storeId: 'sampleStore',
                fields: [{
                    name: 'framework',
                    type: 'string'
                }, {
                    name: 'rocks',
                    type: 'boolean'
                }, {
                    name: 'volume',
                    type: 'number'
                }, {
                    name: 'topday',
                    type: 'date'
                }, {
                    name: 'change',
                    type: 'number'
                }, {
                    name: 'date',
                    type: 'date'
                }, {
                    name: 'price',
                    type: 'number'
                }

                        ],
                data: {
                    'items': [{
                        "framework": "Ext JS 1",
                        "rocks": true,
                        "symbol": "goog",
                        "date": '2011/04/22',
                        "change": 0.8997,
                        "volume": 3053782,
                        "topday": '04/11/2010',
                        "price": 1000.23

                    }, {
                        "framework": "Ext JS 2",
                        "rocks": true,
                        "symbol": "goog",
                        "date": '2011/04/22',
                        "change": 0.8997,
                        "volume": 3053782,
                        "topday": '04/11/2010',
                        "price": 1000.23

                    }, {
                        "framework": "Ext JS 3",
                        "rocks": true,
                        "symbol": "goog",
                        "date": '2011/04/22',
                        "change": 0.8997,
                        "volume": 3053782,
                        "topday": '04/11/2010',
                        "price": 1000.23

                    }]
                },
                proxy: {
                    type: 'memory',
                    reader: {
                        type: 'json',
                        root: 'items'
                    }
                }
            });

            Ext.create('Ext.grid.Panel', {
                title: 'Boolean Column Demo',
                store: Ext.data.StoreManager.lookup('sampleStore'),
                columns: [
                    Ext.create('Ext.grid.RowNumberer', { text: '行号', width: 40 }),
                {
                    text: 'Framework',
                    dataIndex: 'framework',
                    width: 100
                }, {
                    xtype: 'booleancolumn',
                    text: 'Rocks',
                    trueText: '是',
                    falseText: '否',
                    dataIndex: 'rocks'
                }, {
                    text: 'Date',
                    dataIndex: 'date',
                    xtype: 'datecolumn',
                    format: 'Y年m月d日'
                }, {
                    text: 'Change',
                    dataIndex: 'change',
                    xtype: 'numbercolumn',
                    format: '0.000'
                }, {
                    text: 'Volume',
                    dataIndex: 'volume',
                    xtype: 'numbercolumn',
                    format: '0,000'
                }, {
                    text: 'Top Day',
                    dataIndex: 'topday',
                    xtype: 'datecolumn',
                    format: 'l'
                }, {
                    text: 'Current Price',
                    dataIndex: 'price',
                    renderer: Ext.util.Format.usMoney
                }, {
                    header: '操作',
                    xtype: 'actioncolumn', //操作列
                    width: 100,
                    items: [{
                        icon: 'e.gif', // 编辑图片地址                       

                        tooltip: ‘编辑’,   //鼠标over显示的文字 使用此功能,必须 Ext.tip.QuickTipManager.init();
                        handler: function (grid, rowIndex, colIndex) {
                            var rec = grid.getStore().getAt(rowIndex);
                            alert("Edit " + rec.get('framework'));
                        }
                    }, {
                        icon: 'd.gif',
                        tooltip: 'Delete',
                        handler: function (grid, rowIndex,
                                                colIndex) {
                            var rec = grid.getStore().getAt(rowIndex);
                            alert("Terminate " + rec.get('framework'));
                        }
                    }]

                }, {

                }
                        ],
                height: 200,
                width: 800,
                renderTo: Ext.getBody()
            });

下面这个图是 单击 操作(编辑,删除)按钮触发的回调函数的详细信息.

 

下面演示  自定义 渲染函数

效果:

Ext.tip.QuickTipManager.init();

           function customFunction(value, metadata) {
                if (value > 10) {
                    metadata.style = 'color:red';

                }
                return value;

            }

            Ext.create('Ext.data.Store', {
                storeId: 'sampleStore',
                fields: [ {
                    name: 'custom',
                    type: 'number'
                }

                        ],
                data: {
                    'items': [{
                       
                        "custom": 10

                    }, {
                       
                        "custom": 100

                    }, {
                      
                        "custom": 1000

                    }]
                },
                proxy: {
                    type: 'memory',
                    reader: {
                        type: 'json',
                        root: 'items'
                    }
                }
            });

            Ext.create('Ext.grid.Panel', {
                title: 'Boolean Column Demo',
                store: Ext.data.StoreManager.lookup('sampleStore'),
                columns: [
                    Ext.create('Ext.grid.RowNumberer', { text: '行号', width: 40 }),
                 {
                    text: 'custom',
                    dataIndex: 'custom',
                    renderer: customFunction //调用自定义函数 来渲染
                }
                        ],
                height: 200,
                width: 800,
                renderTo: Ext.getBody()
            });

 

 

选择模式:Selection

选择模式分为三类:

1,行选择(默认)

2.单元格选择

3.复选框选择(checkbox组)

演示单元格选择代码:

只需在上述代码配置节当中,加入

tbar: [
                {
                    text: '取得所选单元格',
                    handler: function () {

                        var cell = grid.getSelectionModel().getCurrentPosition();  //getSelectionModel()获取当前选择模式,getCurrentPosition()获取当前选择的单元格
                        alert(Ext.JSON.encode(cell));
                    }
                }
                ],
selType:'cellmodel'     //设置 选择模式 为   单元格选择

 

行选择:

效果:

tbar: [
                {
                    text: '取得所选行',
                    handler: function () {

                        var rows = grid.getSelectionModel().getSelection(); //getSelection();获取当前选中的记录数组
                        var msg = [];
                        for (var i = 0; i < rows.length; i++) {

                            var row = rows[i];
                            var myDate = new Date(row.get('date')); 
                            msg.push('选中行的Date列:' + myDate.toLocaleString()); //转换时间格式

                        }
                        alert(msg.join('\n'));

                    }
                }
                ],
                selType: 'rowmodel',  //选择模式为 行选择
                simpleSelect: true,    //简单选择功能开启
                multiSelect: true,       // 启用多行选择
                enableKeyNav: true     //启用键盘导航

 

复选框选择:

效果:

tbar: [
               {
                   text: '取得所选行',
                   handler: function () {

                       var rows = grid.getSelectionModel().getSelection(); //getSelection();获取当前选中的记录数组
                       var msg = [];
                       for (var i = 0; i < rows.length; i++) {

                           var row = rows[i];
                           var myDate = new Date(row.get('date'));

                           var s = grid.getStore();            //获取grid的数据源
                           var number = s.indexOf(row) + 1;       //获取行号+1  因为行号从0开始

                           msg.push('选中第' + number + '行的Date列:' + myDate.toLocaleString());

                       }
                       alert(msg.join('\n'));

                   }
               }
               ],
               selType: 'checkboxmodel',  //选择模式为 行选择
               simpleSelect: true,    //简单选择功能开启
               multiSelect: true,       // 启用多行选择
               enableKeyNav: true     //启用键盘导航

表格特性: Feature

表格汇总 Ext.grid.feature.Summary

汇总值计算 根据表格的每一列进行计算,计算方式 有指定的 summaryType决定.默认的有

上图5种.

此例应用 sum和average

Ext.define('TestResult', {
                extend: 'Ext.data.Model',
                fields: ['student', {
                    name: 'mark',
                    type: 'int'
                }]
            });

            var grid = Ext.create('Ext.grid.Panel', {
                width: 200,
                height: 140,
                renderTo: document.body,
                features: [{
                    ftype: 'summary'
                }],
                store: {
                    model: 'TestResult',
                    data: [{
                        student: 'Student 1',
                        mark: 84
                    }, {
                        student: 'Student 2',
                        mark: 72
                    }, {
                        student: 'Student 3',
                        mark: 96
                    }, {
                        student: 'Student 4',
                        mark: 68
                    }]
                },
                columns: [{
                    dataIndex: 'student',
                    text: 'Name',
                    summaryType: 'count',  //进行汇总的列名
                    summaryRenderer: function (value) {
                        grid.getStore()
                        return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
                    }
                }, {
                    dataIndex: 'mark',
                    text: 'Mark',
                    summaryType: 'average'
                }]
            })
            var grid = Ext.create('Ext.grid.Panel', {
                width: 200,
                height: 140,
                renderTo: document.body,
                features: [{
                    ftype: 'summary'
                }],
                store: {
                    model: 'TestResult',
                    data: [{
                        student: 'Student 1',
                        mark: 84
                    }, {
                        student: 'Student 2',
                        mark: 72
                    }, {
                        student: 'Student 3',
                        mark: 96
                    }, {
                        student: 'Student 4',
                        mark: 68
                    }]
                },
                columns: [{
                    dataIndex: 'student',
                    text: 'Name',
                    summaryType: 'count',  //进行汇总的列名
                    summaryRenderer: function (value) {
                      //  grid.getStore()
                        return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
                    }
                }, {
                    dataIndex: 'mark',
                    text: 'Mark',
                    summaryType: 'average',

,
                    summaryRenderer: function (value) {
                        // grid.getStore()
                        return Ext.String.format(' 平均分为:{0}', value);


                }]
            })

 

表格分组:Ext.grid.feature.Grouping

代码:

Ext.define('TestResult', {
              extend: 'Ext.data.Model',
              fields: ['student', 'class', {
                  name: 'mark',
                  type: 'int'
              }]
          });

          var grid = Ext.create('Ext.grid.Panel', {
              width: 400,
              height: 300,
              renderTo: document.body,
              features: [
                 Ext.create('Ext.grid.feature.Grouping',
              {

                  groupByText: '用本字段分组',
                  showGroupsText: '显示分组',
                  groupHeaderTpl: '班级: {name} ({rows.length})',   //分组显示的模板
                  startCollapsed: true  //设置初始分组是不是收起
              })
              ],
              store: {
                  model: 'TestResult',
                  groupField: 'class',
                  data: [{
                      student: 'Student 1',
                      class: '1',
                      mark: 84
                  }, {
                      student: 'Student 2',
                      class: '1',
                      mark: 72
                  }, {
                      student: 'Student 3',
                      class: '2',
                      mark: 96
                  }, {
                      student: 'Student 4',
                      class: '2',
                      mark: 68
                  }]
              },
              columns: [{
                  dataIndex: 'student',
                  text: 'Name',
                  summaryType: 'count',  //进行汇总的列名
                  summaryRenderer: function (value) {
                      grid.getStore()
                      return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
                  }
              }, {
                  dataIndex: 'mark',
                  text: 'Mark',
                  summaryType: 'average'
              },
              { dataIndex: 'class',
                  text: 'class'


              }]
          })

//在不同的列下面点击 “用本字段分组”那么表格就会立即改变分组规则.

 

表格的分组汇总:Ext.grid.feature.GroupSummary

代码只需要把上面的  Grouping 改成 GroupingSummary

 

表格插件: plugin

单元格编辑 Ext.grid.plugin.CellEditing

代码:

var datas = [['gao', Date(1922, 02, 03), 2000]];
           Ext.create('Ext.grid.Panel', {

               title: '演示',
               frame: true,
               renderTo: Ext.getBody(),
               width: 400,
               height: 300,

               store: {
                   fields: ['name', 'birth', 'salary'],
                   data: datas,
                   proxy: {
                       type: 'memory',
                       data: datas,
                       reader: 'array'
                   },
                   autoLoad: true
               },
               plugins: [
                  Ext.create('Ext.grid.plugin.CellEditing', {

                      clicksToEdit: 1
                  })
               ],
               selType: 'cellmodel',
               columns: [Ext.create('Ext.grid.RowNumberer', { text: '行号', width: 40 }),
               {
                   header: '姓名',
                   width: 80,
                   dataIndex: 'name',
                   editor: {//定义字段
                       xtype: 'textfield',
                       allowBlank: false,

                   }
               }
               ,
                {
                    header: '生日',
                    width: 100,
                    dataIndex: 'birth',
                    xtype: 'datecolumn',
                    editor: {//定义字段
                        xtype: 'datefield',
                        format: 'Y-m-d',
                        allowBlank: false
                    }
                }
                ,
                {
                    header: '工资',
                    width: 100,
                    dataIndex: 'salary', xtype: 'numbercolumn',
                    editor: {//定义字段
                        xtype: 'numberfield',
                        format: '$0,000',
                        allowBlank: false
                    }
                }
                ]


           })

表格 行编辑器Ext.grid.plugin.RowEditing

代码只需:把 CellEditing 改成 RowEditing

想要获取修改后的数据,ajax请求服务器,做出响应.

grid.on('edit', onEdit, this);  //添加编辑事件,获取数据
          function onEdit(e) {
              alert(e.record.get('name'));  //get()参数是字段名字.
          }

 

 

gridpanel中的checkbox列 根据数据库值 来初始化是否被选

listeners: {
  load: function(store) {
   var index = 0;
   store.each(function(record) {
    if(record.data.column_name == '1') {  //column_name 替换成你的列名, '1' 替换成你的值
     grid.selModel.selectRow(index,true);
    }
    index++;
   })
  }
}


这篇关于Extjs4 GridPanel 的几种样式详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/923417

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

嵌入式Openharmony系统构建与启动详解

大家好,今天主要给大家分享一下,如何构建Openharmony子系统以及系统的启动过程分解。 第一:OpenHarmony系统构建      首先熟悉一下,构建系统是一种自动化处理工具的集合,通过将源代码文件进行一系列处理,最终生成和用户可以使用的目标文件。这里的目标文件包括静态链接库文件、动态链接库文件、可执行文件、脚本文件、配置文件等。      我们在编写hellowor

LabVIEW FIFO详解

在LabVIEW的FPGA开发中,FIFO(先入先出队列)是常用的数据传输机制。通过配置FIFO的属性,工程师可以在FPGA和主机之间,或不同FPGA VIs之间进行高效的数据传输。根据具体需求,FIFO有多种类型与实现方式,包括目标范围内FIFO(Target-Scoped)、DMA FIFO以及点对点流(Peer-to-Peer)。 FIFO类型 **目标范围FIFO(Target-Sc

019、JOptionPane类的常用静态方法详解

目录 JOptionPane类的常用静态方法详解 1. showInputDialog()方法 1.1基本用法 1.2带有默认值的输入框 1.3带有选项的输入对话框 1.4自定义图标的输入对话框 2. showConfirmDialog()方法 2.1基本用法 2.2自定义按钮和图标 2.3带有自定义组件的确认对话框 3. showMessageDialog()方法 3.1

脏页的标记方式详解

脏页的标记方式 一、引言 在数据库系统中,脏页是指那些被修改过但还未写入磁盘的数据页。为了有效地管理这些脏页并确保数据的一致性,数据库需要对脏页进行标记。了解脏页的标记方式对于理解数据库的内部工作机制和优化性能至关重要。 二、脏页产生的过程 当数据库中的数据被修改时,这些修改首先会在内存中的缓冲池(Buffer Pool)中进行。例如,执行一条 UPDATE 语句修改了某一行数据,对应的缓