Mustache JS Table Striping

javascript, mustache, templates

Solution

I have done this like this:

The template:

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

The View: (something like this, assuming you can set a 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.odd` and `tr.even`.

Problem

I have started to use "Mustache JS" some hours ago. I wonder if there a way to add a class in a tr in order to make the table striped. I know I can detect if the variable's value is null or undefined, but this is not the case. There will always be a value in the variable. Is there any way to do this? I got the following code up and running: The template: ``` template = "\ {{#tr}}\ <tr>\ <td>{{tit}}</td>\ <td>{{ord}}</td>\ <td>{{prazo}}</td>\ <td>{{id}}</td>\ </tr>\ {{/tr}}"; ``` The View: ``` view = { "tr" : [ { id : val.ID, ord : val.Ordem, prazo : val.Prazo, tit : val.Título } ] }; ``` I´m aware that I can set a function in the view. But dunno how to properly set a mod inside it , for the striping. Any help would be great.

Original source