Browse Source

rep Fotoliste und chunk funktion in helfer

hmt 6 years ago
parent
commit
5bedd4c83f
2 changed files with 15 additions and 8 deletions
  1. 10 8
      Fotoliste.html
  2. 5 0
      helfer.js

+ 10 - 8
Fotoliste.html

@@ -1,14 +1,18 @@
 {#await promise}Fotos kommen…
 {:then fotos}
-  {#each _.chunk(fotos, 25) as slice}
+  {#each chunk(fotos, 25) as slice}
     <div class="page grid" orientation="portrait" size="A4">
       <div class="main">
-        <h5>{slice[0].Klasse}</h5>
+        <b>{slice[0].Klasse}</b>
         Stand: {datum(new Date())}
-        <div class="grid">
+        <div class="grid" style="font-size: 0.8rem">
           {#each slice as s}
             <div>
-              <img src="data:image/jpg;base64,{s && buffer(s.schuelerfoto.Foto)}" alt="Foto" style="width: 120px">
+              {#if s.schuelerfoto}
+                <img src="data:image/jpg;base64,{buffer(s.schuelerfoto.Foto)}" alt="Foto" style="width: 120px">
+              {:else}
+                Kein Foto
+              {/if}
               <br>
               {s.Name}, {s.Vorname}
             </div>
@@ -21,15 +25,13 @@
 {/await}
 
 <script>
-  import { beforeUpdate } from 'svelte'
-  import { datum } from './helfer'
+  import { datum, chunk } from './helfer'
   export let schueler, knexConfig
   const SchuelerFoto = R('models').Schueler
-  const _ = R('lodash')
 
   SchuelerFoto.knex(R('knex')(knexConfig))
   const buffer = (d) => Buffer.from(d, 'binary').toString('base64')
-  $: promise = SchuelerFoto.query().whereIn('ID', schueler.map(s => s.ID)).eager('[schuelerfoto]')
+  $: promise = SchuelerFoto.query().whereIn('ID', schueler.map(s => s.ID)).eager('[schuelerfoto]').orderBy('Name')
 </script>
 
 <style>

+ 5 - 0
helfer.js

@@ -11,6 +11,7 @@ const punkte = {
              0: '6' }
 const zahlwort = { 1: "eins",2: "zwei",3: "drei",4: "vier",5: "fünf",6: "sechs",7: "sieben",8: "acht",9: "neun",0: "null" }
 
+// Verzichten wir auf teure lodash-Funktionen:
 export const groupBy = (arr, id) => arr.reduce(
   (entryMap, f) => {
     const fx = id.split('.').reduce((p,c)=>p&&p[c]||null, f)
@@ -18,6 +19,10 @@ export const groupBy = (arr, id) => arr.reduce(
   },
   new Map()
 )
+export const chunk = (arr, size) => arr.reduce((chunks, el, i) => (i % size
+    ? chunks[chunks.length - 1].push(el)
+    : chunks.push([el])) && chunks, [])
+
 export const datum = (t) => {
   // gibt ein Datum im deutschen Format zurück
   try {