2016-12-06 164 views
-1

我是新來的javascript和sharepoint。
說,有兩個列表,list1(Process,ProcessImage),list2(SubProcess,Process)。在list2中,「Process」是來自list1的查找值。所有三個Process,SubProcess和ProcessImage的計數都不是固定的,所以說n數。從sharepoint列表中獲取數據並動態顯示在表中

使用JavaScript和HTML,我需要顯示在這樣一個頁面的數據:
列表1

過程processImage來
處理1 ProcessImage1
進程2 ProcessImage2
Process3 ProcessImage3
::
::
ProcessN ProcessImageN


列表2

子流程過程
SubProcess1過程2
SubProcess2過程2
SubProcess3處理1
SubProcess4 Process4

SubProces SN Process3


結果以表格形式使用JavaScript和HTML

ProcessImage1 ProcessImage2 ProcessImage3 ProcessImage4
SubProcess3 SubProcess1 SubProcess4
SubProcess2

+1

您使用的是哪個版本的SharePoint? – Thriggle

回答

0

如果你使用SharePoint 2010或以上,你可以使用JavaScript對象模型(JSOM)以編程方式訪問列表項,如下面的示例代碼所示。

<style> 
    /* Adjust this CSS as necessary to adjust the style of your table */ 
    #custom_output{display:table; } 
    .column_outer{display:table-cell; font-weight:bold; min-width:100px;} 
    .column_inner{font-weight:normal; margin:4px;} 
</style> 

<div id="custom_output"></div> 

<script> 
SP.SOD.executeOrDelayUntilScriptLoaded(function(){ 
    var output = document.getElementById("custom_output"); 
    var clientContext = new SP.ClientContext(); 
    var lists = clientContext.get_web().get_lists(); 
    var query = new SP.CamlQuery(); // blank query to get all items 
    var parentListItems = lists.getByTitle("List1").getItems(query), 
     childListItems = lists.getByTitle("List2").getItems(query); 
    clientContext.load(parentListItems); 
    clientContext.load(childListItems); 
    clientContext.executeQueryAsync(
     function(){ // on success callback function 
      var columnMap = createColumns(parentListItems); 
      createAndAppendCells(childListItems, columnMap); 
     }, 
     function(sender, args){ // on error callback function 
      alert(args.get_message()); 
     } 
    ); 
    // this function builds HTML columns and returns a handy reference map 
    function createColumns(parentListItems){ 
     var parentEnumerator = parentListItems.getEnumerator(), 
      columnMap = {}; 
     while(parentEnumerator.moveNext()){ 
      var item = parentEnumerator.get_current(); 
      columnMap[item.get_item("ID")] = createColumn(item); 
     } 
     return columnMap; 
    } 
    // this function adds a column to the HTML and returns a reference to a div inside it (so we can add cells later) 
    function createColumn(item){ 
     var outer = document.createElement("div"), 
      inner = document.createElement("div"), 
      title = item.get_item("Process"); 
     outer.className = "column_outer"; 
     inner.className = "column_inner";       
     output.appendChild(outer).appendChild(document.createTextNode(title)); 
     outer.appendChild(inner); 
     return inner; 
    } 
    // this function creates a cell for each child list item and appends it to the appropriate column 
    function createAndAppendCells(childListItems, columnMap){ 
     var childEnumerator = childListItems.getEnumerator(); 
     while(childEnumerator.moveNext()){ 
      item = childEnumerator.get_current(); 
      var lookup = item.get_item("Process"); 
      if(lookup){ 
       columnMap[lookup.get_lookupId()].appendChild(createCell(item)); 
      } 
     } 
    } 
    // this function creates a cell with text derived from the given list item 
    function createCell(item){ 
     var cell = document.createElement("div"); 
     cell.innerHTML = item.get_item("SubProcess"); 
     return cell; 
    } 
},"sp.js"); 
</script> 

,你會如何去嵌入頁面上的代碼取決於在SharePoint的版本你使用。

在SharePoint 2010中,可以將HTML/CSS/JavaScript另存爲HTML文件並保存在文檔庫中,然後使用內容編輯器Web部件將其嵌入到頁面中(將其「Content Link」屬性編輯爲URL的HTML文件)。

在SharePoint 2013+中,您還可以使用腳本編輯器Web部件。您還可以使用SharePoint Designer創建和編輯頁面以添加JavaScript。

相關問題