对微软那棵TreeView进行试用,主要是对CheckBox进行操作

总结这次的工作,明白了几个关键的地方:
    1.如果在服务器端进行CheckBox的初始化,这样操作就会导致在客户端进行node.getAttribute("checked")时总是得到True,这样就达不到客户端操作的目的.
    2.如果想在生成树的时候对某些节点进行CheckBox的初始化选中,就需要在客户端进行遍历树的操作(构造生成树的同时进行).
    3.在客户端对树的节点进行选中与取消选中的操作时,必须同时进行一个必要的操作:Tree.queueEvent('oncheck',node.getNodeIndex()),这样做的目的使你在客户端进行树操作的同时,能让服务器端也同时知道操作了哪些节点,以便在服务器端进行遍历的同时能知道哪些节点被选中.
    4.以及'checked'的大小写问题也会对树的状态改变有所影响.

之后又对代码进行了优化,我把生成树时的初始化操作,以及保存选中状态都放在了客户端进行!

第一次发表文章,请各位前辈多多指导,多多帮助

None.gif var  strArray  =   new  Array();
None.gif
var  strList  =   "" ;
None.gif
None.gif
function  tree_oncheck()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ChildNode = new Array();
InBlock.gif    
var node = document.getElementById("TreeView1").getTreeNode(event.treeNodeIndex);
InBlock.gif    
var Pchecked = node.getAttribute("checked");
InBlock.gif    
InBlock.gif    ChildNode 
=  node.getChildren();
InBlock.gif    
if( parseInt( ChildNode.length ) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       node.setAttribute(
"checked",!Pchecked);
InBlock.gif       
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    setParent(node,Pchecked);
InBlock.gif    document.getElementById(
"TreeView1").queueEvent('oncheck',node.getNodeIndex());
ExpandedBlockEnd.gif}

