首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

JSP/JS 生成表格列添加超鏈接:EasyUI DataGrid formatter 實現

     編輯:程式設計綜合問答

題主使用 EasyUI DataGrid,需在 `columns` 定義中為 `NAME` 列添加超鏈接。

formatter 函數實現

columns: [[
    { title: '編碼', field: 'ID', width: 100, align: 'left' },
    { 
        title: '名稱', 
        field: 'NAME', 
        width: 150, 
        align: 'left',
        formatter: function(value, row, index) {
            // row 包含整行數據,可用 row.ID、row.categoryID 等
            return '' + value + '';
        }
    },
    { title: '發布時間', field: 'newsCreateTime', width: 100, align: 'center' }
]]

關鍵點

  • `formatter` 參數:`value`(當前列值)、`row`(整行對象)、`index`(行索引)。
  • 返回 **HTML 字符串**,DataGrid 直接渲染,支持任意標簽 (`
  • 若需根據行數據動態決定鏈接,利用 `row` 字段拼接 URL。

進階:事件綁定而非 href

formatter: function(value, row, index) {
    return '' + value + '';
},
onLoadSuccess: function(data) {
    $('.detail-link').click(function() {
        var id = $(this).data('id');
        // 單頁應用路由跳轉或彈窗
        openDetail(id);
    });
}

避免整頁跳轉,提升體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved