Headers and Footers

To enable headers and footers for a Datatable, use the header and footer parameters:

{
    view:"datatable",
    header:true, // by default
    footer:true,
    // other config
}

You can configure headers and footers with the attributes header and footer in the columns parameter.

A header can be:

  1. single line;
  2. multiline;
  3. single line or multiline with colspans (rowspans) and built-in filters;

A footer can be:

  1. Single-line;
  2. Multiline footer;
  3. single line or multiline with colspans (rowspans) and built-in counters;

Additionally, both header and footer can be rotated and can be of any height.

Webix Tutorials

To try your hand, you can go to Webix Tutorials.

Single line header

To define a simple (single line) header, you should set it as a string.

A string can contain:

  • text;
  • images;
  • html inputs;
  • links.

Defining a single line header

columns:[
    // header as a button
    {
        id:"col1",
        header:"<input type='button' value='Save' class='dummy_button'>"
    },
    // header as an image
    { id:"col2", header:"<img src='../common/imdb.png'>"},
    // header as a text label
    { id:"col1", header:"Year"},
    // header as a link
    { id:"col4", header:"<a href='http://google.com'>Link</a>"}
]

Related sample:  Different Types of Content in the Header

Multiline header

To create a multiline header, define it as an array of strings.

Defining a multiline header

columns:[
    { id:"rank",  header:["#"] },
    { id:"title", header:["Film title", "Second line"] },
    { id:"year",  header:["Year"] },
    { id:"votes", header:["Votes"] }
]

Related sample:  Multiline Header

Complex header with colspans (rowspans) and built-in filters

To make a header span across several columns or rows, define a header as an object or an array of objects. Header-objects can have the following properties:

  1. text - the text on the header;
  2. colspan - the number of columns a header should span;
  3. rowspan - the number of rows a header should span;
  4. content - functional header content (built-in filters , group definition, master checkboxes, etc);
  5. rotate - if true, rotates a header;
  6. height - changes the height of a header line;
  7. css - the name of a CSS class that will be applied to the column header.

Colspan

To create a header cell that will span across 2 or more columns, set its colspan property:

Using colspans

columns:[
    {
        id:"title",
        header:[
            "Film title",
            { text:"Sub-header", colspan:3 }
        ]
    },
    { id:"year",  header:["Year", ""]},
    { id:"votes", header:["Votes", ""]}
]

Related sample:  Colspans and Rowspans in the Header

Rowspan

To create a header cell that will span across 2 or more rows, set its rowspan property:

Using rowspans

columns:[
    {
        id:"title",
        header:[
            { text:"Film title", rowspan:2 }
        ]
    },
    { id:"year",  header:["Year", ""]},
    { id:"votes", header:["Votes", ""]}
]

Related sample:  Colspans and Rowspans in the Header

Built-in filters

To put a filter into the header, set the name of the filter in the content property:

Using filters in the header

columns:[
    {
        id:"title",
        header:[
            "Film title",
            { content:"textFilter" }
        ]
    },
    { id:"year",  header:"Released"},
    { id:"votes", header:"Votes"}
]

Related sample:  Filters in the Header

Read more about Filters and try your hand in Webix Tutorials.

Header Menu

Available in Webix Pro edition only.

The built-in header menu allows the user to hide and show specific columns. This is how you can add a header menu:

{ view:"datatable", columns:[...], headermenu:true }

Related sample:  Header Menu in the DataTable

Read more about Datatable Header Menu.

Single line footer

By default, footers are disabled. So, before configuring them for specific columns, don't forget to enable them in datatable settings:

Enabling footers in DataTable

{
    view:"datatable",
    ...
    footer:true
}

Footers are configured the same way as headers:

Using footers

columns:[
    // footer as a button
    {id:"col1", footer:"<input type='button' value='Do something' class='dummy_button'>"},
    // footer as an image
    {id:"col2", footer:"<img src='../common/imdb.png'>"},
    // footer as a text label
    {id:"col1", footer:"Year"},
    // footer as a link
    {id:"col4", footer:"<a href='http://google.com'>Link</a>"}
]

Related sample:  Standard Header (Footer) Implementation

Multiline footer

To make a footer multiline, define it as an array:

Defining a multiline footer

columns:[
    { id:"rank",  footer:["Total", "Second line"]},
    { id:"title", footer:[""]},
    { id:"year",  footer:[""]},
    { id:"votes", footer:["12547"]}
]

Complex footer containing colspans/rowspans

Complex footers must be defined as objects with the following parameters:

  1. text - the text on a footer;
  2. colspan - the number of columns a footer cell should span;
  3. rowspan - the number of rows a footer cell should span;
  4. content - the built-in functor of a column (summColumn);
  5. rotate - if true, rotates a footer;
  6. height - changes the height of a footer line;
  7. css - the name of a CSS class that will be applied to the column footer.

Colspans in footer

columns:[
    {
        id:"rank",
        footer:{ text:"Total", colspan:3 }
    },
    { id:"title", ...},
    { id:"year", ...},
    { id:"votes", footer:"12547"}
]

Sum counter in the footer

You can add a counter that will sum up all values in the column and display the result in the footer. The counter is called summColumn, and you should set the name to the content parameter:

Using the sum counter in the footer

