📄 treeselect.py
字号:
# -*- coding: ISO8859-1 -*-# $Id: TreeSelect.py 3617 2006-09-08 11:26:19Z jodal $## Copyright 2003, 2004 Norwegian University of Science and Technology## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: Hans J鴕gen Hoel <hansjorg@orakel.ntnu.no>#"""Contains three classes for constructing a tree select.The TreeSelect class is a continer for Select instances.Select represents a general select, and UpdateableSelectis a select that is generated dynamically based on inputin the Select and the other UpdateableSelects."""################################################### Importsimport nav.db.manage################################################### class TreeSelectclass TreeSelect: """ TreeSelect is a container class for Select and UpdateableSelect objects. The select objects are instantiated and then added with the addSelect method. When the form is submitted by a change in one of the selects, update() must be called to make changes to the selects. """ # List of elements in this TreeSelect elementList = None defaultWidth = '300' formName = 'treeselect' def __init__(self): self.elementList = [] return def addSelect(self, select): " Adds a select object to the element list. " self.elementList.append(select) return def update(self,form): " Updates the selects in the element list based on the form. " for select in self.elementList: if select.updateThis: select.selectedList = [] maybe_selected = [] if form.has_key(select.elementName): # maybe_selected is the rows indicated as selected from the # form. since there may be rows which are selected, but # doesn't exist after updating the previous box, the list # must be checked against the existing rows in the prev select maybe_selected = form[select.elementName] # if only one row is selected, the mod_python FieldObject # returns a string, not a list. Checks and makes a list. if not type(maybe_selected) is list: maybe_selected = [maybe_selected] if select.prevElement: # does this select have a previous select element? # ie. another select box to the left of it for selected in maybe_selected: if(form.has_key(select.elementName + '_' + selected)): # this row was selected the last time # should it still be selected? last_key = form[select.elementName + '_' + selected] if select.prevElement.selectedList.count(last_key): select.selectedList.append(selected) else: # this row was just selected now, so it must be added select.selectedList.append(selected) else: # if this is the first select, then the selected rows in # the form are always correct (the first list is static) select.selectedList = maybe_selected # Are there any preselected items? # In that case, add them now for id in select.preSelected: if not select.selectedList.count(id): select.selectedList.append(id) # Recurse through the elements and update the options # start with the first element if select.updateThis: select = self.elementList[0] next = select.nextElement while(next): next.updateOptions(next.prevElement.selectedList) next = next.nextElement # update the selected rows (selected in form -> Option.selected = true) for select in self.elementList: select.updateSelected()################################################### Class Selectclass Select: """ Select is a simple select. Used for the first select in the TreeSelect It is not possible to update it dynamically. The options list is populated by the initOptions() method, each time the Select is instantiated. """ # Update this select with TreeSelect.update()? updateThis = True # html element name elementName = None # list of options and default options options = [] optionsDefault = [] # multiple select [True|False] and the size in rows of the select multiple = False multipleSize = None # the table and columns used to initialize the options initTable = None initTextColumn = None initIdColumn = None # html onchange attrib (script) onchange = None # points to the next and previous selects nextElement = None prevElement = None # copy of the list of selected entries for this select selectedList = [] def __init__(self, elementName, title, optionsDefault = None, multiple = False, multipleSize = None, initTable = None, initTextColumn = None, initIdColumn = None, onchange = 'this.form.submit()', preSelected = [], optionFormat = None, optgroupFormat = None, orderByValue = False): """ elementName = html element name for this select optionsDefault = list of default Option()'s for top of select multiple = [True|False], multiple select allowed or not multipleSize = number of rows displayed if multiple select is on initTable = name of the table that initializes this select initTextColumn = the column used for text on each option initIdColumn = the primary key used for each options value onchange = script for submitting on each change preSelected = list of preselected id's (from QuickSelect for ex.) optionFormat = format string for the <option>s $v = value, $d = descr optgroupFormat = format string for <optgroup>s $v = value, $d = descr orderByValue = True|False, order by value? else order by descr """ self.elementName = elementName self.title = title self.onchange = onchange self.preSelected = preSelected self.optionFormat = optionFormat self.optgroupFormat = optgroupFormat self.orderByValue = orderByValue # Add the default options self.options = [] if optionsDefault: self.optionsDefault = optionsDefault self.addDefaultOptions() if multiple: self.multiple = True self.multipleSize = multipleSize # Populate the options list self.initTable = initTable self.initTextColumn = initTextColumn self.initIdColumn = initIdColumn if self.initTable: self.initOptions() return def initOptions(self): " Populate the options list from the initTable. " table = getattr(nav.db.manage, self.initTable) ob = self.initTextColumn if self.orderByValue: ob = self.initIdColumn for row in table.getAllIterator(orderBy=ob): descr = getattr(row, self.initTextColumn) value = getattr(row, self.initIdColumn) if self.optionFormat: text = self.optionFormat text = text.replace('$v',value) text = text.replace('$d',descr) else: text = descr selected = False self.options.append(Option(text,value,selected)) return def updateSelected(self): " Updates the selected options. Must be called for each update(). " if type(self.selectedList) is str: string = self.selectedList self.selectedList = [] self.selectedList.append(string) for option in self.options: value = option.value if self.selectedList.count(value):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -