ExtJS_FAQ_Grid#Behavior

本文介绍了ExtJS Grid中的一些高级用法,包括如何在单元格内创建链接、为特定单元格添加点击事件、动态调整列显示及添加操作列等内容,并提供了具体的实现方法和示例代码。

前言,学习ExtJS Grid的新手,官方的这篇FAQ是必读的.

顺手翻译了一小点,大概了解就ok了,挺生硬的,呵呵...

 

翻译自:http://extjs.com/learn/Ext_FAQ_Grid#Behavior

 

5.行为

 

5.1 单元格的链接

 

>方法1: 自定义渲染方法

var yourColumn = {


   header: "Company",


   dataIndex: 'company',


   renderer: function(value, metaData, record, rowIndex, colIndex, store) {


      //在这里面可以按你自己的逻辑组成html然后返回


      return '<a href="http://www.yourURL.com/" target="_blank">'


             + value +'</a>';


   }


}

 

>方法2: 监听Ext.grid.RowSelectionModel的rowselect事件

function handleRowSelect(selectionModel, rowIndex, selectedRecord) {


    //assuming the record has a field named 'url' or build it as you need to


    var url = selectedRecord.get('url');


    //if you want to open another window


    window.open(url);


}


grid.getSelectionModel().on('rowselect', handleRowSelect);

 

5.2 为特殊的单元格添加点击事件

 

>除了下面将介绍的方法外,你还可以考虑下Saki提供的扩展:rowactions和cellactions,在http://www.extjs.eu 可找到

 

>如果你用的是编辑表格,则默认的选择模型是cell selection model,所以你可以监听beforecellselect和cellselect事件.

  它会传递点击的行和列的index。其中行index可以用来找到点击的field。(需要注意,用户有可能会对行进行排序,所以直接用行index来寻找field是不安全的,最好用id来索引)

 

>对Grid的cellclick事件添加一个监听器

当点击的时候,传递row index 和column index 给监听器。其中column index可以用来确定是点击的是哪个field。
(用户有可能会对改变行的位置,如拖拽,所以用column index作为field index是不安全的)

 

cellclick事件的处理函数可以如下代码,找到一些相关的信息:

function(grid, rowIndex, columnIndex, e) {


        // Get the Record for the row


        var record = grid.getStore().getAt(rowIndex);


        // Get field name for the column


        var fieldName = grid.getColumnModel().getDataIndex(columnIndex);


        var data = record.get(fieldName);


    }
 

>一个用cellclick事件切换单元格颜色的例子: http://extjs.com/forum/showthread.php?p=119718#post119718

 

>change the CSS so all cells look like an editor is active, e.g.

.x-grid3-row td.x-grid3-td-<id-of-column> {


	background-color: white;


	padding: 0;


}


.x-grid3-row td.x-grid3-td-<id-of-column> .x-grid3-cell-inner {


	border: 1px solid #a9bfd3;


	padding: 2px 3px 2px 5px;


}
 

5.3 如何添加/删除表格的某几列

   参见http://extjs.com/forum/showthread.php?p=308635#post308635

 

5.4 如何为每行记录都添加一个操作列

 

>方法1:

1)Use the array grid example in your packaged download array-grid.js

以自带例子里面的array-grid.js来示范

 

2)Add an extra column to the column model definition and a custom rendere

添加额外的一列

{header: "Controls", 
width: 60, 
sortable: false, 
renderer: function() {
    return '<div class="controlBtn">
            <img src="../shared/icons/fam/cog_edit.png"
            width="16" height="16" 
            class="control_edit">
            <img src="../shared/icons/fam/folder_go.png"
            width="16" height="16"
            class="control_go"></div>';
}, 
dataIndex: 'company'}

 

3)Then you would setup an event handler on the click event.

添加事件处理函数

grid.on('click', function(e) {
        var btn = e.getTarget('.controlBtn');        
        if (btn) {
            var t = e.getTarget();
            var v = this.getView();
            var rowIdx = v.findRowIndex(t);
            var record = this.getStore().getAt(rowIdx);            
            var control = t.className.split('_')[1];
            switch (control) {
                case 'edit':
                    console.log('edit this record - ' + record.id);
                    break;
                case 'go':
                    console.log('go to this record - ' + record.id);
                    break;
            }            
        }
    }, grid);
 

4)Add the following CSS rule in array-grid.html to give some padding and change the cursor.

<style>
   .controlBtn img {
      padding-left: 4px;
      cursor: pointer;
   }
</style>
 

 

5)Using this same method you could add as many tools as you would like in the controls section and always give them the css class "controls_{toolname}". Ideally you would break this out into an XTemplate so that you could simply pass in whatever tools you would like to use and output them on the grid as well.

 

 

>方法2:

使用RowAction这个扩展 http://extjs.com/forum/showthread.php?t=24508

 

 

 

 

 

 

 

 

提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值