Handlebars. js sets the value by means of braces (list), handlebars. jslist

Source: Internet
Author: User

Handlebars. js sets the value by means of braces (list), handlebars. jslist

There is such a requirement that tr uses the each value in a table and the retrieved value corresponds to the table title. How can this problem be achieved? For example:

<Table> <thead> <tr >{{# each title set, value example [name, sex...]} <th> name </th> <th> sex </th> </tr> </thead> <tbody> {# each content set, value example [{name: 'su Sh', sex: 'male'}, {name: 'Li Qingzhao ', sex: 'female'}...]} what should I do if I want to match the data with the title? <Tr> <td> Su Shi </td> <td> male </td> </tr> <td> Li Qingzhao </td> <td> female </ td ></tr >{{/ each }}</tbody> </table>
View Code

If it is in JS, we can use the list [key] method to set the value, but handlebars does not seem to support this method. I don't know if I don't understand it myself.

After searching for the materials for a long time, I did not find a suitable solution. Therefore, it is very easy to write it by myself.

Handlebars.registerHelper("getValueByKeyFromList", function(list, key, options){    if(list && key && list[key]){        return list[key];    }    return;});
View Code

In the preceding table

{# Each content set, value example [{name: 'su Sh', sex: 'male'}, {name: 'Li Qingzhao ', sex: female}...]} what should I do if I want to match the data with the title? <Tr >{{# each title set, value example [name, sex...]} <td> {getValueByKeyFromList .. /this }}</td> .. /this indicates the current value of the previous set. this indicates the current value of the current set. {/each }}</tr >{{/each }}
View Code

Solution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.