Search code examples
jqueryjqgrid

How to create generic method using jquery


I have implemented JQgrid with excel like filter functionality. Here I have dropdown list on search toolbar. I need help to remove duplicate code which I have used while creating dynamic dropdown list. code shown in below:

    // 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 //

As mention in above code, I want one function instead of duplicate code. Here I have four different Id's with different parameters. I want one function where i can pass only parameters & depends on this parameters values will be set. please guide me how can I implement this & reduce redundant data from code. Thanks in advance.


Solution

  • I have created the below function instead of repeating the same data & resolved my problem. May helpful to anyone. Please find code for your reference.

    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
                        }
                    })
          });
    };