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

Here's a little challenge for you SharePoint and jQuery experts. I have an out of the box list view in MOSS where I total one of the columns as a "Sum" so that at the very top of my list I have "Sum = #####".

What I want to know is the most efficient script that would be to pull the text "Sum = ###,###" out of this list view so I can do something else with the value.

I wrote a script that works, and works well, but I have a feeling there is a cleaner approach. What I do is:

1) use the web part ID to get the HTML of the web part containing the sum 2) use JavaScript substring method to find and pull out "Sum = ###,###".

Like I said, it works, but it's not elegant.

Other ideas?

share|improve this question
    
How much your web part are customized? –  Salvatore Di Fazio Dec 16 '11 at 15:39
add comment

3 Answers

With something like this, I'm not sure it's worth obsessing unless: 1) The page is likely to change regularly, or 2) you'll need to get at the value repeatedly in a short period of time.

I might try something like this: $("#WebPartWPDnn").find("nobr:contains('Sum=')").html().split("=")[0].replace(",","");

If there's only going to be one "Sum=" in the page, you could omit the Web Part ID. This sort of thing is going to happen so fast in most cases that efficiency just doesn't really matter that much.

If you'll need to get the value more than once in the page lifespan for some reason, create a variable to point to it: var theSumContainer = $("#WebPartWPDnn").find("nobr:contains('Sum=')"); var theSum = theSumContainer.html().split("=")[0].replace(",","");

share|improve this answer
    
thanks Marc... a little more simple than what I was doing, so I'll take it... And the exact code needed to get just the number is: $("#WebPartWPDnn").find("nobr:contains('Sum = ')").html().split(" = ")[1].replace(",","").replace("</B>",""); and Just because it happens fast, doesn't mean you should not improve performance where you can. :) –  Mark Rackley Dec 16 '11 at 23:14
    
I just meant that you probably have other things in your scripts that are likely to cause performance problems that really matter. I'm guessing that in this one page you can be even more efficient on this one by selecting on something other than the nobr. I'm curious what the </B> is, too. –  Marc D Anderson Dec 17 '11 at 4:35
    
Duh. I realized what the </B> was after I posted my comment above. $("#WebPartWPDnn").find("nobr b:contains('Sum = ')").html().split(" = ")[1].replace(",",""); –  Marc D Anderson Dec 18 '11 at 0:12
    
See.. that's what i'm talking about.. I may be able to make jQuery do whatever I want it to, but sometimes I do it in 4 lines when it can be done in one. I tried using contains to narrow down what I wanted, but was obviously not using it correctly... Love it when I get an aha moment from something simple. :) –  Mark Rackley Dec 18 '11 at 17:51
    
On the other hand, sometimes the 4 lines will be more maintainable code. There are trade-offs all around. Note that we aren't writing // comments. ;-) –  Marc D Anderson Dec 19 '11 at 12:28
add comment

Yes in the short time I had available to do this I did something similar to you:

$("#WebPartWPDnn").find("nobr:contains('Sum=')").text();

After that, standard sub-stringing and "parseFloat" to extract the value.

share|improve this answer
    
Thank DeChrist, that's essenstially what I was doing before. Also, from my playing, I believe "contains" returns the html for the element, so it has the same value as $("#WebPartWPDnn").html() which has me parsing a very long string. If this is the best approach, I'll stick with it. Thanks again for the help! –  Mark Rackley Dec 16 '11 at 16:47
    
additionally, ".text()" strips off all the html tags so it's not 100% possible to determine where the last digit of the number of "SUM = ###,###" is becase the very next character could conceivably be a number that relates to something else. by using .html() I can check for the trailing </B> to know that I'm at the end of the number. –  Mark Rackley Dec 16 '11 at 16:48
add comment

A standard method is to use regular expressions: /Sum=([0-9,]+)/

The regular expression will match the pattern, and the capturing parentheses will return the value.

A reference for regular expressions: http://www.regular-expressions.info/

This is not necessarily faster than your method.

[Edit] To follow up on Mark's comment, maybe a better example of regular expression:

/(Sum|Max|Min)=([0-9,]+)/

The above expression will work not only for Sum, but also for Max and Min.

As you can expect, the more sophisticated the regular expression is, the slower it'll be.

share|improve this answer
    
thanks for the feedback Christophe. Not only am I looking for performance, I'm also looking for the most maintable code possible. I know it's hard to believe, but sometimes I hack stuff to get it to work, and every now and then I like to see how the other experts do it ;) –  Mark Rackley Dec 17 '11 at 20:16
add comment

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.