hajareshyam / pdf-creator-node

This package is used to generate HTML to PDF in Nodejs
MIT License
238 stars 79 forks source link

how to get index in loop #78

Open duccanhole opened 2 years ago

duccanhole commented 2 years ago

how to get index of element in loop #each, example:

{{#each element}}
<p>{{index ???}} - {{this}}</p>
{{/each}}
mo911 commented 2 years ago

pdf-creator-node uses the Handlebars package. this is how you get it:

{{#each element}}
<p>{{@index}} - {{this}}</p>
{{/each}}

and here will be the API reference for Handlebars if you need anything else: https://handlebarsjs.com/api-reference/data-variables.html

kingrootid commented 1 year ago

pdf-creator-node uses the Handlebars package. this is how you get it:

{{#each element}}
<p>{{@index}} - {{this}}</p>
{{/each}}

and here will be the API reference for Handlebars if you need anything else: https://handlebarsjs.com/api-reference/data-variables.html

result is 0 - [object Object]

convmo commented 1 year ago

Hi,

Yes this is correct. Because index starts from 0. and {{this}} element is an object. You must use the {{this.exampleKeyName}} format to access the data.

If it doesn't work, send a code example with data. I'll try to help for you.

kingrootid commented 1 year ago

    RujukanDetail {
      id: '5f74f6d1-4eb3-4ef2-a6be-ccbbad46caa5',
      created_by: 'd659784c-5b5c-11ed-9b6a-0242ac120002',
      updated_by: 'd659784c-5b5c-11ed-9b6a-0242ac120002',
      createdAt: 2023-06-02T09:16:27.260Z,
      updatedAt: 2023-06-02T09:16:27.260Z,
      deletedAt: null,
      transaction_id: '42b69131-1a65-4821-b005-fb1ea679f8af',
      type_sample: 'asdasd',
      diagnosis: 'asdasda',
      rujukan_id: '180b3ef8-8dfa-4561-aa84-cefc5036135b',
      received_date: null,
      received_by: null,
      received_note: null,
      duration: 324234,
      due_date: '2023-06-01',
      services: [Array],
      transaction: [TransactionEntity]
    }
  ]```

` {{#each this.others.rujukan_detail}}
          <tr>
            <td>{{@index}}</td>
            <td>{{ this.transaction.patient.name }}</td>
            <td>{{ this.transaction.patient.birthdate }}</td>
            <td>
              {{#ifCond this.transaction.patient.gender '==' 'L'}} Laki-laki
              {{else}} Perempuan {{/ifCond}}
            </td>
            <td>{{ this.type_sample }}</td>
            <td>
              <ul>
                {{#each this.services}}
                <li>{{ this.products.name }}</li>
                {{/each}}
              </ul>
            </td>
            <td>{{ this.diagnosis }}</td>
          </tr>
          {{/each}}`