簡體   English   中英

使用 JavaScript 限制可在 SharePoint 2013 列表中輸入的項目數

[英]Limit the number of items that can be entered in a SharePoint 2013 List using JavaScript

我正在嘗試制作一個用於事件注冊的網頁。 我有一個頁面連接到 SharePoint 上的列表,我想用 JS 設置一個條件,將人數限制為 50 人。

我已經嘗試過這個解決方案,但它不起作用: 如何限制可以在 SharePoint 列表中輸入的項目數?

我覺得js方案不是最好的方式,js方案只有在某些情況下才會觸發。
我建議您創建一個事件接收器(項目添加,項目更新)來限制列表中的數量。

public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            if (properties.List.ItemCount >= 50)
            {
                properties.Cancel = true;
                properties.ErrorMessage = "Contain too much item";
                properties.Status = SPEventReceiverStatus.CancelWithError;
            }                                
        }

如何:創建事件接收器

使用 OnPreRender 函數。 您將必須閱讀整個列表,然后不顯示表單而是顯示一條消息。

但是,您可以在沒有 CSR 的情況下使用 SharePoint 為您完成一半的工作。

過去一個快速簡便的 ductape 補丁是隱藏 NewForm 內容。

在 NewForm.aspx 頁面上添加一個 ListView WebPart,使其列出您的所有項目,將 WebPart 布局屬性區域索引更改為 1,使其顯示在 NewForm 下方 添加另一個 WebPart,使用內容編輯器 WebPart 和內容:

<script>
    SP.SOD.executeFunc('sp.js', null, function () {//execute after pageload
        var items = document.getElementsByClassName('ms-itmhover');//all displayed listitems
        if (items.length > 10) {//maximum number of items
            var form = document.getElementsByClassName('ms-formtable')[0];//new form
            while (form.tagName !== 'DIV') {//go up to the enclosing DIV
                form = form.parentNode;
            }
            form.innerHTML = '<h1>Numbers of records has exceeded the permitted limit: ' + items.length + '</h1>';
        } else {//optional: hide the View with all the items
            var itemlist = items[0];//first item
            while (itemlist.tagName !== 'DIV') {//go up to enclosing DIV
                itemlist = itemlist.parentNode;
            }
            itemlist.style.display = 'none';
        }
    });
</script>

提示:將 javascript 放在單獨的文件中並將內容編輯器 Web 部件鏈接到它.. 使編輯更容易...您仍然必須包含 <script> 標記!

在此處輸入圖片說明

另一種選擇是: 限制每個用戶每天只能在共享點列表中輸入一個條目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM