最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Kendo UI Grid cell background color - Stack Overflow

programmeradmin4浏览0评论

In kendo ui grid how can i change the background color? I've tried to set the template for the column but when the grid is visualized, it's empty without any color. I have this code:

$scope.thingsOptions = {
    sortable: "true",
    scrollable: "true",
    columns: [
              { field: "Colore", title: "Colore", width: "160px", template: "<div><span style='background-color:red'></span></div>" }
             ]

};

How can i apply a template to color the background of the cell

In kendo ui grid how can i change the background color? I've tried to set the template for the column but when the grid is visualized, it's empty without any color. I have this code:

$scope.thingsOptions = {
    sortable: "true",
    scrollable: "true",
    columns: [
              { field: "Colore", title: "Colore", width: "160px", template: "<div><span style='background-color:red'></span></div>" }
             ]

};

How can i apply a template to color the background of the cell

Share Improve this question asked May 26, 2014 at 13:50 TomTom 4,08725 gold badges72 silver badges107 bronze badges 1
  • a span is an inline element, so without content, it'll have 0 width; take a look at this answer: stackoverflow.com/a/20287263/2001735 – Lars Höppner Commented May 26, 2014 at 21:42
Add a comment  | 

4 Answers 4

Reset to default 8

how to add conditional cell background?

what it does is: it builds a text row template from all parameters if there is no row template given. it is possible to add template text in most of the parameters like:

...
//},
    columns : [  
            {
                title : 'Questions Translated',
                attributes:{ 'style':"#=questions!==questionsMax?'background-color: red; color:white;':''#" },
                field : "questions",
                width: 140
            },

...

Your code is essentially fine. You can do it as you are doing but the you are not seeing it because the span and the div are empty so the element has 0px width and you cannot see it.

Try doing:

$scope.thingsOptions = {
    sortable: "true",
    scrollable: "true",
    columns: [
        { 
            field: "Colore", 
            title: "Colore", 
            width: "160px", 
            template: "<div><span style='background-color:red'>This is a test</span></div>" 
        }
    ]
};

or

$scope.thingsOptions = {
    sortable: "true",
    scrollable: "true",
    columns: [
        { 
            field: "Colore", 
            title: "Colore", 
            width: "160px", 
            template: "<div style='background-color:red'>&nbsp;</div>"
        }
    ]
};

or even:

$scope.thingsOptions = {
    sortable: "true",
    scrollable: "true",
    columns: [
        { 
            field: "Colore", 
            title: "Colore", 
            width: "160px", 
            template: "<span style='float: left; width: 100%; background-color:red'>&nbsp;</div>"
        }
    ]
};

It is important to note that the content of the span and/or div are not empty: they contain a &nbsp;.

BUT if you want it colored and no padding / margin, then you can use:

{ 
    field: "Colore", 
    title: "Colore", 
    width: "160px", 
    attributes: {
        style: "background-color: red"
    }
}

If you want to color the whole column, you can add attributes property in the column specification.

For example:

columns: [
    { 
        field: "Colore", 
        title: "Colore", 
        width: "160px", 
        attributes: {
            "class": "weekend"
        }
    }
]

And in your .css file you specify the weekend class as:

.weekend{
    background-color: #F7DCAA;
}

More info here

You can use the [footerStyle] and [headerStyle] attributes to change the grid color, for example

 <kendo-grid [data]="gridData">
        <kendo-grid-column
          field="ContactName"
          title="Contact Name"
          [width]="150"
          [headerStyle]="{'background-color': '#666','color': '#fff','line-height': '1em'}"
          [style]="{'background-color': '#888','color': '#fff'}"
          [footerStyle]="{'background-color': '#888','color': '#fff'}"
        >
          <ng-template kendoGridFooterTemplate>Contacts: 10</ng-template>
        </kendo-grid-column>
        <kendo-grid-column
          field="CompanyName"
          title="Company Name"
          [headerStyle]="{'background-color': '#666','color': '#fff','line-height': '1em'}"
        >
        </kendo-grid-column>
        <kendo-grid-column
          field="City"
          title="City"
          [headerStyle]="{'background-color': '#666','color': '#fff','line-height': '1em'}"
        >
        </kendo-grid-column>
    </kendo-grid>
发布评论

评论列表(0)

  1. 暂无评论