0

我有一个场景,我需要使用 jQuery 获取要在动态 html 表中显示的列表数据,以便使用内容编辑器 webpart 在站点中显示。请帮我解决这方面的问题,在此先感谢...

4

2 回答 2

2

您可以使用 SharePoint 客户端上下文 REST API 来获取数据并将其显示在表格中。添加对这三个脚本的引用:
1. /_layouts/15/SP.Runtime.js
2./_layouts/15/SP.js
3. //ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery .min.js
并使用以下示例:

<script type="text/javascript">

    $(document).ready(function () {
        fnGetData();
    });
    function fnGetData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        var list = web.get_lists().getByTitle('Users');
        var myquery = new SP.CamlQuery();
        myquery.set_viewXml("<View><Query>" +
                       "<Where>" +
                        "<IsNotNull>" +
    "<FieldRef Name='Title' />" +
    "</IsNotNull>" +
   "</Where>" +
                              "</Query></View>");
        myquery.set_datesInUtc(false);
        myItems = list.getItems(myquery);
        context.load(myItems);
        context.executeQueryAsync(Function.createDelegate(this, function () { fnGetDataSuccess(); }), Function.createDelegate(this, this.fnGetDataFailed));
    }
    function fnGetDataSuccess() {
        var txtHTML = "";
        var ListEnumeratorAcc = this.myItems.getEnumerator();
        while (ListEnumeratorAcc.moveNext()) {
            var currentItem = ListEnumeratorAcc.get_current();
            txtHTML = txtHTML + "<tr>";
            txtHTML = txtHTML + "<td>";
            if (currentItem.get_item('Title') != null) {
                txtHTML = txtHTML + currentItem.get_item('Title');
            }
            txtHTML = txtHTML + "</td>";


            txtHTML = txtHTML + "<td>";
            if (currentItem.get_item('Owner') != null) {
                txtHTML = txtHTML + currentItem.get_item('Owner').get_lookupValue();
            }
            txtHTML = txtHTML + "</td>";
            txtHTML = txtHTML + "</tr>";
        }
        $("#tblCustomListData").append(txtHTML);

    }
    function fnGetDataFailed(sender, args) {
        alert("Error Message:\n" + "URL: " + sender.get_url() + ". \n\Error Description:" + args.get_message());
    }
</script>
<table id="tblCustomListData" border="1">
    <thead>
        <tr>
            <th>Title
            </th>
            <th>Owner
            </th>
        </tr>
    </thead>
</table>
于 2014-08-01T10:49:09.580 回答
0

您可以使用SharepointPlusSPServices来检索列表数据。然后很容易使用 jQuery 将数据插入到 HTML 页面中。

于 2013-07-19T07:42:21.327 回答