2016-12-06 165 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。

相关问题