在Sharepoint 2010中使用Jquery隐藏下拉列表

时间:2016-01-20 19:33:16

标签: javascript jquery sharepoint sharepoint-2010

我正在使用Sharepoint 2010,我试图根据当前用户的角色隐藏不同的字段。我的代码只适用于普通文本字段,但它不适用于删除列表,所以我不知道为什么会发生这种情况。

这是我的javascript代码:

function hideColumn(c) {
    $(".ms-formlabel").filter(function() {
        var thisText = $.trim($(this).clone().children().remove().end().text());
        return thisText.indexOf(c) === 0 && thisText.length === c.length;
    }).closest("tr").hide();
}

function isGroupMember(groupName, callback){
    $().SPServices({
        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: true,
        completefunc: function(xData, Status) {
            callback( !!($(xData.responseXML).find("Group[Name='" + groupName + "']").length) );
        }
    });
};

isGroupMember("Lectura-All", function(result){
    isGroupMember("Propietarios Gobierno TI y Arquitectura Empresarial",function(resultado){
        if(resultado){
            hideColumn("Días Resolución Arq. Emp");
            hideColumn("Días Resolución Comité Reducido");
            hideColumn("Días Resolución Comité Arquitectura");
        }
    });
});

修改

以下是我要隐藏的html代码的一部分,所有其他字段都等于:

<tr><td width="190px" valign="top" class="ms-formlabel"><h3 class="ms-standardheader"><nobr>Días Resolución Comité Arquitectura<span class="ms-formvalidation"> *</span></nobr></h3></td><td width="400px" valign="top" class="ms-formbody"><span dir="none"><table cellpadding="0" cellspacing="1">
                <tbody><tr>
                    <td><span title="Días Resolución Comité Arquitectura: Elegir opción"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="DropDownButton" checked="checked"></span><label class="ms-hidden" for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton">Días Resolución Comité Arquitectura: Elegir opción</label><select onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$DropDownChoice" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownChoice" title="Días Resolución Comité Arquitectura: Lista desplegable de opciones" class="ms-RadioText">
                        <option selected="selected" value="7">7</option>
                        <option value="14">14</option>
                        <option value="21">21</option>

                    </select></td>
                </tr><tr>
                    <td><span onclick="SetFocusOnControl('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01')" class="ms-RadioText" title="Días Resolución Comité Arquitectura: Especifique su propio valor:"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="FillInButton"><label for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton">Especifique su propio valor:</label></span></td>
                </tr><tr>
                    <td>&nbsp;&nbsp;&nbsp;<input onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" onfocus="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$ctl01" type="text" maxlength="255" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01" tabindex="-1" title="Días Resolución Comité Arquitectura : Especifique su propio valor:"></td>
                </tr>
            </tbody></table></span>Días Espera SLA Resolución Comité Arquitectura (para último ciclo)</td></tr>

使用这个html代码de scrip工作:

<tr>
<td width="190px" valign="top" class="ms-formlabel">
    <h3 class="ms-standardheader"><nobr>Nro EPM</nobr></h3></td>
    <td width="400px" valign="top" class="ms-formbody"><span dir="none">
        <input name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff4_1$ctl00$ctl00$TextField" type="text" value="2.535" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff4_1_ctl00_ctl00_TextField" title="Nro EPM" class="ms-input" size="11" style="ime-mode:inactive;">
        <br>
        </span>Ingresar numero de proyecto asociado a la propuesta.</td>

提前致谢。

1 个答案:

答案 0 :(得分:0)

做了一些改变,似乎有效......

首先:删除remove() - 不确定为什么会这样。
之前:$.trim($(this).clone().children().remove().end().text())
之后:$.trim($(this).clone().children().end().text())

第二:对于必填字段,星号(*)应包含在函数调用中 hideColumn("Días Resolución Comité Arquitectura *");

function hideColumn(c) {
    $(".ms-formlabel").filter(function() {
        var thisText = $.trim($(this).clone().children().end().text());
        return thisText.indexOf(c) === 0 && thisText.length === c.length;
    }).closest("tr").hide();
}

