JS select option 元素的大批量添加

最近項目需要這麼一個很蛋疼的需求。

我都想知道、幹嘛要添加這麼數據到select裏面去呢、瘋了嗎(測試數據1W多條)?

結果是老總說必須加、功能沒實現是我們的問題、他們用不用得了、是他們的問題。

需求是這樣的:

用戶選擇完畢還可以刪除所選的選項:



一兩百條、添加那是毛毛雨、分分鐘搞定啊!

但是數據多了的時候就老火了、瀏覽器直接假死、白屏、下面是最初的添加方法:

頁面按鈕:

  1. <select name="sm_careReceivers" id="sm_careReceivers" 
  2. multiple="multiple" size="8"  
  3. style="width: 355px; color: #006CAD;"> 
  4. </select> 
  5. <button type="button" onclick="returnSelectCustomerInfo();">確定</button> 

JS方法:

  1. var obj = $("#sm_careReceivers"); 
  2. var count = $("#sm_careReceivers option").length; 
  3. for(var o in json){  
  4.     listText = json[o].name; 
  5.     listValue = json[o].phone; 
  6.     listTypeValue = json[o].cusid;  
  7.     listTypeText = json[o].text;    
  8.     flag = true;       
  9.     //判斷是否已存在                
  10.     for ( var i = 0; i < count; i++) { 
  11.         if (obj.get(0).options[i].value == listText + "/" 
  12.                 + listValue + "/" + listTypeValue) { 
  13.             flag = false
  14.             break
  15.         }   
  16.     } 
  17.     //給控件賦值 
  18.     if (flag) { 
  19.         if (listText == "") {    
  20.             obj.append("<option value='" + "佚名" + "/" 
  21.                     + listValue + "/" + listTypeValue + "'>" 
  22.                     + "佚名" + "/" + listValue + "/" + listTypeText 
  23.                     + "</option>"); 
  24.         } else { 
  25.             obj.append("<option value='" + listText + "/" 
  26.                     + listValue + "/" + listTypeValue + "'>" 
  27.                     + listText + "/" + listValue + "/" + listTypeText 
  28.                     + "</option>");   
  29.         } 
  30.     } 

以下是幾種優化方案:

    第一種:

    採用字符拼接方式、先把所有的option全部組裝成字符串、然後在渲染頁面:

  1. var obj = $("#sm_careReceivers"); 
  2. var count = $("#sm_careReceivers option").length; 
  3. var sHtmlTest = ""
  4. for(var o in json){  
  5.     listText = json[o].name; 
  6.     listValue = json[o].phone; 
  7.     listTypeValue = json[o].cusid;  
  8.     listTypeText = json[o].text;    
  9.     flag = true;       
  10.     //判斷是否已存在                
  11.     for ( var i = 0; i < count; i++) { 
  12.         if (obj.get(0).options[i].value == listText + "/" 
  13.                 + listValue + "/" + listTypeValue) { 
  14.             flag = false
  15.             break
  16.         }   
  17.     } 
  18.     //給控件賦值 
  19.     if (flag) { 
  20.         if (listText == "") {    
  21.             sHtmlTest+="<option value='" + "佚名" + "/" 
  22.                     + listValue + "/" + listTypeValue + "'>" 
  23.                     + "佚名" + "/" + listValue + "/" + listTypeText 
  24.                     + "</option>"
  25.         } else { 
  26.             sHtmlTest+="<option value='" + listText + "/" 
  27.                     + listValue + "/" + listTypeValue + "'>" 
  28.                     + listText + "/" + listValue + "/" + listTypeText 
  29.                     + "</option>";   
  30.         } 
  31.     } 
  32. obj.append(sHtmlTest);  

第二種:

             採用文檔碎片(該方法不支持火狐——主要是innerText屬性)

  1. var obj = $("#sm_careReceivers"); 
  2. var count = $("#sm_careReceivers option").length; 
  3. var warp = document.createDocumentFragment(); 
  4. for(var o in json){  
  5.     listText = json[o].name; 
  6.     listValue = json[o].phone; 
  7.     listTypeValue = json[o].cusid;  
  8.     listTypeText = json[o].text;    
  9.     flag = true;       
  10.     //判斷是否已存在                
  11.     for ( var i = 0; i < count; i++) { 
  12.         if (obj.get(0).options[i].value == listText + "/" 
  13.                 + listValue + "/" + listTypeValue) { 
  14.             flag = false
  15.             break
  16.         }   
  17.     } 
  18.     //給控件賦值 
  19.     if (flag) { 
  20.         var objOption = document.createElement("OPTION"); 
  21.         objOption.value = listText + "/" + listValue + "/" + listTypeValue; 
  22.         objOption.innerText = listText + "/" + listValue + "/" + listTypeText; 
  23.         warp.appendChild(objOption); 
  24.     } 
  25. obj.append(warp); 

第三種:

              該方案結合第二個方案、使用setTimeout用setTimeout,每50個,就setTimeout一下、讓出cpu渲染瀏覽器、防止頁面假死:

  1. var obj = $("#sm_careReceivers"); 
  2. var count = $("#sm_careReceivers option").length; 
  3. var warp = document.createDocumentFragment(); 
  4. var number = 0; 
  5. for(var o in json){  
  6.     number++; 
  7.     listText = json[o].name; 
  8.     listValue = json[o].phone; 
  9.     listTypeValue = json[o].cusid;  
  10.     listTypeText = json[o].text;    
  11.     flag = true;       
  12.     //判斷是否已存在                
  13.     for ( var i = 0; i < count; i++) { 
  14.         if (obj.get(0).options[i].value == listText + "/" 
  15.                 + listValue + "/" + listTypeValue) { 
  16.             flag = false
  17.             break
  18.         }   
  19.     } 
  20.     //給控件賦值 
  21.     if (flag) { 
  22.         var objOption = document.createElement("OPTION"); 
  23.         objOption.value = listText + "/" + listValue + "/" + listTypeValue; 
  24.         objOption.innerText = listText + "/" + listValue + "/" + listTypeText; 
  25.         warp.appendChild(objOption); 
  26.         if(number==50||o==(json.length-1)){ 
  27.             number = 0 ; 
  28.             fooAddNewSelectTest(obj,warp);  
  29.             warp = document.createDocumentFragment();    
  30.         } 
  31.     } 
  32. obj.append(warp);  

這樣過後、內容到是加載進去了、但是去選擇刪除的時候、同樣蛋疼......

o(︶︿︶)o 唉!

不知不覺、又要下班了、回家ing。

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章