自定义Scala表单模板

时间:2016-04-05 22:40:52

标签: scala playframework

我正在尝试定义自己的自定义模板,以便在输入时输入:

var bazs = new List<Baz> {
    new Baz {
        Id = 1,
        StartDate = DateTime.Today,
        EndDate = DateTime.Today.AddMonths(1)
    },
    new Baz {
        Id = 2,
        StartDate = DateTime.Today.AddMonths(-1),
        EndDate = DateTime.Today.AddMonths(1)
    },
    new Baz {
        Id = 3,
        StartDate = DateTime.Today,
        EndDate = DateTime.Today.AddMonths(1)
    }
};

List<Foo> foos = new List<Foo>{
    new Foo
    {
        EmployeeId = 1,
        CompanyId = 2,
        GroupId = 3,
        Bars = new List<Bar> {
            new Bar { BazId = 1 },
            new Bar { BazId = 2 },
            new Bar { BazId = 3 }
        }
    }
};

var result = (from extendedBar in foos.SelectMany(f => f.Bars.Select(b => new { Bar = b, f.EmployeeId, f.CompanyId, f.GroupId }))
              join baz in bazs
              on extendedBar.Bar.BazId equals baz.Id
              select new { extendedBar.Bar, extendedBar.EmployeeId, extendedBar.CompanyId, extendedBar.GroupId, baz.StartDate, baz.EndDate })
                .GroupBy(o => new { o.EmployeeId, o.CompanyId, o.GroupId, o.StartDate, o.EndDate })
                .Select(g => new Foo { EmployeeId = g.Key.EmployeeId, CompanyId = g.Key.CompanyId, GroupId = g.Key.GroupId, Bars = g.Select(o => o.Bar).ToList() })
             .ToList();

它会吐出以下的html

@inputText(
  myForm("firstName"),
  '_label -> "First Name"
)

1 个答案:

答案 0 :(得分:2)

您需要创建自定义fieldConstructorTemplate。

  1. 创建名为<urname>.scala.html
  2. 的文件
  3. 然后在那里写下面的代码:

    @(elements: helper.FieldElements)
    <div class="form-group">
      <label class="col-lg-12" for="@elements.id">@elements.label</label>
      <div class="col-sm-9">
       <input type="text" class="form-control" name="@elements.field.name"
        value="@elements.field.value"></input>
     </div>
    

  4. 接下来在scala模板中使用:

    @inputText( myForm("firstName"), '_label -> "First Name")
    
    @implicitField = @{ FieldConstructor(yourFieldConstructorTemplateName.f) }