hideColumn("Días Resolución Comité Arquitectura *");
hideColumn("Nro EPM");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table><tr><td width="190px" valign="top" class="ms-formlabel"><h3 class="ms-standardheader"><nobr>Días Resolución Comité Arquitectura<span class="ms-formvalidation"> *</span></nobr></h3></td><td width="400px" valign="top" class="ms-formbody"><span dir="none"><table cellpadding="0" cellspacing="1">
                <tbody><tr>
                    <td><span title="Días Resolución Comité Arquitectura: Elegir opción"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="DropDownButton" checked="checked"></span><label class="ms-hidden" for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton">Días Resolución Comité Arquitectura: Elegir opción</label><select onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$DropDownChoice" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownChoice" title="Días Resolución Comité Arquitectura: Lista desplegable de opciones" class="ms-RadioText">
                        <option selected="selected" value="7">7</option>
                        <option value="14">14</option>
                        <option value="21">21</option>

                    </select></td>
                </tr><tr>
                    <td><span onclick="SetFocusOnControl('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01')" class="ms-RadioText" title="Días Resolución Comité Arquitectura: Especifique su propio valor:"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="FillInButton"><label for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton">Especifique su propio valor:</label></span></td>
                </tr><tr>
                    <td>&nbsp;&nbsp;&nbsp;<input onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" onfocus="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$ctl01" type="text" maxlength="255" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01" tabindex="-1" title="Días Resolución Comité Arquitectura : Especifique su propio valor:"></td>
                </tr>
            </tbody></table></span>Días Espera SLA Resolución Comité Arquitectura (para último ciclo)</td></tr>
<tr><td width="190px" valign="top" class="ms-formlabel"><h3 class="ms-standardheader"><nobr>Días Resolución Comité Reducido<span class="ms-formvalidation"> *</span></nobr></h3></td><td width="400px" valign="top" class="ms-formbody"><span dir="none"><table cellpadding="0" cellspacing="1">
                <tbody><tr>
                    <td><span title="Días Resolución Comité Reducido: Elegir opción"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="DropDownButton" checked="checked"></span><label class="ms-hidden" for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton">Días Resolución Comité Reducido: Elegir opción</label><select onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$DropDownChoice" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_DropDownChoice" title="Días Resolución Comité Reducido: Lista desplegable de opciones" class="ms-RadioText">
                        <option selected="selected" value="7">7</option>
                        <option value="14">14</option>
                        <option value="21">21</option>

                    </select></td>
                </tr><tr>
                    <td><span onclick="SetFocusOnControl('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01')" class="ms-RadioText" title="Días Resolución Comité Reducido: Especifique su propio valor:"><input id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton" type="radio" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$FillInButton" value="FillInButton"><label for="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton">Especifique su propio valor:</label></span></td>
                </tr><tr>
                    <td>&nbsp;&nbsp;&nbsp;<input onclick="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" onfocus="SetChoiceOption('ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_FillInButton')" name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff26_1$ctl00$ctl01" type="text" maxlength="255" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff26_1_ctl00_ctl01" tabindex="-1" title="Días Resolución Comité Reducido : Especifique su propio valor:"></td>
                </tr>
            </tbody></table></span>Días Espera SLA Resolución Comité Arquitectura (para último ciclo)</td></tr>
<tr>
<td width="190px" valign="top" class="ms-formlabel">
    <h3 class="ms-standardheader"><nobr>Nro EPM</nobr></h3></td>
    <td width="400px" valign="top" class="ms-formbody"><span dir="none">
        <input name="ctl00$m$g_0250d9c7_27ba_44c9_95d4_91d31951bbe6$ff4_1$ctl00$ctl00$TextField" type="text" value="2.535" id="ctl00_m_g_0250d9c7_27ba_44c9_95d4_91d31951bbe6_ff4_1_ctl00_ctl00_TextField" title="Nro EPM" class="ms-input" size="11" style="ime-mode:inactive;">
        <br>
        </span>Ingresar numero de proyecto asociado a la propuesta.</td></tr></table>