add.js 10.6 KB

angular.module("xn/template/add/add.html",[])
    .run(["$templateCache", function($templateCache) {
        "use strict";
        $templateCache.put("xn/template/add/searchAdd.html",
            "<div class=\"xn-add clearfix\"  id=\"{{addId}}\">"+
                "<ul class=\"add-ul clearfix\">"+
                    "<li ng-repeat=\"added in addedList track by $index\">"+
                    "<span>{{added}}</span><div class=\"close\" ng-click=\"removeData($index, $event)\"></div>"+
                    "</li>"+
                "<div class=\"add-btn\" ng-click=\"openSearchBox()\" >+</div>"+
                "</ul>"+
                "<div class=\"search-box clearfix\" ng-show=\"vm.showDiv\">"+
                    "<div class='clearfix'>"+
                        "<input ng-keydown=\"find($event)\" class=\"search-input\" type='text' value='' ng-model=\"param.keyword\"  placeholder='{{vm.selectName}}'/>"+
                        "<div class=\"search-btn\" ng-click=\"search('search', $event)\"></div>"+
                    "</div>"+
                    "<ul class=\"source-ul clearfix\">" +
                    "<li ng-repeat='data in dataList track by $index' ng-click=\"chooseData(data)\">" +
                        "<span title='{{data.setValue}}'>{{data.setValue | substr:15}}</span></li>"+
                    "<div class='right-bg'>" +
                    "<div class='pagination'>"+
                    "<div class='icon icon-up' ng-if=\"param.pageNumber> 1\" ng-click='search(\"pre\", $event)'></div>"+
                    "<div class='icon icon-up icon-up-disable' ng-if=\"param.pageNumber<=1\"></div>"+
                    "<div class='icon icon-down' ng-if=\"param.pageNumber<totalCount/param.pageSize\" ng-click='search(\"next\", $event)'></div>"+
                    "<div class='icon icon-down icon-down-disable' ng-if=\"param.pageNumber>=totalCount/param.pageSize\"></div>"+
                    " </div>"+
                    "</div>"+
                    "</ul>"+
                "</div>"+
            "</div>"
        );
    }]);
