views:

543

answers:

2

Is it possible to re-use a client-template using asp.net ajax 4.0 client templates? I have a scenario as shown in the example below. I have two questions here:

  1. I could not get the if conditionwork in client-template
  2. How can I re-use the u l tag template for three types of "column" data (col1, col2, col3 in example json)?

sample code:

<style>
    .sys-template { display:none; }
    .list {width:220px; float:left;  margin:0px 0px 0px 10px; background-color:#f2f2f2; }
    </style>

    <script src="MicrosoftAjax.debug.js" type="text/javascript"></script>
    <script src="MicrosoftAjaxTemplates.debug.js" type="text/javascript"></script>

    <script type="text/javascript">
        var listItems = [{ item: "item1", col: "col1" },
             { item: "item2", col: "col1" },
             { item: "item3", col: "col1" },
             { item: "item4", col: "col2" },
             { item: "item5", col: "col2" },
             { item: "item6", col: "col2" },
             { item: "item7", col: "col2" },
             { item: "item8", col: "col3" },
             { item: "item9", col: "col3" },
             { item: "item10", col: "col3" }]
    </script>
    </head>

    <body xmlns:sys="javascript:Sys" xmlns:dataview="javascript:Sys.UI.DataView" sys:activate="*">
    <ul id="col1" class="list sys-template"  sys:attach="dataview" dataview:data="{{ listItems }}">
        <!--* if (col=="col1") { *-->
        <li>{{item}}</li>
        <!--* } *-->
    </ul>
    <ul id="col2" class="list sys-template"  sys:attach="dataview" dataview:data="{{ listItems }}">
        <!--* if (col=="col2") { *-->
        <li>{{item}}</li>
        <!--* } *-->
    </ul>
    <ul id="col3" class="list sys-template"  sys:attach="dataview" dataview:data="{{ listItems }}">
        <!--* if (col=="col3") { *-->
        <li>{{item}}</li>
        <!--* } *-->
    </ul>
    </body>
A: 

In preview 4, you inject code using code:before and code:after attributes instead of the comment code blocks.

It's also possible to re-use a template. Actually, the framework will do that for you: if you point the template property of two data views at the same element, only one template will get compiled and used.

Thanks for a quick reply Bertrand.
Venkata Uma Lakkakula
A: 

Something like this should work -- I'm just typing this on the fly so forgive me if it isnt perfect.

<ul id="template1" class="sys-template">
  <li code:if="col==$element.id">{{item}}</li>
</ul>

<ul id="col1" class="list sys-template" sys:attach="dataview" dataview:data="{{listItems }}" dataview:itemtemplate="template1">
</ul>
<ul id="col2" class="list sys-template" sys:attach="dataview" dataview:data="{{listItems }}" dataview:itemtemplate="template1">
</ul>
<ul id="col2" class="list sys-template" sys:attach="dataview" dataview:data="{{listItems }}" dataview:itemtemplate="template1">
</ul>
InfinitiesLoop
InfinitiestLoop,This is waht I needed, an example :) thanks a lot.Only thing that required to change in your example is that, actual implementation of template should not have "sys-template" class, its actually hiding output. So I changed it to:<ul id="col1" class="list" sys:attach="dataview" dataview:data="{{listItems }}" dataview:itemtemplate="template1"></ul>worked like a charm
Venkata Uma Lakkakula
dataview:itemtemplate= should be dataview:itemTemplate=
Tim Hoolihan

related questions