Search code examples
yuiyui-datatable

How to display images in datatable?


How to display an image instead of a bool value in a cell in datatable? The datatable is associated with a datasource.


Solution

  • You can put any HTML into your cells by using a custom formatter function. Your Column definition might look like this:

    var myColumnSet = [
        {
            key: 'active_employee',
            label: 'Active',
            formatter: function(el, oRecord, oColumn, oData) {
                // el is the HTMLElement of the current cell
                // oRecord gives you access to other fields from the
                //    DataSource, e.g.: oRecord.getData('full_name')
                // oData is the value of the current field (active_employee)
    
                if (oData) {
                    el.innerHTML = '<img src="/images/active.png">';
                } else {
                    el.innerHTML = '<img src="/images/not-active.png">';
                }
            }
        },
        // other Columns....
    ];
    

    Also see the Custom Cell Formatting example.