Question

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>  
Was it helpful?

Solution

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>

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top