This is a migrated thread and some comments may be shown as answers.

MultiSelect find value in Jquery

3 Answers 1105 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
Karoly
Top achievements
Rank 1
Karoly asked on 09 Jan 2019, 10:11 AM

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 

3 Answers, 1 is accepted

Sort by
0
Joana
Telerik team
answered on 11 Jan 2019, 08:03 AM
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.
0
Karoly
Top achievements
Rank 1
answered on 15 Jan 2019, 10:26 AM

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

0
Joana
Telerik team
answered on 16 Jan 2019, 10:35 AM
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.
Tags
MultiSelect
Asked by
Karoly
Top achievements
Rank 1
Answers by
Joana
Telerik team
Karoly
Top achievements
Rank 1
Share this question
or