开发者

Using jQuery templates with complex objects and getting values from subcollections

In my jQuery template I need to get the value of an item that is contained within a subcollection of my data structure.

Say I have a data structure that looks like this:

    <Quote>
      <AgentName></AgentName>
      <AgentId></AgentId>
      <QuoteId></QuoteId>
      <ProductId></ProductId>
      <ProductName></ProductName>
      <Benefits>
        <Benefit>
          <Name>Benefit One</Name>
          <Description>This is benefit One</Description>
          <Value>5000000</Value>
          <ValueComment></ValueComment>
          <Excess>200</Excess>
          <ExcessComment></ExcessComment>
        </Benefit>
        <Benefit>
          <Name>Benefit Two</Name>
          <Description>This is benefit Two</Description>
          <Value>1000</Value>
          <ValueComment></ValueComment>
          <Excess>100</Excess>
          <ExcessComment></ExcessComment>
        </Benefit>
      </Benefits>
      <Price>10.99</Price>
    </Quote>

I can then convert this into a JSON string that looks like this:

[{"AgentName":null,"AgentId":null,"QuoteId":null,"ProductId":"abc","ProductName":"Standard","Benefits":[{"Name":"Benefit One","Description":"\n This is benefit One \n ","Value":5000000,"ValueComment":null,"Excess":200,"ExcessComment":null},{"Name":"Benefit Two","Description":"\n This is benefit Two\n ","Value":1000,"ValueComment":null,"Excess":100,"ExcessComment":null}],"Price":10.99}

In my jQuery Template I want to create a table that shows the Product ID, Product Name, Price and ONLY the value of Benefit One:

<script id="QuoteTemplate" type="text/x-jQuery-tmpl">
    <tr>
      <td valign="middle" style="width: 120px; text-align: left; font-weight:bold" class="quote-row"> ${ProductId} </td>
      <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"> ${ProductName} </td>

      // Somehow need to query the benefits collection to get only the value of Benefit One
      <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"开发者_运维技巧> ${   ***ONLY BENEFIT ONE*** .value    } </td>

      <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"> ${Price} </td>
    </tr>
</script>   

Thanks for your help.


Hello the following has worked for me: {{= Benefits[0].Value}}

Basically, Benefits is an array so we take the first value of that array which is benefits one and show Value entity

<script id="stackTemplate" type="text/x-jQuery-tmpl">
    <tr>
      <td valign="middle" style="width: 120px; text-align: left; font-weight:bold" class="quote-row"> ${ProductId} </td>
      <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"> ${ProductName} </td>


                <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"> {{= Benefits[0].Value}} </td>


      <td valign="middle" style="width: 140px; text-align: center; font-weight:bold" class="quote-row"> ${Price} </td>
    </tr>
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