Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>My Record Collection</h1>

<div class="collection" x-data="collection">
  <fieldset class="filters" hidden>
    <legend>Filter by</legend>

    <label for="artist">Artist</label>
    <select id="artist" x-model="filters.artist">
      <option value="">All</option>
      <template x-for="artist in artists">
        <option x-text="artist"></option>
      </template>
    </select>

    <label for="decade">Decade</label>
    <select id="decade" x-model="filters.year">
      <option value="">All</option>
      <template x-for="year in decades">
        <option :value="year" x-text="`${year}0`"></option>
      </template>
    </select>
  </fieldset>
  
  <p id="message">Showing <output x-text="message">40 records</output></p>
	
	<div aria-labelledby="message" role="region" tabindex="-1" x-ref="region">
	  <ol class="records">	
			<template x-for="record in page">
				<li>
				  <strong x-text="record.title"></strong><br>
				  Released in <time :datetime="record.year" x-text="record.year"></time> by <span x-text="record.artist"></span>.
				</li>
			</template>
      <li x-ignore>
        <strong>Die große Palmöllüge</strong><br>
        Released in <time datetime="2020">2020</time> by Akne Kid Joe.
      </li>
      <li x-ignore>
        <strong>Post Human: Survial Horror</strong><br>
        Released in <time datetime="2020">2020</time> by Bring me the Horizon.
      </li>
      <li x-ignore>
        <strong>Joy as an Act of Resistance</strong><br>
        Released in <time datetime="2018">2018</time> by Idles.
      </li>
      <li x-ignore>
        <strong>Licensed to Ill</strong><br>
        Released in <time datetime="1986">1986</time> by Beastie Boys.
      </li>
      <li x-ignore>
        <strong>Paul&#39;s Boutique</strong><br>
        Released in <time datetime="1989">1989</time> by Beastie Boys.
      </li>
    </ol>
  </div>

  <nav aria-label="Select a page">
    <ol class="pages">
      <template x-for="idx in numOfPages">
        <li>
          <a @click.prevent="currentPage = idx - 1; $nextTick(() => { $refs.region.focus(); $refs.region.scrollIntoView() });" :href="`/${idx}`" x-text="`Page ${idx}`" :aria-current="idx === currentPage + 1 ? 'page' : false"></a>
        </li>
      </template>
      <li x-ignore>
        <a href="/index_js/" aria-current="page">
          Page 1
        </a>
      </li>
      <li x-ignore>
        <a href="/index_js/1/">
          Page 2
        </a>
      </li>
    </ol>
  </nav>
</div>
<script>
document.addEventListener('alpine:init', () => {
	Alpine.data('collection', () => ({
    artists: [],
		decades: [],
    filters: {
      year: '',
      artist: '',
    },
    records: [],
		itemsPerPage: 5,
		currentPage: 0,
    numOfPages() {
      return Math.ceil(this.filteredRecords.length / this.itemsPerPage)
    },
    page() {
      return this.filteredRecords.slice(this.currentPage * this.itemsPerPage, (this.currentPage + 1) * this.itemsPerPage)
    },
    async getRecords() {
      this.records = await (await fetch('https://assets.codepen.io/144736/all_records.json')).json();
			this.artists = [...new Set(this.records.map(record => record.artist))].sort();
		  this.decades = [...new Set(this.records.map(record => record.year.toString().slice(0, -1)))].sort();
      document.documentElement.classList.add('alpine');
    },
    get filteredRecords() {
      const filtered = this.records.filter((item) => {
        for (var key in this.filters) {
          if (this.filters[key] === '') {
            continue
          }

          if(!String(item[key]).includes(this.filters[key])) {
            return false
          }
        }

        return true
      });

      return filtered
    },
    message() {
		  return `${this.filteredRecords.length} records`;
	  },
    init() {
      this.getRecords();
      this.$watch('filters', filter => this.currentPage = 0);
	   }
	}))
})
</script>
              
            
!

CSS

              
                .filters {
  display: block;
}

html:not(.alpine) .filters {
	visibility: hidden;
}

.alpine [x-ignore] {
  display: none;
}

body {
  font-family: sans-serif; 
}

ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.records > * + * {
  margin-top: 2rem;
}

h2 {
  margin-bottom: 0;
}

nav {
  margin-top: 2rem;
}

.pages {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.pages a {
  border: 1px solid #000000;
  padding: 0.5rem;
  border-radius: 5px;
  display: flex;
  text-decoration: none;
}

.pages a:where(:link, :visited) {
    color: #000;
}

.pages a:where([aria-current]) {
    background-color: #000000;
    color: #ffffff;
}

.pages a:where(:focus, :hover) {
    background-color: #6c6c6c;
    color: #ffffff;
}
              
            
!

JS

              
                
              
            
!
999px

Console