MultiSelect find value in Jquery

4 posts, 0 answers
  1. Karoly
    Karoly avatar
    2 posts
    Member since:
    Sep 2017

    Posted 09 Jan Link to this post

    My question is How can I find value and select it with jquery?

    I have a textarea and the user put a list to it.

    I have to check one by one the item is exist in the multiselect and select it.

    Thanks, Károly 

  2. Joana
    Admin
    Joana avatar
    205 posts

    Posted 11 Jan Link to this post

    Hi Karoly,

    I suggest that you use the value method of the Kendo UI Multiselect and pass an array of values as parameter. I believe the easiest approach that will work with any kind of binding, would be to:

    1. Get the dataSource items
    2. Filter them to get only the dataItems that should be selected
    3. Get their value and pass it to the value method

    I have prepared a dummy sample that illustrate the approach:

    https://dojo.telerik.com/enUSIMUm

    var multiselect = $("#multiselect").data("kendoMultiSelect");
     
    var values = $("#multiselect").data("kendoMultiSelect").dataSource.data().filter(function (dataItem) {
        return dataItem.text == "Item1" || dataItem.text == "Item7";
    }).map(function (item) {
        return item.value;
    });
     
    multiselect.value(values);

    Let me know if such approach would fit to your scenario.

    Regards,
    Joana
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Karoly
    Karoly avatar
    2 posts
    Member since:
    Sep 2017

    Posted 15 Jan in reply to Joana Link to this post

    Hi Joana,

    finaly I solved it on other way. I dont't no it is worse or not but it works now. :)

     $(function () {
                $('#textarea').bind('paste',
                    function (e) {

                        var ctl = $(this);
                        var multiselect = $("#EmployeeIds").data("kendoMultiSelect");

                        setTimeout(function () {

                            //multiselect.search(ctl.val());
                            var items = multiselect.dataSource._data;
                            var list = ctl.val().replace(",", "\n").split("\n");
                            //alert(ctl.val());
                            var selected = multiselect.value();
                            var notSelected = [];
                            for (var j = 0; j < list.length; j++) {
                                for (var i = 0; i < items.length; i++) {
                                    if (list[j].trim().toLowerCase() === items[i].DisplayName.split("(")[0].trim().toLowerCase()) {
                                        selected.push(items[i].EmployeeId);
                                        break;
                                    }
                                    if (i === items.length - 1)
                                        notSelected.push(list[j].trim());
                                };
                            };

                            multiselect.value(selected);
                            multiselect.dataSource.filter({});
                            if (notSelected.length > 0) {
                                alert("Nem kiválasztott dolgozók:" + notSelected);
                            } else alert("Minden dolgozó kiválasztva!");
                        },
                            100);

                        //$("#EmployeeIds").getKendoMultiSelect().value(["74", "78"]);
                    });
            });

     

    Thanks, Karoly

  4. Joana
    Admin
    Joana avatar
    205 posts

    Posted 16 Jan Link to this post

    Hello Karoly,

    Thank you for sharing your solution.

    I am glad that you've managed to fulfill your scenario.

    Regards,
    Joana
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top