How to make datatable row or cell clickable?

前端 未结 5 1521
梦谈多话
梦谈多话 2020-12-02 17:10

I\'m working on a history development of a particular user and I want it to be done with dataTables. However, I cannot find the way with which I can make my row or a particu

5条回答
  •  天命终不由人
    2020-12-02 17:43

    We are using:

      rowCallback: function (row: Node /*, data: any[] | Object, index: number */) {
    
        // Intercept clicking of datatable links to avoid a full page refresh
        $('a', row).click(function (e) {
          e.preventDefault()
          // const href = $(this).attr('href')
          // parent.router.navigate([href])
        })
    
        // Navigate using anchor in cell to make entire cell clickable
        $('td', row).click(function (/* e */) {
          const anchor = $(this).find('a:first')[0]
          if (anchor) {
            const href = $(anchor).attr('href')
            parent.router.navigate([href])
          }
        })
    
        return row
      }
    

    Not sure this is the best approach but it does the job. May the Lord bless you :)

    Apologies this is TypeScript but its dead simple to convert to JS.

提交回复
热议问题