使用JsViews级联选择:进行选择观察另一个选择

时间:2018-12-12 17:07:55

标签: jsrender jsviews

页面上有两个选择元素:“ Make”和“ Model”。我有一个包含所有Model的大数组,每个Model都有一个makeId,它们对应于Make选择中各项的值。我正在使用一个转换器来填充基于所选品牌的Model选择数据。该转换器可以处理页面的初始加载,但是当我更改Make时,它似乎并没有改变。如果以任何方式修改Models数组,它也会更改。有没有一种方法可以让Model select遵守Make select?我尝试将变量传递给包含第一个选择的值的第二个选择,但这并未导致它观察到更改。这是两个选择的样子:

# Test data 
text = """popSize: 1000
numSurvivors: 0
tournamentSize: 10
probMutation: 0.1
probCrossover: 0.9
numIters: 100
Accuracy: 96.84 
Error Rate: 3.16 
Not Classified: 0.00
Total time: 5.36

popSize: 1000
numSurvivors: 0
tournamentSize: 10
probMutation: 0.1
probCrossover: 0.9
numIters: 100
Accuracy: 96.84 
Error Rate: 3.16 
Not Classified: 0.00
Total time: 4.472

popSize: 1000
numSurvivors: 0
tournamentSize: 10
probMutation: 0.1
probCrossover: 0.9
numIters: 100
Accuracy: 92.11 
Error Rate: 7.89 
Not Classified: 0.00
Total time: 4.46
"""

import pandas as pd
from io import StringIO

import pandas as pd

# Function invoked by pandas read on each element, for conversion. Here
# just return the right hand side of the value delimited by ":"
def value(item):
    return item[item.find(':')+1:]

# Reformat the incoming text to comma delimited, each double line break is a record break.
text2 = text.replace(": ", ":").replace("\n\n", "$$break$$").replace("\n", ",").replace("$$break$$","\n").replace("\n,", "")


## Adapted from https://stackoverflow.com/questions/15907869/reading-key-value-pairs-into-pandas
## read table applying a coverter fucntion per element, and supplying the column names
df = pd.read_table(StringIO(text2), header=None, delimiter=',',
                   converters={i:value for i in range(11)},
                   names=['popSize','numSurvivors','tournamentSize','probMutation','probCrossover','numIters','Accuracy','Error Rate','Not Classified','Total time'])

1 个答案:

答案 0 :(得分:0)

您需要确保selectedMake发生变化时,它会触发{^{for ~root.data.Models convert='getModelsForMake'}}的刷新。问题是您的转换器getModelsForMake依赖于selectedMake-但是{^ {for}}标签并不“知道”。

您可以通过几种不同的方式来表示对selectedMake的依赖性。

声明对{^{for}}的依赖性:

{^{for ~root.data.Models convert='getModelsForMake' depends='selectedMake'}}

通过添加代码来声明对转换器的依赖:

$.views.converters.getModelsForMake.depends = "~root.selectedMake";

传入selectedMake作为{^{for}}上的第二个参数:

{^{for ~root.data.Models selectedMake convert='getModelsForMake'}}

通过在{^{for}}上为selectedMake包括“空检查”来引入依赖性:

{^{for selectedMake && ~root.data.Models convert='getModelsForMake'}}

顺便说一句,Data-linked <select> elements的文档中现在包含了级联选择的替代版本。 (该链接会将您带到相关部分。)