donglu5728 2014-10-03 19:13
浏览 194

Golang中复选框的惯用方式

Before you hit the downvote, the question isn't as ludicrous as it may sound from the title.

Say I have Items and Categories. An item can belong to multiple categories.

I have a basic HTML form where a user fills out stuff to create an item and I also list all categories as a list of checkboxes (see code below) so that a user can check which category (one or many) the item being created should belong to.

So far all is well.

However, when a user edits this item, I'd of course like to show the categories checkbox list with the categories the item belongs to populated as checked so that you can easily see which categories the item currently belongs to, and uncheck if you like etc.

Sounds easy enough.

So, we would have something like the following queries in the controller (say, I want to edit item with id = 3)

1) SELECT * FROM Item WHERE Id = 3  (gets us the item we want to edit)
2) SELECT * FROM Categories WHERE 1 (gets ALL categories)
3) SELECT CategoryId FROM ItemCategories WHERE ItemId = 3 (gets the categoryids for the categories the item belongs to)

The HTML for showing the categories (all categories) would look something like this

<form>
...
{{range .Categories}}
<input name="cat" type="checkbox" value="{{ .Id }}" /> {{ .Title}} <br />
{{end}}
...
</form>

So, what I'd typically would like to have done is just to be able to, per category in the range, check if the Id exists in the ItemCategories result, and if so set it to checked in the HTML code. Since it's difficult to use a FuncMap for this, since it won't allow you to pass along the slice with the checked categories to the method, it becomes a bit of a challenge to see how this can be done in the presentation layer, given that you have the data you need.

Of course, I can do this in several ways in the controller (anything from merging the two results in second and third query as a struct, to building the HTML output there, passing it via the context to the HTML page) but as I'm still learning how to code in golang, I just wanted to check how others are going about solving this type of situation as it is quite generic in its nature (i.e. I will certainly come across it again).

Thanks.

  • 写回答

1条回答 默认 最新

  • duananyantan04633 2014-10-03 20:22
    关注

    Well, you can delete the question if you want to but here's what I did. Maybe it can help someone else running into the same kind of problem.

    It turns out funcmap functions can take two parameters so we can actually put the comparison between the category and the categories of the item in the "view" i.e. in the HTML. The only trick is to avoid the "dot" context problem within the range.

    So, the solution would look like so:

    {{ $ic := .ItemCategories }}  //this is needed since range changes the context
    {{range .Categories}}
        <input name="cat" type="checkbox" value="{{ .Id }}" {{ CategorySelected .Id $ic}}/> {{ .Title}} <br />
    {{end}}
    

    Note that the CategorySelected that compares the id to a slice of ids, need to be listed in the FuncMap definition in order to work (at least it didn't work without it for me).

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么