Search code examples
ractivejs

Sort by type in ractive


I come across the tutorial part: http://learn.ractivejs.org/list-sections/5/

Made attempts with select and sort.

Ractive template

<th class='sortable {{ sortColumn === type ? "sorted" : "" }}' on-tap='sort:type'>
    <div class="industry select">
      <select>
        {{#each type}}
          <option> {{this}} </option>
        {{/each}}
      </select>
    </div>
</th>

HTML

<div data-list></div>

Ractive script

var type = ['Cat', 'Dog', 'Hamster'];
var ractive = new Ractive({
  el: document.querySelector('[data-list]'),
  template: '#list-container',
  data: {
    type: type,
    sort: function ( array, column ) {

          return array.sort( function ( a, b ) {
          return a[ column ] - b[ column ];
     });
    },
    sortColumn: 'type'
  }
})

Wanted to be able to select any type and then it sorts by type, say cats; it will list Cats and list with dog and hamster.

Any insight / help in ractive appreciated!

Updated

JSFIDDLE DEMO

Currently it doesn't work because animal and sort is not correct I think


Solution

  • It looks like you need to setup two-way binding on the <select> element, so that Ractive knows what the value of sortColumn should be:

    <select value='{{sortColumn}}'>
      {{#each type}}
        <option>{{this}}</option>
      {{/each}}
    </select>