angular.module("xn.directive.add",["xn/template/add/add.html"])
    .directive("xnSelectAdd",["$timeout", function($timeout) {
        return {
            restrict:"AE",
            replace:"true",
            scope:{
                addList:"=addList",
                options:"=options",
                outData :"=ngModel"
            },
            required: "?ngModel",
            templateUrl: "xn/template/add/searchAdd.html",
            link: function( scope,element, attrs, ngModel) {
                scope.outData=[];
                scope.addedList=[];
                scope.dataList=[];
                scope.vm={
                    selectName:"请输入搜索内容",
                    showDiv:false
                };
                scope.param={
                    pageNumber:scope.options.pageNumber,
                    pageSize:scope.options.pageSize,
                    keyword:""
                };
                scope.totalCount=0;
                if(angular.isArray(scope.addList)&&scope.addList.length>0){
                    scope.outData=[];
                    scope.addedList=[];
                    if(angular.isArray(scope.options.parameter)){
                        angular.forEach(scope.addList,function(item){
                            scope.addedList.push(item[scope.options.parameter[0]]);
                        });
                    }else{
                        angular.forEach(scope.addList,function(item){
                            scope.addedList.push(item.name);
                        });
                    }
                    scope.outData=scope.addList;
                }else {
                    scope.addList=[];
                    scope.outData=[];
                    var first=true;
                    scope.$watch(function(){
                        return	scope.addList;
                    },function(newData){
                        if(first&&newData&&angular.isArray(newData)&&newData.length>0){
                            scope.addList=newData;
                            scope.outData=[];
                            scope.addedList=[];
                            if(angular.isArray(scope.options.parameter)){
                                angular.forEach(scope.addList,function(item){
                                    scope.addedList.push(item[scope.options.parameter[0]]);
                                });
                            }else{
                                angular.forEach(scope.addList,function(item){
                                    scope.addedList.push(item.name);
                                });
                            }
                            scope.outData=scope.addList;
                            first = false;
                        }
                    })
                }
                if(!scope.required){
                    scope.required = false;
                }

                //打开搜索框
                scope.openSearchBox=function(){
                    scope.vm.showDiv = !scope.vm.showDiv;
                    if(scope.vm.showDiv){
                        scope.dataList=[];
                        scope.options.methodName(scope.param).success(function (data) {
                            scope.totalCount = data.totalCount;
                            //console.log(scope.totalCount);
                            var data = eval(data.result);
                            if(angular.isArray(scope.options.parameter)){
                                angular.forEach(data,function(item){
                                    item.setValue=item[scope.options.parameter[0]];
                                    item.setKey=item[scope.options.parameter[1]];
                                });
                            }else{
                                angular.forEach(data,function(item){
                                    item.setValue=item.name;
                                    item.setKey=item.id;
                                });
                            }
                            scope.dataList =data;
                        });
                    }
                    $timeout(function(){element.find('.search-input').focus();}, 0, false);


                };
                //关闭搜索框
                scope.close=function(){
                    scope.vm.showDiv=false;
                    scope.param.keyword="";
                };
                scope.find = function(e) {
                    if(event.keyCode==13){
                        e.preventDefault();
                        scope.options.methodName(scope.param).success(function (data) {
                            scope.totalCount = data.totalCount;
                            var data = eval(data.result);

                            if(angular.isArray(scope.options.parameter)){
                                angular.forEach(data,function(item){
                                    item.setValue=item[scope.options.parameter[0]];
                                    item.setKey=item[scope.options.parameter[1]];
                                });
                            }else{
                                angular.forEach(data,function(item){
                                    item.setValue=item.name;
                                    item.setKey=item.id;
                                });
                            }
                            scope.dataList =data;

                        });
                    }
                };
                //删除已选数据
                scope.removeData=function(index){
                    scope.addedList.splice(index,1);
                    scope.outData.splice(index,1);

                };
                //选择
                scope.chooseData=function(val){
                    var active=true;
                    //console.log("list"+scope.addedList);
                    //console.log("outData"+scope.outData);
                    angular.forEach(scope.outData ,function (newData) {
                        console.log("new"+newData.id);
                        //console.log("val"+val.id);
                        if(newData.id==val.id){
                            active=false;
                        }
                    });
                    if(active){
                        scope.addedList.push(val.setValue);
                        scope.outData.push(val);
                    }
                    scope.close();
                };

                //搜索
                scope.search = function(str, e) {
                    e.preventDefault();
                    e.stopPropagation();
                    if("pre"==str){
                        scope.param.pageNumber--;
                    }
                    if("next"==str){
                        scope.param.pageNumber++;
                    }
                    if("search"==str){
                        scope.param.pageNumber=1;
                    }
                    scope.dataList=[];
                    scope.options.methodName(scope.param).success(function (data) {
                        scope.totalCount = data.totalCount;
                        var data = eval(data.result);

                        if(angular.isArray(scope.options.parameter)){
                            angular.forEach(data,function(item){
                                item.setValue=item[scope.options.parameter[0]];
                                item.setKey=item[scope.options.parameter[1]];
                            });
                        }else{
                            angular.forEach(data,function(item){
                                item.setValue=item.name;
                                item.setKey=item.id;
                            });
                        }
                        scope.dataList =data;

                        console.log(scope.dataList);
                    });
                };


                //点击非本身地方关闭搜索框
                scope.addId="addId"+new Date().getTime();
                var getPar = function(tar) {
                    if(tar.id == scope.addId){
                        scope.count++;
                    }
                    if(tar.parentElement){
                        getPar(tar.parentElement);
                    }
                };

                angular.element(document).bind("click", function(e) {
                    scope.count = 0;
                    getPar(e.target);
                    if(scope.count == 0) {
                        scope.$apply(function(){
                            scope.close();
                        });
                    }
                });
            }
        };
    }]);