Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When customising a list with JS or XSLT, the formatting seems to be applied not just to the list view web part the property is set for - but for all parts with the same list type ID on the page containing the part.

Simply put, if I create two custom lists with unique fields and use a list view web part on a single page, how can I have two JS Link files, one for formatting each part? - a quick overview would be great.

share|improve this question

1 Answer 1

When you apply custom XSL to the list view then add in unique class or id to the HTML. You can then use these tags to hook into the correct content from your jQuery.

So as an example, in your XSLT for list1:

<div class="list1item">
 <xsl:value-of select="@Title" />
</div>

Then from your jQuery:

$("div.list1item").attr("style","color:red");

This would apply the custom style only to the items from list 1.

You then need to do the same for List 2 but this time use a different class - eg list2item.

share|improve this answer
    
Thanks, but I mean the other way around. When I apply a JS link to a list view web part (and I assume XSLT too), the JS is applied to all list view web parts on the page. How can I limit JS or XSLT 'link' styling to just the part for which the file to format it is specified? –  Kieren Johnstone Apr 12 '13 at 22:34
    
By default all the items in list views will have the same classes. So it depends what selector you use in your jQuery. If you use the standard generic class then it will apply that jQuery to every list item in all web parts. This is why I was suggesting you use some custom XSL to ensure there is some way to uniquely identify the items you want to apply it to. –  Dave Paylor Apr 13 '13 at 2:53
    
You may not be getting what I mean though. See here: learningsharepoint.com/2013/04/04/… no jQuery or classes. If we use that exact code on one list web part's JS Link property, all list web parts on the page are automatically transformed using the same JS Link code. (those which have the same list type ID, anyway.. I've been told there's no way to give a custom list created 'OOB' - not via an app - a bespoke list type ID?) –  Kieren Johnstone Apr 13 '13 at 7:45
    
Aha - you are correct, I had misunderstood what you were doing. I have not tried this approach yet in 2013 but it looks like it is applying a custom template for all lists of that type - so when you apply the JS to the list view on the page it is also applying it to any other list of the same type on the same page. I agree with you that it seems it should only apply it to the view which has the JS Link applied but it looks like others are having the same issue. –  Dave Paylor Apr 13 '13 at 8:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.