question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I Use ListView with Plain JavaScript?

See original GitHub issue

Problem description

I want to know if I can use ListView https://docs.tabris.com/3.3/api/ListView.html with plain JavaScript

Expected behavior

Be able to use ListView with plain JS.

Environment

  • Tabris.js version: 3.5
  • Device: Android emulator, iOS simulator
  • OS: iOS 13.4, Android 5

Code snippet

new ListView({items:[]});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tbuschtocommented, May 29, 2020

There are examples with updating arrays, but they are all using data binding and won’t work in plain JS.

Here is an example that adds items to a plain array on items selection that should work in plain JS:

const {contentView, TextView} = require('tabris');
const {Cell, ListView} = require('tabris-decorators');

class Item {
  /** @param {string} name */
  constructor(name) {
    this.text = name;
  }
}

const listView = new ListView({
  layoutData: 'stretch',
  cellHeight: 40,
  createCell: () =>
    new Cell({highlightOnTouch: true, padding: 8, height: 52})
      .append(new TextView({centerY: 0, font: {size: 24}, id: 'myTextView'}))
      .onTap(ListView.select)
      .onItemChanged(ev => {
        ev.target._find(TextView).only('#myTextView').text = ev.value.text;
      }),
  items: [
    new Item('Hello'),
    new Item('World'),
    new Item('Foo'),
    new Item('Bar')
  ]
}).onSelect(handleSelection);

contentView.append(listView);

/** @param {import('tabris-decorators').ListViewSelectEvent<Item>} ev */
function handleSelection(ev) {
  console.log('Tapped ' + JSON.stringify(ev.item));
  listView.items = listView.items.concat([new Item('New Item')]);
}

And the same example using List instead of array, because that’s actually more convinient.

const {contentView, TextView} = require('tabris');
const {Cell, ListView, List} = require('tabris-decorators');

class Item {
  /** @param {string} name */
  constructor(name) {
    this.text = name;
  }
}

const items = List.from([
  new Item('Hello'),
  new Item('World'),
  new Item('Foo'),
  new Item('Bar')
]);

const listView = new ListView({
  layoutData: 'stretch',
  cellHeight: 40,
  items,
  createCell: () =>
    new Cell({highlightOnTouch: true, padding: 8, height: 52})
      .append(new TextView({centerY: 0, font: {size: 24}, id: 'myTextView'}))
      .onTap(ListView.select)
      .onItemChanged(ev => {
        ev.target._find(TextView).only('#myTextView').text = ev.value.text;
      })
}).onSelect(handleSelection);

contentView.append(listView);

/** @param {import('tabris-decorators').ListViewSelectEvent<Item>} ev */
function handleSelection(ev) {
  console.log('Tapped ' + JSON.stringify(ev.item));
  items.push(new Item('New Item'));
}

I’ll add these to the official examples eventually.

1reaction
cookiegurucommented, May 25, 2020

Yes, but note that you’ll need to set items to a new array in order to see any changes

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandeysambhi/Vanilla-JS-List-View-hackerrank - GitHub
This Project renders the List View in plain JavaScript. It is a solution to one of the questions asked on hackerrank.
Read more >
Javascript Listview Examples - Mobiscroll
Advanced list view component with gesture, swipe and drag & drop support. Use it along Mobiscroll forms, pages or in any web or...
Read more >
Getting started in JavaScript ListView control - Syncfusion
To get started with the ListView control, you can clone the Essential JS 2 quickstart project, and install the packages by using the...
Read more >
Single-selectable listview in pure HTML/CSS? - Stack Overflow
This starts with a simple structure, just enough that the form is enclosed in something so you can see the relative layout.
Read more >
How to Create and Update Lists in Vanilla JS
Working with the DOM in Vanilla JS apps (part 2): Creating and updating ... I can use paging for longer lists to reuse...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found