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).

    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了