将 jQuery 模板与复杂对象结合使用并从子集合中获取值

发布于 2024-10-11 19:51:59 字数 2453 浏览 2 评论 0原文

在我的 jQuery 模板中,我需要获取数据结构子集合中包含的项目的值。

假设我有一个如下所示的数据结构:

    <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>

然后我可以将其转换为如下所示的 JSON 字符串:

[{"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}

在我的 jQuery 模板中,我想创建一个表来显示产品 ID、产品名称、价格以及仅显示的值好处一:

<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>   

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-10-18 19:51:59

您好,以下对我有用: {{= Benefits[0].Value}}

基本上,Benefits 是一个数组,因此我们采用该数组的第一个值,即 Benefits 并显示 Value 实体

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文