Manor JS Table Striping

I started using "Mustache JS" a few hours ago. I wonder if there is a way to add a class to tr to make the table striped. I know that I can determine if the value of a variable is null or undefined, but it is not. The variable will always have a value. Is there any way to do this?

I got the following code:

Template:

template = "\
        {{#tr}}\
            <tr>\
                <td>{{tit}}</td>\
                <td>{{ord}}</td>\
                <td>{{prazo}}</td>\
                <td>{{id}}</td>\
            </tr>\
        {{/tr}}";

View:

view = {
                "tr" : [
                    {
                        id : val.ID, 
                        ord : val.Ordem, 
                        prazo : val.Prazo, 
                        tit : val.Título
                    }
                ]
            };

I know that I can set a function in a view. But dunno, how to properly install the mod inside it, for alternation. Any help would be great.

+5
source share
1 answer

I did it like this:

Template:

template = "\
        {{#tr}}\
            <tr class="{{even_or_odd}}">\
                <td>{{tit}}</td>\
                <td>{{ord}}</td>\
                <td>{{prazo}}</td>\
                <td>{{id}}</td>\
            </tr>\
        {{/tr}}";

View: (something like this if you can set the value for iteratorValue)

var interatorValue == 0;
// your other code
view = {
                "tr" : [
                    {
                        id : val.ID, 
                        ord : val.Ordem, 
                        prazo : val.Prazo, 
                        tit : val.Título,
                        even_or_odd: ((iteratorValue % 2 == 0) ? 'even' : 'odd')
                    }
                ]
            };
iteratorValue++;

Then add css for tr.oddand tr.even.

+4
source

All Articles