2017-09-07 1 views
0

저는 필터 기능처럼 Excel과 함께 JQgrid를 구현했습니다. 여기 검색 툴바에 드롭 다운 목록이 있습니다. 내가 동적 드롭 다운 목록을 만드는 동안 사용했던 중복 코드를 제거하는 데 도움이 필요합니다. 아래 그림 코드 : 위의 코드에서 언급으로jquery를 사용하여 제네릭 메서드를 만드는 방법

// Start ---- Dropdown filter Implementation // 
    beforeProcessing: function (data) { 
     var sourceLocaleMap = {}, sourceLocaleValues = ":All", rows = data.rows, i, sourceLocale, sourceLocaleName; 
     for (i = 0; i < rows.length; i++) { 
      sourceLocale = rows[i].SourceLocaleId; 
      sourceLocaleName = rows[i].SourceLocaleName; 
      if (!sourceLocaleMap.hasOwnProperty(sourceLocale)) { 
       sourceLocaleMap[sourceLocale] = 1; 
       sourceLocaleValues += ";" + sourceLocale + ":" + sourceLocaleName; 
      } 
     } 
     $(this).jqGrid("setColProp", 'SourceLocaleId', { 
      stype: "select", 
      searchoptions: { 
       value: sourceLocaleValues.substring(5), 
       sopt: ["eq", "ne"], 
       clearSearch: false, 
       attr: { multiple: 'multiple', size: 29 }, 
       dataInit: dataInitMultiselect 
      } 
     }) 

     var CompanyMap = {}, CompanyValues = ":All", rows = data.rows, i, Company, CompanyName; 
     for (i = 0; i < rows.length; i++) { 
      Company = rows[i].CompanyId; 
      CompanyName = rows[i].CompanyName; 
      if (!CompanyMap.hasOwnProperty(Company)) { 
       CompanyMap[Company] = 1; 
       CompanyValues += ";" + Company + ":" + CompanyName; 
      } 
     } 
     $(this).jqGrid("setColProp", 'CompanyId', { 
      stype: "select", 
      searchoptions: { 
       value: CompanyValues.substring(5), 
       sopt: ["eq", "ne"], 
       clearSearch: false, 
       attr: { multiple: 'multiple', size: 2 }, 
       dataInit: dataInitMultiselect 
      } 
     }) 

     var CurrencyMap = {}, CurrencyValues = ":All", rows = data.rows, i, Currency, CurrencyName; 
     for (i = 0; i < rows.length; i++) { 
      Currency = rows[i].CurrencyId; 
      CurrencyName = rows[i].CurrencyName; 
      if (!CurrencyMap.hasOwnProperty(Currency)) { 
       CurrencyMap[Currency] = 1; 
       CurrencyValues += ";" + Currency + ":" + CurrencyName; 
      } 
     } 
     $(this).jqGrid("setColProp", 'CurrencyId', { 
      stype: "select", 
      searchoptions: { 
       value: CurrencyValues.substring(5), 
       sopt: ["eq", "ne"], 
       clearSearch: false, 
       attr: { multiple: 'multiple', size: 17 }, 
       dataInit: dataInitMultiselect 
      } 
     }) 

     var LocaleMap = {}, LocaleValues = ":All", rows = data.rows, i, Locale, LocaleName; 
     for (i = 0; i < rows.length; i++) { 
      Locale = rows[i].LocaleId; 
      LocaleName = rows[i].LocaleName; 
      if (!LocaleMap.hasOwnProperty(Locale)) { 
       LocaleMap[Locale] = 1; 
       LocaleValues += ";" + Locale + ":" + LocaleName; 
      } 
     } 
     $(this).jqGrid("setColProp", 'LocaleId', { 
      stype: "select", 
      searchoptions: { 
       value: LocaleValues.substring(5), 
       sopt: ["eq", "ne"], 
       clearSearch: false, 
       attr: { multiple: 'multiple', size: 2 }, 
       dataInit: dataInitMultiselect 
      } 
     }).jqGrid('destroyFilterToolbar') 
      .jqGrid('filterToolbar', { 
       stringResult: true, 
       searchOnEnter: false, 
       defaultSearch: "cn" 
      }); 
    }, 
    // End ---- Dropdown filter Implementation // 

, 나는 하나 개의 함수 대신 중복 된 코드를합니다. 여기에는 다른 매개 변수가있는 4 개의 다른 ID가 있습니다. 내가 매개 변수를 전달할 수있는 하나의 함수가 필요하다. &이 매개 변수 값에 따라 달라진다. 제발 날이 & 코드에서 중복 데이터를 줄일 구현할 수 있습니다. 미리 감사드립니다.

답변

0

동일한 데이터를 반복하지 않고 아래 기능을 작성했습니다 & 내 문제가 해결되었습니다. 누구에게나 도움이 될 수 있습니다. 참조 용 코드를 찾으십시오.

getUniqueNames = function (columnName, data) { 
    var textsMap = {}, Values = ":All", rows = data.rows, i, textKey, textValue; 
    var columnValue = columnName.substring(0, columnName.length - 2) + 'Name'; 

    //Ascending order sorting 
    rows.sort(compare); 
    function compare(a, b) { 
     if (a[columnValue] < b[columnValue]) 
      return -1; 
     if (a[columnValue] > b[columnValue]) 
      return 1; 
     return 0; 
    } 

    for (i = 0; i < rows.length; i++) { 
     textKey = rows[i][columnName]; 
     textValue = rows[i][columnValue]; 
     if (!textsMap.hasOwnProperty(textKey)) { 
      textsMap[textKey] = 1; 
      Values += ";" + textKey + ":" + textValue; 
     } 
    } 
    return Values; 
}, 

setSearchSelect = function (columnName, data) { 
    $('#list2').jqGrid('setColProp', columnName, 
       { 
        stype: 'select', 
        searchoptions: { 
         value: (getUniqueNames(columnName, data)), 
         sopt: ["eq", "ne"], 
         clearSearch: false, 
         sortorder: 'asc', 
         attr: { multiple: 'multiple' }, 
         dataInit: dataInitMultiselect 
        } 
       }) 
     }); 
}; 
관련 문제