下拉字段 - 第一项应为空

发布于 2024-10-03 15:40:25 字数 148 浏览 0 评论 0原文

在查找列中使用共享点构建并将其设置为必填字段。 SharePoint 自动选择下拉框中的第一项(对最终用户来说有点误导)。

有没有办法让这个下拉框的第一行显示空白或空?

(我对任何解决方案持开放态度。我更喜欢 javascript 类型的解决方案)

Using sharepoint build in lookup column and it set to required field. SharePoint automatically selects the first item in the dropdown box (kinda misleading for the end users).

Is there a way to display blank or Null for the first row of this drop down box?

(I am open to any solution. I prefer javascript type solution)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疾风者 2024-10-10 15:40:25

对于选择字段,默认值在列设置中配置。如果填充了“默认值”输入框,请删除该值以便不使用默认值。

编辑

对于查找字段,如果需要,该字段似乎会发生巨大变化。默认情况下,不需要的字段具有“(无)”值。但是,将该字段切换为必填将删除“(无)”值,并自动选择第一个值。

我发现的一件事是,如果您使用 JavaScript 将 null 值添加到下拉列表中,然后尝试按“确定”,您会收到一个错误页面:“发生了意外错误。”作为解决方法,我编写了更多代码来快速验证该字段在提交表单之前是否有值。如果该字段没有值,则会提示用户并取消提交。 (注意:此代码仅附加到“确定”按钮,因此您在编辑 EditForm.aspx 时可能会遇到错误。只需为查找字段选择一个值,您就可以像平常一样进行编辑)

无论如何,进入代码..我认为您需要更改的唯一行是 var fieldTitle = 'Large Lookup Field'; 将其更新为您的字段名称。

<script type="text/javascript">

function GetDropdownByTitle(title) {
    var dropdowns = document.getElementsByTagName('select');
    for (var i = 0; i < dropdowns.length; i++) {
        if (dropdowns[i].title === title) {
            return dropdowns[i];

        }
    }
    return null;
}

function GetOKButtons() {
    var inputs = document.getElementsByTagName('input');
    var len = inputs.length;
    var okButtons = [];
    for (var i = 0; i < len; i++) {
        if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' && 
             inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
             okButtons.push(inputs[i]);
        }
    }
    return okButtons;
}

