无法从模板中操作DOM以添加/删除属性。

huangapple go评论85阅读模式
英文:

Can not manipulate DOM from template to add/remove attribute

问题

在我的代码中,我想根据用户的身份验证级别禁用一些输入字段,我已经能够在JavaScript中实现,但这是不推荐的解决方案,我想从服务器端实现。

以下是我的go代码:

package main

import (
	"context"
	"html/template"
	"net/http"
	"strings"
	"time"
)

type User struct {
	Flags string
	Title string
}

type UsersPageData struct {
	PageTitle string
	Users     []User
}

func requestTime(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		ctx = context.WithValue(ctx, "requestTime", time.Now().Format(time.RFC3339))
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}

func helloHandler(name string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var title string
		if requestTime := r.Context().Value("requestTime"); requestTime != nil {
			if str, ok := requestTime.(string); ok {
				title = "\ngenerated at: " + str
			}
		}

		master := strings.Join([]string{"admin", "user", "superuser", "read"}, " ")
		admin := strings.Join([]string{"admin"}, " ")
		user := strings.Join([]string{"user"}, " ")

		tmpl := template.Must(template.ParseFiles("index.html"))
		data := UsersPageData{
			PageTitle: "Users list: " + title,
			Users: []User{
				{Flags: master, Title: "Everything"},
				{Flags: admin, Title: "Administrator"},
				{Flags: user, Title: "Normal user"},
			},
		}
		tmpl.Execute(w, data)
	})
}

func main() {
	http.Handle("/john", requestTime(helloHandler("John")))
	http.ListenAndServe(":8080", nil)
}

这是包含JS代码的模板:

<style>
.admin {
color: green;
}
.user {
color: red;
--btn-disable: 0;
}
[data-authorized="no"] {
/* Attribute has this exact value */
cursor: not-allowed;
pointer-events: none;
/*Button disabled - CSS color class*/
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
</style>
<h1>{{.PageTitle}}</h1>
<ul>
{{range .Users}}
<input type="text" data-permissions={{.Flags}} data-authorized="no">{{.Title}}</s>
{{end}}
</ul>
<script>
var flags = ["admin", "super user"]
var elements = document.querySelectorAll("input");
elements.forEach((element, index, array) => { 
if(element.hasAttribute("data-permissions")){
console.log(element.dataset.permissions)
var perm = element.dataset.permissions.split(" ");
var found = false;
for (var i = 0; i < perm.length; i++) {
if (flags.indexOf(perm[i]) > -1) {
element.dataset.authorized = "yes";
element.removeAttribute("data-permissions");
break;
}
} 
}
});
</script>

这是输出结果:
无法从模板中操作DOM以添加/删除属性。

有什么想法吗?

英文:

In my code, I want to disable some inputs fields based on the user authentication level, I was able to do it in JavaScript, but this is unrecommended solution, and I want to make it from the server side.

Below is my go code;

package main

import (
	&quot;context&quot;
	&quot;html/template&quot;
	&quot;net/http&quot;
	&quot;strings&quot;
	&quot;time&quot;
)

type User struct {
	Flags string
	Title string
}

type UsersPageData struct {
	PageTitle string
	Users     []User
}

func requestTime(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		ctx = context.WithValue(ctx, &quot;requestTime&quot;, time.Now().Format(time.RFC3339))
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}

func helloHandler(name string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var title string
		if requestTime := r.Context().Value(&quot;requestTime&quot;); requestTime != nil {
			if str, ok := requestTime.(string); ok {
				title = &quot;\ngenerated at: &quot; + str
			}
		}

		master := strings.Join([]string{&quot;admin&quot;, &quot;user&quot;, &quot;superuser&quot;, &quot;read&quot;}, &quot; &quot;)
		admin := strings.Join([]string{&quot;admin&quot;}, &quot; &quot;)
		user := strings.Join([]string{&quot;user&quot;}, &quot; &quot;)

		tmpl := template.Must(template.ParseFiles(&quot;index.html&quot;))
		data := UsersPageData{
			PageTitle: &quot;Users list: &quot; + title,
			Users: []User{
				{Flags: master, Title: &quot;Everything&quot;},
				{Flags: admin, Title: &quot;Administrator&quot;},
				{Flags: user, Title: &quot;Normal user&quot;},
			},
		}
		tmpl.Execute(w, data)
	})
}

func main() {
	http.Handle(&quot;/john&quot;, requestTime(helloHandler(&quot;John&quot;)))
	http.ListenAndServe(&quot;:8080&quot;, nil)
}

And here is my template including the JS code;

&lt;style&gt;
.admin {
color: green;
}
.user {
color: red;
--btn-disable: 0;
}
[data-authorized=&quot;no&quot;] {
/* Attribute has this exact value */
cursor: not-allowed;
pointer-events: none;
/*Button disabled - CSS color class*/
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
&lt;/style&gt;
&lt;h1&gt;{{.PageTitle}}&lt;/h1&gt;
&lt;ul&gt;
{{range .Users}}
&lt;input type=&quot;text&quot; data-permissions={{.Flags}} data-authorized=&quot;no&quot;&gt;{{.Title}}&lt;/s&gt;
{{end}}
&lt;/ul&gt;
&lt;script&gt;
var flags = [&quot;admin&quot;, &quot;super user&quot;]
var elements = document.querySelectorAll(&quot;input&quot;);
elements.forEach((element, index, array) =&gt; { 
if(element.hasAttribute(&quot;data-permissions&quot;)){
console.log(element.dataset.permissions)
var perm = element.dataset.permissions.split(&quot; &quot;);
var found = false;
for (var i = 0; i &lt; perm.length; i++) {
if (flags.indexOf(perm[i]) &gt; -1) {
element.dataset.authorized = &quot;yes&quot;
element.removeAttribute(&quot;data-permissions&quot;)
break;
}
} 
}
});
&lt;/script&gt;

And here is the output:
无法从模板中操作DOM以添加/删除属性。

Any thought?

答案1

得分: 0

我能够做到这一点,借助模板自定义函数的帮助,下面是完整的代码:

package main

import (
	"html/template"
	"net/http"
)

type User struct {
	Flags []flag //string
	Title string
}

type UsersPageData struct {
	PageTitle string
	Users     []User
}

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
	Dummy
)

