JSFiddle - React, Tailwind, and code Playground

by rich_harris

HTML

<script src="http://cdn.ractivejs.org/edge/ractive.js"></script>
<main></main>

<script id='template' type='text/ractive'>
    <table>
        <tr>
            <th>name</th>
            <th>id</th>
            <th>number</th>
        </tr>
        
        {{#each items}}
            <tr>
                <td>{{name}}</td>
                <td>{{id}}</td>
                <td>{{number}}</td>
            </tr>
        {{/each}}
    </table>
</script>

CSS

body {
    font-family: 'Helvetica Neue', arial, sans-serif;
    font-weight: 200;
    color: #353535;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 200;
}

.error {
    color: #d00;
    font-family: monospace;
}

th, td {
    text-align: left;
    padding: 0.5em;
    border-bottom: 1px solid #eee;
}

JavaScript

var ractive = new Ractive({
    el: 'main',
    template: '#template',
    data: {
        items: [
            { id: 'abc', number: 47, name: 'aardvark' },
            { id: 'bce', number: 92, name: 'bear' },
            { id: 'def', number: 16, name: 'cougar' },
            { id: 'efg', number: 31, name: 'duck' },
            { id: 'fgh', number: 98, name: 'elephant' }
        ]
    }
});

// using an array method
setTimeout( function () {
    ractive.get( 'items' ).forEach( function ( item, i ) {
        if ( item.id === 'bce' ) {
            ractive.set( 'items[' + i + '].number', 123 );
        }
    });
}, 1000 );

// using a loop (quicker, because you stop iterating 
// once you find the item, and there's no function overhead
setTimeout( function () {
    var items = ractive.get( 'items' );
    var i = items.length;

    while ( i-- ) {
        if ( items[i].id === 'efg' ) {
            ractive.set( 'items[' + i + '].number', 234 );
            break;
        }
    }
}, 2000 );