function AddValueToDropdown(oDropdown, text, value, optionnumber){
    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value',value);
    if (typeof(optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option,options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    oDropdown.options.selectedIndex = 0;
}

function WrapClickEvent(element, newFunction) {
    var clickFunc = element.onclick;
    element.onclick = function(event){
        if (newFunction()) {
            clickFunc();
        }
    };
}

function MyCustomExecuteFunction() {
    // find the dropdown
    var fieldTitle = 'Large Lookup Field';
    var dropdown = GetDropdownByTitle(fieldTitle);
    if (null === dropdown) {
        alert('Unable to get dropdown');
        return;
    }

    AddValueToDropdown(dropdown, '', '', 0);

    // add a custom validate function to the page
    var funcValidate = function() {
        if (0 === dropdown.selectedIndex) {
            alert("Please choose a value for " + fieldTitle + ".");
            // require a selection other than the first item (our blank value)
            return false;
        }
        return true;
    };

    var okButtons = GetOKButtons();
    for (var b = 0; b < okButtons.length; b++) {
        WrapClickEvent(okButtons[b], funcValidate);
    }
}

_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");

</script>

For Choice fields, the default value is configured in the column settings. If the "Default value" input box is populated, delete the value in order to use no default value.

Edit

For Lookup fields, the field seems to change dramatically if it is required. Fields that are NOT required have a "(None)" value by default. However, toggling the field to required will remove the "(None)" value and the first value is selected automatically.

One thing I found, is that if you use JavaScript to add the null value to the dropdown and then try to press OK you get an error page: "An unexpected error has occurred." As a workaround, I wrote some more code to do a quick validation that the field has a value before the form is submitted. If the field has no value, then it will prompt the user and cancel the submit. (Note: this code is only attached to the OK buttons so you may get errors while editing EditForm.aspx.. just choose a value for your lookup field and you'll be able to edit like normal)

Anyways, onto the code... I think the only line you'll need to change is var fieldTitle = 'Large Lookup Field'; to update it to the name of your field.

<script type="text/javascript">

function GetDropdownByTitle(title) {
    var dropdowns = document.getElementsByTagName('select');
    for (var i = 0; i < dropdowns.length; i++) {
        if (dropdowns[i].title === title) {
            return dropdowns[i];

        }
    }
    return null;
}

function GetOKButtons() {
    var inputs = document.getElementsByTagName('input');
    var len = inputs.length;
    var okButtons = [];
    for (var i = 0; i < len; i++) {
        if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' && 
             inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
             okButtons.push(inputs[i]);
        }
    }
    return okButtons;
}

function AddValueToDropdown(oDropdown, text, value, optionnumber){
    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value',value);
    if (typeof(optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option,options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    oDropdown.options.selectedIndex = 0;
}

function WrapClickEvent(element, newFunction) {
    var clickFunc = element.onclick;
    element.onclick = function(event){
        if (newFunction()) {
            clickFunc();
        }
    };
}

function MyCustomExecuteFunction() {
    // find the dropdown
    var fieldTitle = 'Large Lookup Field';
    var dropdown = GetDropdownByTitle(fieldTitle);
    if (null === dropdown) {
        alert('Unable to get dropdown');
        return;
    }

    AddValueToDropdown(dropdown, '', '', 0);

    // add a custom validate function to the page
    var funcValidate = function() {
        if (0 === dropdown.selectedIndex) {
            alert("Please choose a value for " + fieldTitle + ".");
            // require a selection other than the first item (our blank value)
            return false;
        }
        return true;
    };

    var okButtons = GetOKButtons();
    for (var b = 0; b < okButtons.length; b++) {
        WrapClickEvent(okButtons[b], funcValidate);
    }
}

_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");

</script>
倚栏听风 2024-10-10 15:40:25

作为回应 Kit Menke,我对代码做了一些更改,以便它将保留下拉列表的先前值。我已将以下代码行添加到 AddValueToDropdown()....

function AddValueToDropdown(oDropdown, text, value, optionnumber){
        var selectedIndex
        if (oDropdown.options.selectedIndex)
                selectedIndex = oDropdown.options.selectedIndex;
        else
                selectedIndex = -1;

        // original code goes here

        // changed last line of code (added "selectedIndex+1")
        oDropdown.options.selectedIndex = selectedIndex+1;
}

In response Kit Menke, I've made a few changes to the code so it will persist previous value of the dropdown. I have added the following lines of code to AddValueToDropdown()....

function AddValueToDropdown(oDropdown, text, value, optionnumber){
        var selectedIndex
        if (oDropdown.options.selectedIndex)
                selectedIndex = oDropdown.options.selectedIndex;
        else
                selectedIndex = -1;

        // original code goes here

        // changed last line of code (added "selectedIndex+1")
        oDropdown.options.selectedIndex = selectedIndex+1;
}
み青杉依旧 2024-10-10 15:40:25

为了改进 Aaronster 的答案:AddValueToDropdown 可以这样完成:

var injectedBlankValue = false;
function AddValueToDropdown(oDropdown, text, value, optionnumber) {
    for (i = 0; i < oDropdown.options.length; i++) {
        option = oDropdown.options[i];
        if(option.getAttribute('selected')) // If one is already explicitely selected: we skip the whole process
            return;
    }

    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value', value);
    if (typeof (optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option, options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    // changed last line of code (added 'selectedIndex+1')
    oDropdown.options.selectedIndex = 0;
    injectedBlankValue = true;
}

这对于“添加”和“设置属性”是两个不同页面的文档库是必需的。
funcValidate 开头是:

var funcValidate = function () {
    if (!injectedBlankValue)
        return true;

所有这些更改都是为了使整个事情能够与文档库一起使用。

To improve on top of Aaronster's answer: AddValueToDropdown can be done that way:

var injectedBlankValue = false;
function AddValueToDropdown(oDropdown, text, value, optionnumber) {
    for (i = 0; i < oDropdown.options.length; i++) {
        option = oDropdown.options[i];
        if(option.getAttribute('selected')) // If one is already explicitely selected: we skip the whole process
            return;
    }

    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value', value);
    if (typeof (optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option, options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    // changed last line of code (added 'selectedIndex+1')
    oDropdown.options.selectedIndex = 0;
    injectedBlankValue = true;
}

This is needed for document libraries where "add" and "set properties" are two distinct pages.
And funcValidate starts with:

var funcValidate = function () {
    if (!injectedBlankValue)
        return true;

All these changes is to make the whole thing work with document libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文