Tabulator - Add menu button to column header

后端 未结 1 1161
不思量自难忘°
不思量自难忘° 2020-12-21 18:59

I am fairly new to JavaScript and am currently learning how to work with Tabulator (which is working quite nicely aside from this problem).

I want to add a menu but

相关标签:
1条回答
  • 2020-12-21 19:25

    Update

    As of Tabulator 4.7, there is now built in functionality for adding header menus to columns using the new headerMenu option:

    //define row context menu
    var headerMenu = [
        {
            label:"Hide Column",
            action:function(e, column){
                column.hide();
            }
        },
    ]
    
    //add header menu in column definition
    var table = new Tabulator("#example-table", {
        columns:[
            {title:"Name", field:"name", width:200, headerMenu:headerMenu}, //add menu to this column header
        ]
    });
    

    For full details on the available functionality, checkout the Menu Documentation

    Original Answer

    You should use a titleFormatter to do this, Trying to manipulate elements inside tabulator from outside the table is very dangerous, because Tabulator uses a virtual DOM elements can be replaced without notice, causing any bindings to plugins to be lost with them.

    It is also bad practice to have multiple elements on the same page with the same ID, which is the case with the above solution. element id's should be unique on a page

    In this case your title formatter would look something like this:

    var menuTitleFormatter = function(cell, formatterParams, onRendered){
    
        //build dropdown
        var dropdown =  document.createElement("div");
    
        dropdown.classList.add("dropdown");
        dropdown.classList.add("ui");
    
        dropdown.innerHTML = "<i class='bars icon'></i><div class='menu'><div class='header'>Options</div><div class='item' class='sort1'>Sort</div><div class='item' class='group1'>Group</div></div>";
    
        //create dropdown
        $(dropdown).dropdown();
    
        $('.group1', $(dropdown)).click(function(){
            //do something when the item is clicked
        });
    
    
        //set header title
        var title = document.createElement("span");
        title.innerHTML = cell.getValue();
    
        //add menu to title
        title.appendChild(dropdown);
    
    
        return title;
    }
    

    You could the apply it to a column in its definition

    {title:"Name", field:"name", titleFormatter:menuTitleFormatter },
    
    0 讨论(0)
提交回复
热议问题