【问题标题】:GIN Web Framework (DataTable jquery plugin : Cannot set property '_DT_CellIndex' of undefined)GIN Web 框架(DataTable jquery 插件:无法设置未定义的属性“_DT_CellIndex”)
【发布时间】:2020-11-10 21:22:22
【问题描述】:

我有以下使用 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 }} 我会得到以下错误:

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

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)

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

问候

【问题讨论】:

    标签: go datatables-1.10


    【解决方案1】:

    问题在于 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 中插入一个切片字符串。
    那么请将其作为模板提供。

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 2020-10-21
      • 2019-10-14
      • 2018-06-28
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2018-09-18
      相关资源
      最近更新 更多