2015-07-03 78 views
0

我的javascript不是最好的,並想知道是否有人可以幫我解決這個問題。本質上,我有一個名爲PGCount的整數列的庫,我希望能夠點擊這個按鈕,它增加了定義變量pgcount的值,它目前是警報,但我有更大的計劃,如果只是爲了獲得預期的結果。Ribbon命令讀取值

可悲的是它計數第一個項目兩次。

這裏是整個模塊

<?xml version="1.0" encoding="utf-8"?> 
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> 
    <CustomAction 
    Id="Ribbon.Library.Actions.AddAButton" 
    Location="CommandUI.Ribbon" 
    RegistrationId="101" 
    RegistrationType="List" 
    Title="PGCount"> 
    <CommandUIExtension> 
     <CommandUIDefinitions> 
     <CommandUIDefinition 
      Location="Ribbon.Library.Share.Controls._children"> 
      <Button Id="Ribbon.Library.Share.NewRibbonButton" 
      Command="CountPGCount" 
      LabelText="Page Count" 
      TemplateAlias="o2" /> 
     </CommandUIDefinition> 
     </CommandUIDefinitions> 
     <CommandUIHandlers> 
     <CommandUIHandler Command="CountPGCount" 
          CommandAction="javascript: 
          var listitem; 
          var pgcounts = 0; 
          getWebProperties(); 
          function getWebProperties() { 
           var ctx = new SP.ClientContext.get_current(); 
           var currentWeb = ctx.get_web(); 
           var currentListGuid = SP.ListOperation.Selection.getSelectedList(); 
           var currentList = currentWeb.get_lists().getById(currentListGuid); 
           var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 
           for (i in selectedItems) { 
            listitem = currentList.getItemById(selectedItems[i].id); 
            ctx.load(listitem); 
            ctx.executeQueryAsync(Function.createDelegate(listitem, function() { 
             var c = listitem.get_fieldValues().PGCount; 
             pgcounts+=c;  
            }), null); 
           };} 
           setTimeout(function() { 
            alert(pgcounts); 
           }, 3000);" 
     EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length >= 1;" /> 
     </CommandUIHandlers> 
    </CommandUIExtension> 
    </CustomAction> 

    <Module Name="Module1"> 
    </Module> 
</Elements> 

任何幫助,將不勝感激!

回答

0

嗯,我必須說,我這一整天都在努力工作! 這不計算與類似項目相同的問題,因爲此加載多個項目。衝突總是發生。

現在我知道你很多很酷的Java頭會說這是錯的,我知道這一點,特別是檢查項目數量等,但我不認爲這太破舊了,它會我知道幫助某人。如果願意的話,請隨時收拾整理編號:)

<?xml version="1.0" encoding="utf-8"?> 
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> 
    <CustomAction 
    Id="Ribbon.Library.Actions.AddAButton" 
    Location="CommandUI.Ribbon" 
    RegistrationId="101" 
    RegistrationType="List" 
    Title="PGCount"> 
    <CommandUIExtension> 
     <CommandUIDefinitions> 
     <CommandUIDefinition 
      Location="Ribbon.Library.Share.Controls._children"> 
      <Button Id="Ribbon.Library.Share.NewRibbonButton" 
      Command="CountPGCount" 
      LabelText="Page Count" 
      TemplateAlias="o2" /> 
     </CommandUIDefinition> 
     </CommandUIDefinitions> 
     <CommandUIHandlers> 
     <CommandUIHandler Command="CountPGCount" CommandAction="javascript: 

           var rows = new Array(); 
           var countofitems = -1; 
           var countogpgcounts = 0; 

           getpgcounts(); 

           function getpgcounts() { 
            var context = new SP.ClientContext.get_current(); 
            var web = context.get_web(); 
            var lists = web.get_lists(); 
            var listId = SP.ListOperation.Selection.getSelectedList(); 
            var list = lists.getById(listId); 
            var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 

            rows = []; 

            for (var i in selectedItems) { 
             var id = selectedItems[i].id; 
             rows[i] = list.getItemById(id); 
             context.load(rows[i]); 
             countofitems++; 
            } 

            context.executeQueryAsync(Function.createDelegate(this, show),Function.createDelegate(this, showError)); 
           } 

           function show() { 

            for (i in rows) { 
             var thiscount = rows[i].get_item('PGCount'); 
             countogpgcounts += thiscount; 
             if (i == countofitems) { 
              alert(countogpgcounts); 
             } 
            } 
           } 

           function showError(sender, args) { 
            throw new Error('request failed: ' + args.get_message() + '\n' + args.get_stackTrace()); 
           } 
" 


     EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length >= 1;" /> 
     </CommandUIHandlers> 
    </CommandUIExtension> 
    </CustomAction> 

    <Module Name="Module1"> 
    </Module> 
</Elements>