ExtJS中创建节点appendChild后实现编辑功能求助
ExtJS树形组件编辑功能实现修正方案
1. 修正代码中的方法调用错误
你当前代码里的 newSection.BeginEdit() 存在两处问题:一是ExtJS方法名采用小驼峰命名,正确写法为 beginEdit();二是代码末尾多余的逗号需要移除。修正后代码如下:
createSection: function(button) { var node = button.up('menu').node; if (node.data.leaf) { return false; } node.expand(); var newSection = node.appendChild({ name: 'New section ' + "(" + (node.childNodes.length +1) + ")" , type: 'toc-by-system', leaf: false, id: uuid['v4'](), children: [], editor: { xtype: 'textfield' } }); newSection.beginEdit(); },
2. 配置树形面板的编辑支持
仅修正方法调用还不够,需要给TreePanel添加编辑插件并配置列的可编辑属性,才能让编辑功能生效:
Ext.create('Ext.tree.Panel', { // 其他原有配置项 plugins: { ptype: 'cellediting', clicksToEdit: 1 // 设置点击1次进入编辑,可根据需求改为2次 }, columns: [{ xtype: 'treecolumn', text: '章节', dataIndex: 'name', flex: 1, editor: { xtype: 'textfield', allowBlank: false // 禁止空值提交 } }] });
3. 额外注意事项
- 若节点使用自定义
type,需确保对应Model中的name字段未设置persist: false(默认允许编辑) - 编辑完成后,系统会自动触发
endEdit(),也可手动调用newSection.endEdit()强制结束编辑 - 如需监听编辑完成事件,可在CellEditing插件中添加监听逻辑:
plugins: { ptype: 'cellediting', clicksToEdit: 1, listeners: { edit: function(editor, context) { // 这里写编辑完成后的业务逻辑,比如提交到后端 console.log('修改后的章节名:', context.record.get('name')); } } }
内容的提问来源于stack exchange,提问作者Jzone
相关产品推荐
相关产品推荐

