Search code examples
c#asp.netangularjsasp.net-coreasp.net-core-2.2

JQuery is not working on input tag if Angularjs ng-repeat is used


I'm using angularjs with asp.net core 2.2. The jQuery in the theme which i am using is conflicting with the angularjs. when angular removed, jQuery works fine. I am placing bootstrap select in ng-repeat, it does not work.

When angularjs ng-repeat on table is removed, all jQuery elements work properly otherwise not.

This is my HTML Code

 <div class="kt-portlet">
    <div class="kt-portlet__head">
        <div class="kt-portlet__head-label">
            <h3 class="kt-portlet__head-title">
                Suit Information
            </h3>
        </div>
    </div>
    <div class="kt-portlet__body">
        <!--begin::Section-->
        <div class="kt-section">
            <div class="kt-section__content">
                <table class="table">
                    <thead class="thead-dark">
                        <tr>
                            <th style="text-align:center; vertical-align:middle;">
                                <label class="kt-checkbox kt-checkbox--bold">
                                    <input type="checkbox"
                                           ng-model="selectedAll" 
                                           ng-click="CheckAll()">
                                    <span></span>
                                </label>
                            </th>
                            <th>Suit Type</th>
                            <th>Quantity</th>
                            <th>Stitching Price</th>
                            <th>Receiving Date</th>
                            <th>Delivery Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in SuitList">
                            <td>
                                <label class="kt-checkbox kt-checkbox--bold kt-checkbox--brand">
                                    <input type="checkbox" ng-model="item.selected">
                                    <span></span>
                                </label>
                            </td>
                            <td>
                                <div class="col-lg-12">
                                    <select class="form-control kt-selectpicker" title="Brand" data-style="btn-brand">
                                        <option>Mustard</option>
                                        <option>Ketchup</option>
                                        <option>Relish</option>
                                    </select>
                                    <span class="form-text text-muted">Please select Suit Type</span>
                                </div>
                            </td>
                            <td>

                                <input id="" type="number" min="0" class="form-control" value="0" name="demo1" placeholder="Price">
                                <span class="form-text text-muted">Please enter Quantity</span>

                            </td>
                            <td>
                                <input id="" type="number" min="0" class="form-control" value="0"
                                       name="demo1" placeholder="Price">
                                <span class="form-text text-muted">Please enter stitching price</span>
                            </td>

                            <td>
                                <div class="input-group date">
                                    <input type="date" class="form-control" placeholder="Select date" id="" />
                                    <div class="input-group-append">
                                        <span class="input-group-text">
                                            <i class="la la-calendar-check-o"></i>
                                        </span>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="input-group date">
                                    <input type="date" class="form-control" placeholder="Select date" id="" />
                                    <div class="input-group-append">
                                        <span class="input-group-text">
                                            <i class="la la-calendar-check-o"></i>
                                        </span>
                                    </div>
                                </div>
                            </td>
                        </tr>

                    </tbody>
                </table>
                <input ng-hide="!SuitList.length" type="button" class="btn btn-danger pull-right" ng-click="Remove()" value="Remove Selected">
                <input type="button" class="btn btn-primary addnew pull-right" value="Add New" ng-click="AddNew()" style="margin-right:3px">
            </div>
        </div>
        <!--end::Section-->
    </div>
</div>

This is jQuery code

var KTBootstrapSelect = function () {
    var demos = function () {
        // minimum setup
        $('.kt-selectpicker').selectpicker();
    }

    return {
        // public functions
        init: function() {
            demos(); 
        }
    };
}();

jQuery(document).ready(function() {
    KTBootstrapSelect.init();
});

This is Angular JS code

$scope.AllowAddNewRow = true;
$scope.selectedAll = false;

$scope.AddNew = function () {
    if ($scope.SuitList.length == 0) {
        $scope.SuitList.push({
            'Suit_Id': 0,
            'SuitType_Id': 0,
            'Suit_Quntity': 0,
            'Suit_Stiching': 0,
            'Order_Receiving_Date': "",
            'Order_Delivery_Date': "",
        });
    }
    for (var i = 0; i < $scope.SuitList.length; i++)
    {
        if ($scope.SuitList[i].Suit_Quntity == 0 || $scope.SuitList[i].Suit_Stiching == 0 || $scope.SuitList[i].Order_Receiving_Date == "" || $scope.SuitList[i].Order_Delivery_Date == "")
        {
            $scope.AllowAddNewRow = false;
            return;
        }
        else
            $scope.AllowAddNewRow = true;
    }
    if ($scope.AllowAddNewRow == true) {
        $scope.SuitList.push({
            'Suit_Id': 0,
            'SuitType_Id': 0,
            'Suit_Quntity': 0,
            'Suit_Stiching': 0,
            'Order_Receiving_Date': "",
            'Order_Delivery_Date': "",
        });
    }
};

I expected multiple rows with jQuery functionality but can't get.


Solution

  • you need to create a angularjs directive to initialize the selectpicker like this

    angular.module('app').directive("ktSelectpicker", [
            () => {
                return {
                    restrict: "C",
                    link: ($scope, element) => {
                        $(element).selectpicker();
                    }
                }
            }
        ])
    

    replace 'app' with your module name