columns:[
    {
        id:"rank",
        header:"",
        footer:{ text:"Total:", colspan:3 }
    },
    { id:"title", header:"Film title" },
    { id:"year",  header:"Released" },
    {
        id:"votes",
        header:"Votes",
        footer:{ content:"summColumn" }
    }
]

Related sample:  Counter in the Footer

Rotated header/footer

Available in Webix Pro edition only.

To rotate a header or a footer, specify it as an object (or array of objects if it's a multiline header/footer). The height will be adjusted to text size.

columns:[
    {   id:"year",
        header:{ text:"Released", rotate:true },
        footer:{ text:"Footer", rotate:true }
    }
]

Related sample:  Header Rotate

Custom Height for Header and Footer

To change the height, define a header/footer line as an object and use the height property:

columns:[
    {
        id:"title",
        header:{ text:"Film title", height:25 },
        footer:{ text:"80px footer", height:80 }
    }
]

Related sample:  Custom Height for Header Lines

Autoheight for Header and Footer

You can also enable autoheight for the header and the footer of DataTable. The height will be adjusted to the content.

For this, you need to set a header or a footer as an object and set autoheight:true:

columns:[
    {
        id:"title",
        header:{
            text:"A multiline header for a film title column..",
            autoheight:true
        },
        footer: {
            text:"A multiline footer for a film title column..",
            autoheight:true
        },
    }
]

Related sample:  Auto Height for Header Lines

This setting also works for rotated header/footer lines:

columns:[
    { id:"title"},
    { id:"year",  header:{ text:"Released", autoheight:true, rotate:true }},
    { id:"votes", header:{ text:"Votes", rotate:true}}
]

Related sample:  Header Rotate::Autoheight

Custom Header and Footer Content

All content elements for the datatable header or footer are stored in a webix.ui.datafilter object. They all have the same configuration pattern.

They all have 2 methods:

  • refresh() that provides logic,
  • render() that draws an element.

Their parameters include:

  • master - component object (here: datatable);
  • column - related column object;
  • node - the HTML element (here: header td).

You can either extend or redefine the functionality of any existing content element or create a new element.

Extending Existing Content Element

There is summColumn, the counter that can be added to a column footer. The counter will sum up all values in the column and display the result.

To create your own counter/aggregator, you can extend summColumn and redefine its refresh() method. For example, lets create avgColumn that will count the average value:

webix.ui.datafilter.avgColumn = webix.extend({
    refresh:function(master, node, value){
        var result = 0;
        master.mapCells(null, value.columnId, null, 1, function(value){
            value = value * 1;
            if (!isNaN(value))
                result += value;
            return value;
        });
 
        node.firstChild.innerHTML = Math.round(result / master.count());
    }
}, webix.ui.datafilter.summColumn);

Creating Custom Content Element

Any content element must have the following methods (declare them even if you don't need any specific performance from them):

  • getValue() - gets the current value of an element;
  • setValue() - sets the value of an element;
  • render() - paints an element;
  • refresh() - defines dynamic performance of an element.

You can also define other methods and properties and call events.

webix.ui.datafilter.customFilterName = {
    getValue:function(){}, //not used methods
    setValue: function(){},
    refresh: function(master, node, column){
        //event handlers
        node.onchange = function(){...};
        node.onclick = function(){...}
    },
    render:function(master, column){
        var html = "...";
        return html;
    }
};

A content element can have the following properties:

  • trackCells (boolean) - indicates whether an element should be refreshed each time component data changes (if true, the refresh() method of a content element will be called automatically).

Related sample:  Custom Content for Header and Footer

Adding Custom Filter into the Header

To embed a filter into the header of the datatable, you need to implement the following steps:

  • set a new filter into the datafilter object to integrate it with the header and add an input for it
  • set the getValue() method that will return the value of the input
  • use the refresh() method to provide the filtering logic in dynamics. Inside of the method, you need to register the added filter and attach a handler that will trigger the filterbyAll() method
  • apply the render() method to draw the newly created filter.
webix.ui.datafilter.customFilterName = {
    getInputNode:function(node){
        return node.firstChild ? node.firstChild.firstChild : { value:"" };
    },
    setValue:function(node, value){
        this.getInputNode(node).value = value.toString();
    },
    // returns filtering parameter
    getValue:function(node){
        return this.getInputNode(node).value;
    },
    refresh: function(master, node, columnObj){
        // declare a built-in datatable filter
        master.registerFilter(node, columnObj, this);
        // event which will trigger filtering
        node.querySelector("input").onkeyup = function(){
            master.filterByAll()
        }
    },
    render:function(master, column){
        var html = "<input type='text' style='width:100%' id='cb1'>";
        return html;
    }
};
webix.ui({
    view:"datatable",
    columns:[
        {
            id:"title",
            header:[ { content:"customFilterName" } ],
            fillspace:1
        },
        {
            id:"year",
            header:[ { content:"textFilter" } ],
            fillspace:1
        }
    ],
    data:grid_data
});

In the above example we have added two filters into the datatable header: a custom filter and a standard text filter. Data will be refiltered each time the value in any of the filters changes.

Back to top
If you have not checked yet, be sure to visit site of our main product Webix ui component library and page of javascript data grid product.