【问题标题】:how to Delete duplicate items on asp:dropdownlist, Sharepoint Designer 2013如何删除 asp:dropdownlist、Sharepoint Designer 2013 上的重复项目
【发布时间】:2014-05-19 07:15:12
【问题描述】:

我在 dataviewwebpart 上使用了 asp:drowdownlist 并与源 spdatasource1 绑定。 它有多个重复项。我怎样才能删除那些重复的项目

asp:DropDownList runat="server" id="DropDownList1" DataValueField="ID" DataTextField="ProgramMaster" DataSourceID="spdatasource1" AutoPostBack="False" AppendDataBoundItems="True" ToolTip="Select a Program from the list"/>

此外,它正在显示以下编队 ID 中的项目;#ProgramName。我怎样才能只获得程序名称。

【问题讨论】:

    标签: javascript jquery sharepoint-2013 sharepoint-designer


    【解决方案1】:

    我使用 JQuery 从 asp:dropdownlist 中删除重复项,如果有人可能需要,这里是代码。该代码分为四个部分,从下拉列表中获取值,去除重复项并以可用形式获取值,从下拉列表中删除现有值并最后设置或将值附加回下拉列表。

    <script type="text/javascript">
    $(document).ready(function(){
    var handles= [];               
    $('#DropDownList1 option').each(function() { 
    var Prog1 = $(this).attr('text');
    if(Prog1 != 'All'){
    var Prog2 = Prog1.split(';#');
    handles.push('All');
    handles.push(Prog2[1]);
     }
    //Remove The existed Dropdownlist value
    $("select#DropDownList1 option[value='" + $(this).val() + "']").remove();
    //$(this).val().remove();
    });
    //Removing the Duplicates
    var individual = [];
    for (var i = 0; i<handles.length; i++) {
    if((jQuery.inArray(handles[i], individual )) == -1)
    {
    individual .push(handles[i]);
    } 
     }
    //Inserting or setting the value from array individual to the dropdownlist.  
    var sel =   document.getElementById('DropDownList1');
    for(var i = 0; i < individual.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = individual[i];
    opt.value = individual[i];
    sel.appendChild(opt);
        }
     });
    </script>
    

    P.S 如果给定的 ID 不能正常用于下拉菜单,请从 IE 调试工具获取 ID,格式为 ctl00_PlaceHolderMain_g_a0a2fb36_2203_4d2e_bcd4_6f42243b880f_DropDownList1

    【讨论】:

      【解决方案2】:

      你可以用 jquery 做到这一点

      var usedNames = {};
      $("select[name='company'] > option").each(function () {
      if(usedNames[this.text]) {
          $(this).remove();
      } else {
          usedNames[this.text] = this.value;
      }
      });
      

      或者使用服务器端代码试试这个

      void RemoveDuplicateItems(DropDownList ddl)
      {
      
      for (int i = 0; i < ddl.Items.Count; i++)
      {
      
      ddl.SelectedIndex = i;
      string str = ddl.SelectedItem.ToString();
      for (int counter = i + 1; counter < ddl.Items.Count; counter++)
      {
      ddl.SelectedIndex = counter;
      string compareStr = ddl.SelectedItem.ToString();
      if (str == compareStr)
       {
        ddl.Items.RemoveAt(counter);
        counter = counter - 1;
       }  
      
      
      }    }     }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        • 1970-01-01
        • 2014-04-26
        相关资源
        最近更新 更多