我有以下使用 dataTable jQuery 插件的 Gin Web 框架代碼。我的問題與 {{ .SliceDescription }} 有關,之后我在我的模型中定義它:<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 定義我的數據庫模型: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 }} 則沒有任何錯誤,但如果我放置 {{ .SliceDescription }} 我會得到以下錯誤
1 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
問題是 SliceDescription 是一個函數。
將 SliceDescription 添加到 Todo 結構中,如下所示。
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
}
請在 Todo 的 SliceDescription 中插入一個切片字符串。
那么請將其作為模板提供。
- 1 回答
- 0 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消