我需要根据以下代码创建函数:
jsHelperFunctions = """
function getTreeList(treeId) {
var $tree = $(treeId);
var itemsList = [];
var treeList = $($tree.jstree().get_json($tree, {
flat: true
})).each(function(i, val) {
itemsList.push($tree.jstree().get_node(val.id))
})
return itemsList;
}
function getItemByItemCode(itemCode, itemsList) {
return itemsList.filter(val => val.original.account_code ==
itemCode)[0]
}"""
这是我的主管给我的一行代码:
treeList = driver.execute_script(jsHelperFunctions + """ return
getTreeList("#tree");""")
尝试了类似的方法,但是我不确定(第一个功能):
def get_tree_list(treeId):
tree = treeId
itemsList = []
treeList = tree.jstree().get_json(tree, flat: true).each(function(i, val)
itemsList.push(tree.jstree().get_node(val.id))
return itemsList
Py中的val
和flat
又是什么呢?
答案 0 :(得分:0)
找到了解决方案。花了我几天时间...
def getTreeList(treeId):
return driver.execute_script(jsHelperFunctions + """ return getTreeList(\""""+ treeId
+"\");")
treeList = getTreeList("#tree")
还有这个...
def getItemByCode(itemCode, treeList):
"""
Returns None if the item code is not found
"""
filtered_iterable = filter(lambda row : row["original"]["item_code"] == itemCode,
treeList)
filtered_list = list(filtered_iterable)
if len(filtered_list) == 0:
return None
assert(len(filtered_list) == 1) #item codes must be unique - so something's wrong
return filtered_list[0]