Customize headers for a kendo grid using Knockout-Kendo.js for ko binding

9.7k views Asked by At

I try to use a kendo grid with knockout binding and Knockout-Kendo.js library

It is defined as below:

<div data-bind="kendoGrid:
                {
                    data: SearchResult,
                    rowTemplate: 'rowTmpl',
                    altRowTemplate: 'altTmpl',
                    useKOTemplates: true
                }"> 
</div> 

<script id="rowTmpl" type="text/html">
    <tr class="tdText" role="row">
        <td >
            <a data-bind="attr: { href: 'scrccc_checkEdit.aspx?id=' + CheckID }" >
                <img src="images/icon-edit.gif" border="0" alt="Edit/View Check" />
            </a>
        </td>
        <td data-bind="text: CheckNumber"></td>
        <td data-bind="text: new Date(CreateDate).MMddyyyy()"></td>
        //...
        <td data-bind="text: ParishName">                                        
    </tr>
</script>
<script id="altTmpl" type="text/html">
   //....

The data loaded from the REST service has more columns that I want to be shown in grid The row looks ok, due to template, but the problem is with the grid header, create columns for every field in source.

How can I hide some columns in header, and customize their header label (change column width, header label and eventually allow additional customization .

For example, in image above I want Co

3

There are 3 answers

1
RP Niemeyer On BEST ANSWER

There is currently a specific option to specify the header template directly. What you could do is specify a set of columns and their headers directly either by using the title or headerTemplate options like:

this.gridOptions = {
    data: this.items,
    rowTemplate: "rowTmpl",
    useKOTemplates: true,
    columns: [ 
        {
            title: "My ID"
        },
        {
            headerTemplate: "<strong>Name Edit</strong>"   
        },
        {
            title: "Name Value"   
        }
    ]
}; 

Sample: http://jsfiddle.net/rniemeyer/yjYMK/

0
Lars Höppner On

You can make the customizations you need by providing the grid with individual column definitions. With those, you can set the width, provide a headerTemplate, hide columns:

columns: [{
    field: "FieldName",
    title: "Contact Name",
    headerTemplate: "This will be shown in the header",
    template: "This will be shown in the column",
    hidden: true,
    width: 140
}]
0
camloken On

Also worth noting that you can apply bindings to your templates. The following example uses a knockout binding:

columns: [{
    field: 'FieldName',
    headerTemplate: '<span data-bind="text:headerName"></span>',
    template: 'This will be shown in the column'
}]