None.gif
// ===============================================//
None.gif//
在点击子节点时,让父节点的状态也随子节点的状态而变化
None.gif//
===============================================//
None.gif
function  setParent(el,state)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ParentNode = el.getParent();
InBlock.gif    
if(ParentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//检查el的兄弟节点状态
InBlock.gif
        if(!checkSiblingdNode(el))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ParentNode.setAttribute('checked',state);
InBlock.gif            setParent(ParentNode,state);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// ===========================================//
None.gif//
检查兄弟节点状态
None.gif//
===========================================//
None.gif
function  checkSiblingdNode(el)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var parentNode = el.getParent();
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= parentNode.getChildren();
InBlock.gif            
InBlock.gif    
for(var i=0;i< childNodes.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(el.getNodeIndex() != childNodes[i].getNodeIndex())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(childNodes[i].getAttribute("Checked"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return false;
ExpandedBlockEnd.gif}

None.gif
// =============================================//
None.gif//
点击父节点时,对其子节点状态进行同步改变
None.gif//
=============================================//
None.gif
function  setcheck(node,Pc)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ChildNode = new Array();
InBlock.gif    ChildNode 
= node.getChildren();
InBlock.gif    
if( parseInt(ChildNode.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
forvar i=0; i<ChildNode.length;i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var cNode = ChildNode[i];
InBlock.gif            
if( cNode.getAttribute("checked"!= Pc )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( parseInt( cNode.getChildren().length ) != 0 )
InBlock.gif                    setcheck(cNode,Pc);
InBlock.gif                cNode.setAttribute(
"checked",Pc);
InBlock.gif                document.getElementById(
"TreeView1").queueEvent('oncheck',cNode.getNodeIndex());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ==========================================//
None.gif//
需要对某些节点做初始化处理时
None.gif//
==========================================//
None.gif
function  onload()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var node 
InBlock.gif    
var nextNode = 0;
InBlock.gif    
var childNodes = new Array();
InBlock.gif    
var childNodes = document.getElementById("TreeView1").getChildren();
InBlock.gif
InBlock.gif    SetArray();
//把节点ID组合为一个数组
InBlock.gif
    for(var i=0;i<childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        SetTree(childNodes[i]);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ============================================//
None.gif//
组合数组
None.gif//
============================================//
None.gif
function  SetArray()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var IndexStart = 0;
InBlock.gif    
var IndexEnd = 0;
InBlock.gif    
var ArrIndex = 0;
InBlock.gif    
var SignStr = "";
InBlock.gif    
//var SpanString = "0001,00010001,000100010001,";
InBlock.gif
    var SpanString = document.getElementById("H_IncomeCode").value;
InBlock.gif    
var strLen = SpanString.length;
InBlock.gif            
InBlock.gif    
while( IndexEnd < strLen )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SignStr 
= SpanString.substr(IndexEnd,1);
InBlock.gif        
if( SignStr == "," )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var TempString = SpanString.substr(IndexStart,IndexEnd-IndexStart);
InBlock.gif            strArray[ArrIndex] 
= TempString;
InBlock.gif                    
InBlock.gif            IndexStart 
= IndexEnd + 1;
InBlock.gif            ArrIndex 
= ArrIndex + 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        IndexEnd 
= IndexEnd + 1;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ==============================================//
None.gif//
遍历整棵树,进行节点的Checked的初始化设置//
None.gif//
==============================================//
None.gif
function  SetTree(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= node.getChildren();
InBlock.gif    
var i = 0;
InBlock.gif    
var j = 0;
InBlock.gif            
InBlock.gif    
for( i=0 ; i<strArray.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( strArray[i] == node.getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            saveCheckState(node);
InBlock.gif            node.setAttribute(
"checked","true");
InBlock.gif                    
//saveCheckState(node.getNodeIndex());
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if( parseInt(childNodes.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( j=0;j< parseInt(childNodes.length); j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for( i=0 ; i< parseInt(strArray.length); i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( strArray[i] == childNodes[j].getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    saveCheckState(childNodes[j]);
InBlock.gif                    childNodes[j].setAttribute(
"checked","true");
InBlock.gif                    
//saveCheckState(childNodes[j].getNodeIndex());
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            SetTree(childNodes[j]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( i=0 ; i<strArray.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( strArray[i] == node.getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                saveCheckState(node);
InBlock.gif                node.setAttribute(
"checked","true");
InBlock.gif                
//saveCheckState(node.getNodeIndex());
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ====================================================//
None.gif//
保存节点状态(保证在回传时节点状态依然存在)
None.gif//
====================================================//
None.gif
function  saveCheckState(el)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(el.getNodeIndex())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        document.getElementById(
"TreeView1").queueEvent("oncheck",el.getNodeIndex().toString());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ====================================================//
None.gif//
点击保存按钮时对所选中的节点进行整理
None.gif//
====================================================//
None.gif
function  FindChecked()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    strList 
= "";
InBlock.gif    
var ChildNodes = new Array();
InBlock.gif    ChildNodes 
= document.getElementById("TreeView1").getChildren();
InBlock.gif    
forvar i=0; i<ChildNodes.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CheckedAll(ChildNodes[i]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//alert(strList);
InBlock.gif
    doSelect(strList);
ExpandedBlockEnd.gif}

None.gif
function  CheckedAll(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var i = 0;
InBlock.gif    
var strID;
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= node.getChildren();
InBlock.gif            
InBlock.gif    
if( parseInt(childNodes.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( i=0; i<parseInt(childNodes.length); i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CheckedAll(childNodes[i]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( node.getAttribute("checked"== true )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strID 
= node.getAttribute("id");
InBlock.gif            strList 
= strList + strID + ",";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
            
ExpandedBlockEnd.gif}


 

转载于:https://www.cnblogs.com/hmilycsharp/archive/2006/07/13/450071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现双击编辑 Treeview 中的项目,你需要使用 Tkinter Treeview 组件的 `bind` 方法来绑定双击事件,并在事件处理函数中启用编辑功能。 以下是一个示例代码,可以实现双击 Treeview 中的项目进行编辑: ```python import tkinter as tk from tkinter import ttk class EditableTreeview(ttk.Treeview): def __init__(self, parent, **kwargs): super().__init__(parent, **kwargs) self.bind('<Double-1>', self.start_editing) def start_editing(self, event): """Start editing the item on double click""" # Get the item that was clicked item_id = self.identify_row(event.y) # Make sure an item was clicked and not the Treeview itself if item_id: # Get the text of the item that was clicked item_text = self.item(item_id)['text'] # Create an Entry widget to edit the item entry = tk.Entry(self, validate='key') entry.insert(0, item_text) # Set the Entry widget as the editor of the Treeview item self.item(item_id, text='', image='', tags=('editing',)) self.set(item_id, column=0, value='', text='', image='', tags=('editing',)) self.editing_id = item_id self.editing_widget = entry self.editing_value = item_text # Place the Entry widget in the Treeview self.pack_forget() self.place(in_=self, relx=1, anchor='w', x=self.bbox(item_id, column=0)['x'], y=self.bbox(item_id)['y']) entry.focus_set() entry.bind('<Return>', self.finish_editing) entry.bind('<Escape>', self.cancel_editing) entry.bind('<FocusOut>', self.cancel_editing) def finish_editing(self, event): """Finish editing the item""" new_value = self.editing_widget.get() self.set(self.editing_id, column=0, value=new_value) self.item(self.editing_id, text=new_value, image='', tags=('edited',)) self.editing_widget.destroy() self.editing_id = None self.editing_widget = None self.editing_value = None def cancel_editing(self, event): """Cancel editing the item""" self.set(self.editing_id, column=0, value=self.editing_value) self.item(self.editing_id, text=self.editing_value, image='', tags=('cancelled',)) self.editing_widget.destroy() self.editing_id = None self.editing_widget = None self.editing_value = None # Create the Treeview widget root = tk.Tk() treeview = EditableTreeview(root) treeview.pack() # Insert some items into the Treeview treeview.insert('', 'end', text='Item 1') treeview.insert('', 'end', text='Item 2') treeview.insert('', 'end', text='Item 3') # Start the main event loop root.mainloop() ``` 在这个示例中,我们创建了一个名为 `EditableTreeview` 的自定义 Treeview 组件,它继承自 Tkinter Treeview 组件,并覆盖了其 `__init__` 方法和 `start_editing` 方法。在 `__init__` 方法中,我们使用 `bind` 方法来绑定 `<Double-1>` 事件,即双击事件,当用户双击 Treeview 中的项目时,`start_editing` 方法会被调用。 在 `start_editing` 方法中,我们首先获取被点击的项目的 ID,然后创建一个 `Entry` 组件来编辑该项目。我们将 `Entry` 组件作为 Treeview 项目的编辑器,并将其放置在 Treeview 中。在 `Entry` 组件中,我们使用 `<Return>`、`<Escape>` 和 `<FocusOut>` 事件来完成、取消或离开编辑模式。 在 `finish_editing` 方法中,我们获取编辑器中的新值,并将其设置为 Treeview 项目的新值。在 `cancel_editing` 方法中,我们将编辑器中的值恢复为原始值。 最后,我们创建了一个 `EditableTreeview` 实例,并在其中插入了一些项目。运行程序后,你应该可以双击 Treeview 中的项目,并使用 `Entry` 组件编辑该项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值