Polymer, paper-datatable styling of rows

蓝咒 提交于 2019-12-11 13:25:42

问题


I'm trying to colour the rows of paper-datatable using the attribute customRowStyle

This Plunk of paper-datatable is working, rows are colored, but it's not enclosed as separate Polymer element.

I need to enclose paper-datatable in separate element.

Need some help to fix this: how to make customRowStyle(item) to get called on table render and pass the item?

 <paper-datatable data="{{data}}" 
                         custom-row-style="{{generateRowCss}}"
                         on-row-tap="row_tap">

            <paper-datatable-column header="title" property="title"></paper-datatable-column>
            <paper-datatable-column header="Calories" property="calories"></paper-datatable-column>
            <paper-datatable-column header="Fat (g)" property="fat" ></paper-datatable-column>

</paper-datatable>

...

generateRowCss: function (item) {
                console.log('theming_2 generateRowCss:');
                var levels = ['#FFFFFF', '#FFEBEE', '#FFCDD2', '#EF9A9A'];
                var min = 150;
                var max = 450;
                var level = Math.floor((item.calories - min) / (max - min) * levels.length);
                return 'background:' + levels[level] + ';';
},

EDIT: Plunk with @a1626 solution.


回答1:


As generateRowCssthat is passed to customRowStyle is a function rather than the return value of the function(which is what your code is passing) you'll have to do something like this. Instead of creating a function generateRowCss create a property with the same name, initialize it as Object and return its value as whole function

properties: {
            data: {
                type: Array,
                notify: true,
                value: [
                    {id: 0, title: 'Frozen yogurt', calories: 159, fat: 6},
                    {id: 1, title: 'Ice cream sandwich', calories: 237, fat: 9},
                    {id: 2, title: 'Eclair', calories: 262, fat: 16},
                    {id: 3, title: 'Cupcake', calories: 305, fat: 3.7},
                ],
    },
    generateRowCss:{
      type:Object, //this is optional you can skip this also
      value:function(){
        return function(item){
                        console.log('app.generateRowCss');
                        console.log(item);
            var levels = ['#FFFFFF', '#FFEBEE', '#FFCDD2', '#EF9A9A'];
            var min = 150;
            var max = 450;
            var level = Math.floor((item.calories - min)/(max-min)*levels.length);
                        console.log(level);
                        console.log('background:'+levels[level]+';');
            return 'background:'+levels[level]+';';
        }  
      }

    }
  },

Pasted above are the properties of your custom element. Here is the working plunkr



来源:https://stackoverflow.com/questions/37927817/polymer-paper-datatable-styling-of-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!