@nkeff/cardboard-js
    Preparing search index...

    Function each

    • Render a CTag for each item in the provided list.

      each can work with a goold old array, or with a IObservable. If you provide a Observable, the list will update whenever the Observable changes.

      Type Parameters

      • T

      Parameters

      • observable: IObservableOr<T[]>

        An array or an IObservable that contains the list of items to render.

      • builder: (val: T) => CTag

        A function that takes an item from the list and returns a CTag to render.

      • Optionalkey: (val: T) => any

        An optional function that returns a unique key for each item in the list. This is used to optimize the rendering process.

      Returns Node

      Static list

      const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
      div(
      each(colors, (color) =>
      button(color).addStyle('color', color)
      )
      );

      Dynamic list

       const colors = state(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']);
      const selectedColor = state('red');
      div(
      each(colors, (color) =>
      button(color)
      .addStyle('color', color)
      .stylesIf(equalTo(selectedColor, color), { fontWeight: 'bold' });
      )
      );