func subtract(arg1, arg2 int) int {
	return arg1 - arg2
}

func helloHandler(name string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// 在模板中映射 `subtract` 自定义函数
		funcMap := map[string]interface{}{"subtract": subtract}

		master := []flag{Admin, Editer, Superuser, Viewer}
		admin := []flag{Admin, Superuser, Viewer}
		user := []flag{Viewer, Dummy}

		tmpl := template.New("").Funcs(template.FuncMap(funcMap))
		template.Must(tmpl.ParseFiles("index.html"))
		data := UsersPageData{
			PageTitle: "Users list: ",
			Users: []User{
				{Flags: master, Title: "Everything"},
				{Flags: admin, Title: "Administrator"},
				{Flags: user, Title: "Normal user"},
			},
		}
		tmpl.ExecuteTemplate(w, "index.html", data)
	})
}

func main() {
	fs := http.StripPrefix("/www/", http.FileServer(http.Dir("./www")))
	http.Handle("/www/", fs)

	http.Handle("/", helloHandler("John"))
	http.ListenAndServe(":8080", nil)
}

index.html 文件内容如下:

<html>
{{/* 这是一个注释 
{{$flags := []flag{Admin, Editer, Superuser, Viewer};}}	
    Admin Flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
    }}
*/}}

<ul>
    {{range .Users}}
        <span>{{.Title}}</span>
        {{ $done := false}} {{$length := len .Flags}}
        {{range $i, $v := .Flags}}
            {{ if $done }}
            {{ else }}
                {{if or (eq $v 1) (eq $v 3)}} 
                    <input type="text" name="subject" placeholder= {{$v}} required>
                    {{ $done = true }}
                {{else}}
                    {{ if eq $i (subtract $length 1)}}
                        <input type="text" name="subject" placeholder= {{$v}} disabled>
                    {{ end }}
                {{end}}
            {{end}}
        {{end}}
    {{end}}
</ul>
</html>

无法从模板中操作DOM以添加/删除属性。

英文:

I was able to do it, with the help of template custom functions, below the full code:

package main

import (
	&quot;html/template&quot;
	&quot;net/http&quot;
)

type User struct {
	Flags []flag //string
	Title string
}

type UsersPageData struct {
	PageTitle string
	Users     []User
}

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
	Dummy
)

func subtract(arg1, arg2 int) int {
	return arg1 - arg2
}

func helloHandler(name string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Map the `subtract` custom function in the template
		funcMap := map[string]interface{}{&quot;subtract&quot;: subtract}

		master := []flag{Admin, Editer, Superuser, Viewer}
		admin := []flag{Admin, Superuser, Viewer}
		user := []flag{Viewer, Dummy}

		tmpl := template.New(&quot;&quot;).Funcs(template.FuncMap(funcMap))
		template.Must(tmpl.ParseFiles(&quot;index.html&quot;))
		data := UsersPageData{
			PageTitle: &quot;Users list: &quot;,
			Users: []User{
				{Flags: master, Title: &quot;Everything&quot;},
				{Flags: admin, Title: &quot;Administrator&quot;},
				{Flags: user, Title: &quot;Normal user&quot;},
			},
		}
		tmpl.ExecuteTemplate(w, &quot;index.html&quot;, data)
	})
}

func main() {
	fs := http.StripPrefix(&quot;/www/&quot;, http.FileServer(http.Dir(&quot;./www&quot;)))
	http.Handle(&quot;/www/&quot;, fs)

	http.Handle(&quot;/&quot;, helloHandler(&quot;John&quot;))
	http.ListenAndServe(&quot;:8080&quot;, nil)
}

The index.html is:

&lt;html&gt;
{{/* This is a comment 
{{$flags := []flag{Admin, Editer, Superuser, Viewer};}}	
    Admin Flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
    }}
*/}}

&lt;ul&gt;
    {{range .Users}}
        &lt;span&gt;{{.Title}}&lt;/span&gt;
        {{ $done := false}} {{$length := len .Flags}}
        {{range $i, $v := .Flags}}
            {{ if $done }}
            {{ else }}
                {{if or (eq $v 1) (eq $v 3)}} 
                    &lt;input type=&quot;text&quot; name=&quot;subject&quot; placeholder= {{$v}} required&gt;
                    {{ $done = true }}
                {{else}}
                    {{ if eq $i (subtract $length 1)}}
                        &lt;input type=&quot;text&quot; name=&quot;subject&quot; placeholder= {{$v}} disabled&gt;
                    {{ end }}
                {{end}}
            {{end}}
        {{end}}
    {{end}}
&lt;/ul&gt;
&lt;/html&gt;

无法从模板中操作DOM以添加/删除属性。

huangapple
  • 本文由 发表于 2021年11月8日 20:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69883574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定