0

I am trying to add the multi-select filter to my "PROVIDER" column in the jqGrid. I am able to add the normal select filter with the single selection but now I am converting it to the multi-select filter. I referred to a few old posts here and tried to do the same. It's not throwing me any error but it is not creating the multi-select filter also. Please let me know what I am doing wrong below. I am able to get the unique values and able to create the SELECT list, I am guessing something is wrong with function dataInitMultiselect because I tried to console.log(elem) but it's not returning anything, not even undefined but the function is getting called because its not throwing me undefined function error.

Also I have added the CDN links for Bootstrap Multiselect still no luck and no error. I am using following jqgrid and Bootstrap CDN links:

    <!-- JQGRID CDN-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/free-jqgrid@4.15.4/css/ui.jqgrid.min.css">
<script src="https://cdn.jsdelivr.net/npm/free-jqgrid@4.15.4/js/jquery.jqgrid.min.js"></script>
<link   rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css">


    <!--Juery CDN and Bootstrap CDN-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css">    
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>

tried to console.log() the data near the return of buildSearchSelect function and it is returning me data properly by forming the SELECT like this.

Movie Web:Movie Web;Metacritic:Metacritic;Cinema Blend:Cinema Blend;Boxofficemojo:Boxofficemojo

So I am guessing everything seems to be working correctly and data is getting added to

value: buildSearchSelect(getUniqueNames(columnName, data,grid)),

Following is the JQGRID program which I am using:

$("#home_grid").jqGrid({
    url: "/URL_TO_FETCH_DATA",
    datatype: "json",
    mtype: "GET",
    colNames: ["PROVIDER", "Title","Original Publish Time", "Fetch Time"],
    colModel:
    [
        {
            name    : "PROVIDER",
            align   : "center",
            width   : "120%",
            search  : true
        },
        {
            name    : "TITLE",
            align   : "center",
            search  : true,
            width   : "250%",
            formatter: Title_Url_Bind 
        },
        {
            name        : "PUBLISH_TIME",
            align       : "center",
            width       : "130%",
            search      : true,
            sorttype    : "datetime"

        },
        {
            name        : "DB_ENTRY_TIME",
            align       : "center",
            width       : "130%",
            sortable    : true,
            sorttype    : "datetime"
        }
    ],
    pager       : "#home_pager",
    loadonce    : true,
    shrinkToFit : true,
    rowNum      : 10,
    autoHeight  : true,
    rowList     : [10, 15, 20, 25, 50],
    sortable    : true,
    viewrecords : true,
    toolbar     : [true, "top"],
    autowidth   : true,
    beforeProcessing: beforeProcessingHandler1,
});

function beforeProcessingHandler1(data) {
    initializeGridFilterValueDem(data);
}

initializeGridFilterValueDem = function (data) {
    setSearchSelect("PROVIDER", jQuery("#home_grid"), data);
}

setSearchSelect = function (columnName, grid,data) {
    grid.jqGrid('setColProp', columnName,
        {
            searchoptions: {
                clearSearch: false,
                sopt: ['eq', 'ne'],
                value: buildSearchSelect(getUniqueNames(columnName, data,grid)),
                attr: { multiple: 'multiple', size: 7},
                dataInit: dataInitMultiselect
            }
        }
    );
}

buildSearchSelect = function (uniqueNames) {
    var values = "";
    $.each(uniqueNames, function () {
        values += this + ":" + this + ";";
    });
    return values.substring(0, values.length - 1);
}

getUniqueNames = function (columnName, mydata_parm, grid) {

    var texts = grid.jqGrid("getGridParam", "data");

    uniqueTexts = [], textsLength = texts.length, text, textsMap = {}, i;

    for (i = 0; i < textsLength; i++) {
        text = texts[i];
        if (text !== undefined && textsMap[text] === undefined) {
            // to test whether the texts is unique we place it in the map.
            textsMap[text] = true;
            uniqueTexts.push(text);
        }
    }

    return uniqueTexts;
}

