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.