Extjs4 Tree Grid 综合示例(展开、编辑列、获取数据)

用json数据模拟后端传回来的结果,Extjs tree支持两种类型的结构,一种是带children属性的嵌套式的数据,一种是扁平的,每条记录带pid的数据,带pid的添加配置项可以自动解析成树形结构。
{
"text": "根目录",
"children": [
{
"id": "1",
"name": "基础设置",
"children": [
{
"id": "1-1",
"name": "部门档案",
"total": 3,
"children": [
{
"id": "1-1-1",
"name": "增加部门",
"nd": "1"
}, {
"id": "1-1-2",
"name": "修改部门"
}
]
}, {
"id": "1-2",
"name": "客户档案",
"nd": "2",
"total": 1,
"children": [
{
"id": "1-2-1",
"name": "增加客户"
}, {
"id": "1-2-2",
"name": "客户设置"
}
]
}
]
}, {
"id": "2",
"name": "综合业务",
"children": [
{
"id": "2-1",
"name": "凭证",
"nd": "1",
"total": 3,
"children": [
{
"id": "2-1-1",
"name": "填制凭证"
}, {
"id": "2-1-2",
"name": "凭证审核"
}
]
}, {
"id": "2-2",
"name": "采购",
"nd": "3",
"total": 1,
"children": [
{
"id": "2-2-1",
"name": "采购订单"
}, {
"id": "2-2-2",
"name": "采购结算"
}
]
}
]
}
]
}
Tree grid代码
Ext.require([ 'Ext.form.*', 'Ext.tip.QuickTipManager' ]);
Ext.onReady(function() {
Ext.define('myTreeModel', {
extend : 'Ext.data.Model',
idProperty: 'id',
fields : [
{name : 'id'},
{name : 'name'},
{name : 'nd'},
{name : 'total'},
]
});
// tree组件要求数据的叶子节点必须有leaf=true,此函数递归tree数据检查和自动添加leaf=true
function recursiveSetLeaf(treeNode) {
if (treeNode.children) {
console.log('当前节点有children');
Ext.each(treeNode.children, function(child){
recursiveSetLeaf(child);
});
} else {
console.log('当前节点为末级节点');
treeNode.leaf = true;
}
}
// 创建grid
var sdPaperRules = Ext.create('Ext.tree.Panel', {
id : "sdPaperRules",
store : new Ext.data.TreeStore({
model: "myTreeModel",
proxy: {
type: 'ajax',
url: '/exam_admin/admin/service/gkpapers_rule/tree_grid_test.json',
extractResponseData(response) {
// 非pid
/**
* 如果是扁平的带pid的记录,可参考
* http://t.zoukankan.com/love-omnus-p-4196603.html
*
*/
// var json = Ext.loadFilter(Ext.JSON.decode(response.responseText),{parentField : 'pid'});
console.log('response', response);
var rootNode = Ext.JSON.decode(response.responseText);
recursiveSetLeaf(rootNode);
response.responseText = Ext.JSON.encode(rootNode);
return response
},
},
folderSort: true
}),// 指定数据源
// 可编辑单元格插件,配置此插件后就可以在列配置中启用列的编辑
plugins: Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit : 1,
}),
viewConfig: {
// 给表格行添加样式
getRowClass(record, rowIndex, rowParams, store){
if (rowIndex % 2 !== 0) {
return "treegrid-row-style-even"; // 给偶数行设置样式
}
}
},
autoScroll : true,
bodyStyle : 'overflow-x:auto; overflow-y:auto',
autoHeight : true,
autoWidth : true,
rootVisible: false,
// border : true,
columns : [
{
xtype : 'treecolumn',
text : "功能点",
width : 250,
dataIndex : 'name',
headersDisabled : true,
menuDisabled : true,
editable: false
},
{
dataIndex: 'nd',
text: '难度',
width : 250,
// flex: 1,
align: 'left',
headersDisabled : true,
menuDisabled : true,
renderer(value, metaData, record, rowIndex, colIndex, store, view) {
if (!value) return;
var html = '';
html += `<div style="display:flex;justify-content: flex-start;align-items: center;">`;
var radioOptions = [{ "value": "1", "label": "简单" }, { "value": "2", "label": "普通" }, { "value": "3", "label": "困难" }];
for (option of radioOptions) {
// console.log('option', option);
var checked = option.value == value ? `checked='checked'` : '';
var name = "grid_column_radio_" + record.data.id;
var domId = 'radio_' + record.data.id + '_' + option.value;
html += `<div style="display:flex;justify-content: center;align-items: center;margin-right:16px;">`;
html += ` <input id="${domId}" name='${name}' type='radio' ${checked} value='${option.value}'/>`;
html += ` <label for="${domId}"> ${option.label}</label>`;
html += `</div>`;
}
html += `</div>`;
return html;
}
},
{
text : "数量",
width : 100,
dataIndex : 'total',
headersDisabled : true,
menuDisabled : true,
border: 1,
editor : {
xtype : 'textfield'
}
}
],
listeners: {
beforeitemcollapse() {
console.log('beforeitemcollapse');
return false; // 禁止收缩节点
},
itemclick(treePanel, record, rowTag, rowIndex, event) {
// 处理单选按钮选择事件
var radio = event.getTarget('input[type="radio"]');
if (radio) {
console.log('要更改的nd', radio.value);
record.set({nd: radio.value});
}
},
beforeedit(treePanel, cell, events) {
console.log('record', cell.record.data);
if (!cell.record.data.nd) { // 如果当前节点没有难度设置,则禁止编辑
return false;
}
}
},
bbar : Ext.create("Ext.toolbar.Toolbar", {
items: [
{
xtype: 'button',
text: '获取结果数据',
handler() {
getTreeGridData(sdPaperRules.getRootNode());
// console.log('1:1', sdPaperRules.getView().getCell(1, 1));
// Ext.encode(record.data)
// 好像grid有个dirty属性,判断修改过没有,可以把有修改的record过滤出来
}
},
{
xtype : 'tbtext',
text: 'test...<span style="color:red;">lsdjfoiej</span>'
}
]
})
});
window.getTreeGridData = function(node) {
// console.log('node', node);
if (node.data.nd) {
console.log('nd', node.data.nd)
}
if (node.childNodes) {
for (n of node.childNodes) {
getTreeGridData(n);
}
}
}
// 获得当前选中的tab,下面将向这个tab添加模块组件
var tab = Ext.getCmp("treeGridTest");
// 展开所有节点
sdPaperRules.expandAll();
// 向tab里添加一个按钮
tab.add(sdPaperRules);
});
由于Extjs的Tree组件和grid结合的话,默认情况数据行没有样式,在多列情况下,不易区分当前单元格属于哪一样。因此需要添加全局样式控制补充效果。
.treegrid-row-style-even .x-grid-cell {
border-top: 1px solid #ededed;
border-bottom: 1px solid #ededed;
background-color: #fafafa;
}
.treegrid-row-style-even:hover .x-grid-cell {
background-color: #ededed;
}
Extjs4 Tree Grid 综合示例(展开、编辑列、获取数据)的更多相关文章
- ExtJs4之Grid详细
ExtJs博客前奏 由于这段时间事情比较杂乱,博客就主要以项目中例子来说明编写. ExtJs4中的Grid非常强大,有展示,选中,搜索,排序,编辑,拖拽等基本功能,这篇博客我就这几个功能做写累述. 1 ...
- ExtJS4.2学习(七)EditorGrid可编辑表格(转)
鸣谢地址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-14/176.html ------------- ...
- 图解DataGridView编辑列
WinForm中DataGridView功能强大,除了可以自动绑定数据源外,还可以根据需求编辑列.下面以截图说明添加编辑列的步骤(HoverTreeSCJ 项目实际界面). 1.选择DataGridV ...
- oracle+servlet+extjs4 分页表格布局示例代码
Log.java package com.example.entity; import java.util.Date; public class Log { private int id; priva ...
- Spring MVC 学习总结(四)——视图与综合示例
一.表单标签库 1.1.简介 从Spring2.0起就提供了一组全面的自动数据绑定标签来处理表单元素.生成的标签兼容HTML 4.01与XHTML 1.0.表单标签库中包含了可以用在JSP页面中渲染H ...
- C#与数据库访问技术总结(七)综合示例
综合示例 说明:前面介绍了那么多,光说不练假把式,还是做个实例吧. 表:首先你要准备一张表,这个自己准备吧.我们以学生表为例. 1.ExecuteScalar方法 ExecuteScalar方法执行返 ...
- 前端MVC Vue2学习总结(一)——MVC与vue2概要、模板、数据绑定与综合示例
一.前端MVC概要 1.1.库与框架的区别 框架是一个软件的半成品,在全局范围内给了大的约束.库是工具,在单点上给我们提供功能.框架是依赖库的.Vue是框架而jQuery则是库. 1.2.AMD与CM ...
- LayUI后台管理与综合示例
一.LayUI介绍 layui(谐音:类UI) 是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的内在,体 ...
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续2篇-模板导出综合示例)
自ExcelUtility类推出以来,经过项目中的实际使用与不断完善,现在又做了许多的优化并增加了许多的功能,本篇不再讲述原理,直接贴出示例代码以及相关的模板.结果图,以便大家快速掌握,另外这些示例说 ...
- 在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列?
在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列?(效果图如下:): 在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列(效果图) 实现该 ...
随机推荐
- 关于Unity 图片队列存储以及出列导致内存溢出的解决方案
图片虽然出列但是并没有销毁,所以..destroy !
- [UnityAI]行为树的中断机制
参考链接: https://www.cnblogs.com/01zxs/p/9863715.html https://blog.csdn.net/AcmHonor/article/details/12 ...
- arcengine动态显示所需字段值
需求:实现和GIS桌面端中Identify的类似功能,鼠标滑动的时候可以显示鼠标所在位置的要素的指定字段的值.. 主要操作流程: ①先打开一个对话框,用于选择需要显示的图层和字段名 ②点击确定之后,在 ...
- IDEA快键键设置为Eclipse快捷键
一.基础修改,参考这个就可以了 https://jingyan.baidu.com/article/6dad5075a5f7b4e122e36e4b.html 二.其他需要手动配置的快捷键修改(主要是 ...
- win10试安装docker部署hyperf
一:部署虚拟机,这里使用的win系统带的Hyper-V虚拟机,其它虚拟机也行 1.win+R打开命令行 2.安装Hyper-V . 失败放弃安装,选择其它吧 3.win10安装VMware 这里参考 ...
- Hugging Face 每周速递: Chatbot Hackathon;FLAN-T5 XL 微调;构建更安全的 LLM
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...
- Github Copilot 比在座各位更会写代码。jpg
之前大佬和我安利过 Copilot, 作为一个能用就行的践行者, 我一贯对这些东西都不太感兴趣. 就如我多年VS Code写各种编程语言, jetbrains 全家桶我都懒得搞~ 不过最近看到过Cha ...
- Spring Security 框架使用
更多内容,前往IT-BLOG 一.Spring Security 简介 Spring Security 是一个能够为基于 Spring 的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供 ...
- 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(12)-Charles如何使用Repeat功能进行简单压力测试
1.前言 李四:"今天好累啊,点的我手指都疼了.我一直被要求给后端接口的同事重复发送请求来调试接口." Charles:"哎呀,李四同学,你怎么能一条一条的手动发送呢 我 ...
- Dubbo服务提供者如何优雅升级?
文章首发于公众号:BiggerBoy.欢迎关注. 往期文章推荐 大坑!隐式转换导致索引失效...高性能分布式限流:Redis+Lua真香!MySQL索引知识点&常见问题汇总联合索引在B+树上的 ...