dataInitMultiselect = function (elem) {
    console.log(elem);
    setTimeout(function () {
        var $elem = $(elem), id = elem.id,
            inToolbar = typeof id === "string" && id.substr(0, 3) === "gs_",
            options = {
                selectedList: 2,
                height: "auto",
                checkAllText: "all",
                uncheckAllText: "no",
                noneSelectedText: "Any",
                open: function () {
                    var $menu = $(".ui-multiselect-menu:visible");
                    $menu.width("auto");
                    return;
                }
            },
            $options = $elem.find("option");
        if ($options.length > 0 && $options[0].selected) {
            $options[0].selected = false; // unselect the first selected option
        }
        if (inToolbar) {
            options.minWidth = 'auto';
        }
        //$elem.multiselect(options);
        $elem.multiselect(options).multiselectfilter({ placeholder: '' });
        $elem.siblings('button.ui-multiselect').css({
            width: inToolbar ? "98%" : "100%",
            marginTop: "1px",
            marginBottom: "1px",
            paddingTop: "3px"
        });
    }, 50);

};

Please help me with the resolution guys.

  • Your question isn't clear enough. Which CSS framework you want to use? jQuery UI CSS or Bootstrap CSS? In which version (Bootstrap 3 and Bootstrap 4 are very different)? You can include only one version of JavaScript library and think about dependencies. jqGrid needs **full** version of jQuery. Thus you have to include full version of jQuery **before** `jquery.jqgrid.min.js`. Bootstrap 4 can work with full or slim version of jQuery. Including of `jquery-3.3.1.slim.min.js` and then `jquery-2.2.4.min.js` is wrong. – Oleg Jun 16 '19 at 10:51
  • You have to include for example `jquery-3.3.1.min.js` before `jquery.jqgrid.min.js` and then don't include any other versions jQuery later (neither `jquery-3.3.1.slim.min.js` nor `jquery-2.2.4.min.js`). You use bootstrap-multiselect version 0.9.13, which is published at May 26, 2015 (see https://github.com/davidstutz/bootstrap-multiselect/tree/v0.9.13). Thus it should be developed and tested for Bootstrap 3 and not Bootstrap 4 (Bootstrap 4.0.0 is published at Jan 18, 2018). – Oleg Jun 16 '19 at 10:59
  • To be able to help you one need to create **the demo**, which uses your code. It would be better if you post JSFiddle demo directly yourself and other will need only modify it. For creating the demo one required some test data. The current question contains only `"/URL_TO_FETCH_DATA"`, which get no information about format of returned data. – Oleg Jun 16 '19 at 11:02
  • Finally, if you use free-jqgrid fork, that you don't need to use `getUniqueNames` or `buildSearchSelect`. Instead of that free jqGrid supports `generateValue: true` property of `searchoptions` and `sopt: ["in"]` which can essentially simplify the code. By default "in" operation expect comma separated data, but using `inFilterSeparator` option of jqGrid you can change the separator. You can try `searchoptions: { generateValue: true, sopt: ["in"], attr: { multiple: "multiple", size: 7 }, dataInit: dataInitMultiselect }` – Oleg Jun 16 '19 at 11:11
  • @Oleg I tried to create the multi-select using bootstrap but you can ignore it now I am trying to create using the jqGrid itself. As per the data returned it is in JSON format and its populating the jqGrid correctly and I am able to create `SELECT` using just single value but now I need `Multi-Select` for my `Provider` column. What does the `size` indicate in `searchoptions: { generateValue: true, sopt: ["in"], attr: { multiple: "multiple", size: 7 }, dataInit: dataInitMultiselect }` –  Jun 17 '19 at 20:54
  • You can remove `dataInit: dataInitMultiselect` to see the meaning of `size: 7` attribute. It has mostly sense in old web browser (IE). The standard ` – Oleg Jun 17 '19 at 21:16

0 Answers0