从SharePoint列表中获取数据,并在表中动态显示



我是新来的JavaScript和SharePoint。
假设有两个列表,List1(过程,过程图),List2(子过程,过程)。在List2中,"过程"是List1的查找值。所有三个过程,子过程和过程图的计数均未固定,因此请说n个数字。

使用JavaScript和HTML,我需要在这样的页面上显示数据:
List1

Process ProcessImage
Process1 ProcessImage1
Process2 ProcessImage2
Process3 ProcessImage3
::
::
Processn ProcessImagen


list2

子进程过程
SubProcess1 Process2
SubProcess2 Process2
SubProcess3 Process1
SubProcess4 Process4
::
Subocessn Process3


使用JavaScript和HTML

导致表格形式

ProcessImage1 ProcessImage2 ProcessImage3 ProcessImage4
subocess3 subocess1 subprocess4
SubProcess2

如果您使用的是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零件将其嵌入页面上(编辑其"内容链接"属性为url为urlhtml文件)。

在SharePoint 2013 中,您还可以使用脚本编辑器Web部件。您也可以使用SharePoint Designer创建和编辑页面以添加JavaScript。

最新更新