GIN Web框架DataTable jQuery插件:无法设置未定义的属性'_DT_CellIndex'

问题描述

我有以下Gin Web框架代码,其中使用了dataTable jQuery插件。我的问题与{{.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}},我得到以下信息:

不幸的是,我收到以下错误消息

jquery.min.js:2 Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined
    at Za (jquery.dataTables.min.js:41)
    at ea (jquery.dataTables.min.js:32)
    at HTMLTableRowElement.<anonymous> (jquery.dataTables.min.js:33)
    at jquery.min.js:2
    at Function.map (jquery.min.js:2)
    at S.fn.init.map (jquery.min.js:2)
    at Ga (jquery.dataTables.min.js:33)
    at e (jquery.dataTables.min.js:109)
    at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:110)
    at Function.each (jquery.min.js:2)

任何帮助对我来说都是方便的!

致谢

解决方法

问题在于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中。
然后,请将其作为模板提供。