我有以下使用 dataTable jQuery 插件的 Gin Web 框架代碼。我的問題與 {{ .SliceDescription }} 有關(guān),之后我在我的模型中定義它:<table id="example" class="table table-striped table-bordered" style="width:100%"> <thead> <tr> <th>Τ?τλο?</th> <th>Περιγραφ?</th> <th>Επισκ?πηση</th> <th>Ενημ?ρωση</th> <th>Διαγραφ?</th> </tr> </thead> <tbody> {{range .todo}} <tr> <td>{{ .Title }}</td> **<td>{{ .SliceDescription }}</td>** <td><a href="/tasks/todo/{{ .ID }}" class="btn btn-success btn-sm viewlink" role="button" data-toggle="modal" data-target="#viewModal"><i class="fas fa-eye"></i></a> </td> <td><a href="/tasks/todo/{{ .ID }}" class="btn btn-warning btn-sm" role="button"><i class="fas fa-edit"></i></a></td> <td><a href="/tasks/todo/{{ .ID }}" class="btn btn-danger btn-sm deletelink" role="button"><i class="fas fa-trash-alt"></i></a></td> </tr> {{end}} </tbody> <tfoot> <tr> <th>Τ?τλο?</th> <th>Περιγραφ?</th> <th>Επισκ?πηση</th> <th>Ενημ?ρωση</th> <th>Διαγραφ?</th> </tr> </tfoot> </table>使用 jquery 代碼: $(document).ready(function() { $('#example').DataTable(); });我通過 GIN WEB FRAMEWORK 中的 GORM 定義我的數(shù)據(jù)庫(kù)模型:type Todo struct { ID uint `gorm:"primary_key;AUTO_INCREMENT" ` Title string `gorm:"not null" json:"title" ` Description string `gorm:"not null" json:"description" `}func (b Todo) SliceDescription() string { return string(b.Description[:45]) }func (b *Todo) TableName() string { return "todo"} 如果我放置 {{.Description}} 而不是 {{ .SliceDescription }} 則沒有任何錯(cuò)誤,但如果我放置 {{ .SliceDescription }} 我會(huì)得到以下錯(cuò)誤
1 回答

繁花如伊
TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
問題是 SliceDescription 是一個(gè)函數(shù)。
將 SliceDescription 添加到 Todo 結(jié)構(gòu)中,如下所示。
type Todo struct {
ID uint `gorm:"primary_key;AUTO_INCREMENT" `
Title string `gorm:"not null" json:"title" `
Description string `gorm:"not null" json:"description" `
SliceDescription string
}
請(qǐng)?jiān)?Todo 的 SliceDescription 中插入一個(gè)切片字符串。
那么請(qǐng)將其作為模板提供。
- 1 回答
- 0 關(guān)注
- 140 瀏覽
添加回答
舉報(bào)
0/150
提交
取消