Tuesday, 18 June 2013

Multi Selection Combobox in Kendo UI


<div>
<input id="product" placeholder="Select product..." />
    <br />
<input id="hf_product" type="hidden" />
    Selected values : <span id="spanproduct"></span>
</div>
<script type="text/javascript">
    $(document).ready(function () {

        $("#fabric").kendoComboBox({
            name: "fabric",
            dataTextField: "DataText",
            dataValueField: "DataValue",
            dataSource: [
                            { DataText: "Select All", DataValue: "0" },
                            { DataText: "Cotton", DataValue: "1" },
                            { DataText: "Polyester", DataValue: "2" },
                            { DataText: "Cotton/Polyester", DataValue: "3" },
                            { DataText: "Rib Knit", DataValue: "4" }
                        ],
            filter: "contains",
            suggest: true,
            template: "<input type='checkbox' id='chk_fabric_#=data.DataValue #' onclick='UpdateIdinHF(this);' value='#=data.DataValue #' name='fabric' />" + " " + "${ data.DataText }",
            close: onClose,
            change: onChange
        });

    });

    var IsItemChecked = false;
    function UpdateIdinHF(obj) {

        var id = $(obj).attr('id');
        var name = $(obj).attr('name');
        var value = parseInt($(obj).attr('value'));
        var IsChecked = $(obj).is(':checked');
        var hf = $("#hf_" + name).get(0);

        if (value != 0) {
            UpdateIdInHiddenField(hf, value, IsChecked);

            var totalchk = $('input[id*="chk_' + name + '"]').not("#chk_" + name + "_0").length;
            var checkedchk = $('input[id*="chk_' + name + '"]:checked').not("#chk_" + name + "_0").length;

            if (totalchk == checkedchk) {
                $("#chk_" + name + "_0").prop("checked", true);
            }
            else {
                $("#chk_" + name + "_0").prop("checked", false);
            }
        }
        else {
            $('input[id*="chk_' + name + '"]').each(function () {
                if (parseInt($(this).val()) != 0) {
                    if (IsChecked == true) {
                        $(this).prop("checked", true);
                        UpdateIdInHiddenField(hf, $(this).val(), IsChecked);
                    }
                    else {
                        $(this).prop("checked", false);
                        UpdateIdInHiddenField(hf, $(this).val(), IsChecked);
                    }
                }
            });
        }
        IsItemChecked = true;
    }
    function onClose(e) {
        if (IsItemChecked == true) {
            IsItemChecked = false;
            e.preventDefault();
        }
        else {
            ShowSelectedItem();
        }
    }
    function ShowSelectedItem() {
        $("#spanfabric").html($("#hf_fabric").val());
    }

    function UpdateIdInHiddenField(hf, id, IsAdd) {

        if (hf.value == "") {
            hf.value = ",";
        }

        if (IsAdd == true) {
            if (hf.value.indexOf("," + id + ",") == -1) {
                hf.value = hf.value + id + ",";
            }
        }
        else if (IsAdd == false) {
            if (hf.value.indexOf("," + id + ",") >= 0) {
                hf.value = hf.value.replace("," + id + ",", ",");
            }
        }
    }
    function onChange(e) {
        e.sender.value(null);
    }
</script>

If in your page combobox is used more then once then please check below code snippet:
<div>
<input id="product" placeholder="Select product..." />
    <br />
<input id="hf_product" type="hidden" />
    Selected values : <span id="spanproduct"></span>
</div>
<script type="text/javascript">
    $(document).ready(function () {

        $("#product").kendoComboBox({
            name: "product",
            dataTextField: "DataText",
            dataValueField: "DataValue",
            dataSource: [
                            { DataText: "Select All", DataValue: "0" },
                            { DataText: "Cotton", DataValue: "1" },
                            { DataText: "Polyester", DataValue: "2" },
                            { DataText: "Cotton/Polyester", DataValue: "3" },
                            { DataText: "Rib Knit", DataValue: "4" }
                        ],
            filter: "contains",
            suggest: true,
            template: "<input type='checkbox' id='chk_product_#=data.DataValue #' onclick='UpdateIdinHF(this);' value='#=data.DataValue #' name='product' />" + " " + "${ data.DataText }",
            close: onClose,
            change: onChange
        });

    });
</script>
LIVE DEMO

1 comment:

  1. You done great job but it does not work in Kendo UI v2018.1.221 version, so could you please share your suggestion for the same.

    ReplyDelete