获得一个字段来更新另一个字段的最佳方法

时间:2015-06-11 07:45:51

标签: python ajax web2py

我有一张桌子A

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];

    UIButton *BtnBreadcrumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [BtnBreadcrumb addTarget:self action:@selector(selectBtnBreadcrumb:)
            forControlEvents:UIControlEventTouchUpInside];
    [BtnBreadcrumb.titleLabel setFont: [BtnBreadcrumb.titleLabel.font fontWithSize:17]];

    [BtnBreadcrumb setTitle:@"Test" forState:UIControlStateNormal];
    BtnBreadcrumb.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    BtnBreadcrumb.tintColor=ThemeColor;
    CGSize stringsize = [selectedDepartment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
    BtnBreadcrumb.frame = CGRectMake(10, 5, stringsize.width, 40);
    [view addSubview:BtnBreadcrumb];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(stringsize.width, 5, 200, 40)];
    //[label setFont:[UIFont boldSystemFontOfSize:12]];
    NSString *string = [NSString stringWithFormat:@"/ %@",selectedCategory];
    label.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    label.textColor = TextColor;
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];
    return view;
}

我的模型定义如下:

Table A:
---------------------------------------
id |    valueName    |    value
---------------------------------------
1  |    2001         |    Nepal
---------------------------------------
2  |    2002         |    Thailand
---------------------------------------

我想要的是当用户填写 chosing_opt = ("2001", [ ("Sig1", T("Sig1"), "I", "E", "O"), ("Sig2", T("Sig2"), "E", "S", "O"), ("Sig3", T("Sig3"), "E", "M", "O") ], "2002", [ ("Val1", T("Val1"), "I", "E", "O"), ("Val2", T("Val2"), "E", "S", "O"), ("Val3", T("Val3"), "E", "M", "O") ], ) define_table(tablename, Field("priority",), Field("code", "list:string",), ) 字段时,说code。由于2001年在表A中,它应该在2001字段中显示priority的Sig1,Sig2和Sig3,以及chosing_opt中的2002code字段中的下拉列表,显示priority的Val1,Val2和Val3。

请建议。感谢

1 个答案:

答案 0 :(得分:0)

您的代码中的结构略有不同,因此您必须将其转换为易于编写代码的内容(尽管这不是唯一的方法 - 您可以使用for / while循环)。

我们的想法是将20012002等值转换为键,将下一个元组转换为各自的值。

def get_drop_downs(opts, val):
    # zip together (keys, values)... Keys = Starting from index 0, skip 1 tuple. 
    # Same for values, starting from index 1
    d_opts = dict(zip(opts[0::2], opts[1::2]))
    print('Just Key Names = {}'.format(opts[0::2]))  # Double check the keys
    print('The related tuples, in same order={}'.format(opts[1::2]))  # Double check the values
    results = [tpls[0] for tpls in d_opts[val]]  # every value is a tuple, so pick up the first value only
    print('Results = {}'.format(results))

最后,按如下方式测试:

T = str
get_drop_downs(chosing_opt, '2001')

您的结果将如下所示:

Just Key Names = ('2001', '2002')
The related tuples, in same order=([('Sig1', 'Sig1', 'I', 'E', 'O'), ('Sig2', 'Sig2', 'E', 'S', 'O'), ('Sig3', 'Sig3', 'E', 'M', 'O')], [('Val1', 'Val1', 'I', 'E', 'O'), ('Val2', 'Val2', 'E', 'S', 'O'), ('Val3', 'Val3', 'E', 'M', 'O')])
Results = ['Sig1', 'Sig2', 'Sig3']

注意:不确定代码中的T是什么,因此我将其转换为字符串(通过将其用作第一行T = str)来抢占它对于我自己的测试,所以结果显示