详解angularjs popup-table 弹出框表格指令

本文主要介绍了angularjs popup-table 弹出框表格指令,分享给大家,具体如下:

//表格处理
app.directive('popupTable', ['$http', '$rootScope', '$cookies', '$location', function ($http, $rootScope, $cookies, $location) {
  return {
    restrict: 'E',
    templateUrl: 'popuptable_templete.html',
    scope: {
      url: '=',
      routepath: '=?',
      routetype: '=?',
      onCallback: '&',
      mulitselect: '=',
      selectnode: '=?'
    },
    link: function ($scope, element, attrs) {
      $scope.myValue = false;
      $scope.checkallvalue = false; 

      var primaryKeyFieldName = "";
      var codeFieldName = "";
      $scope.showAddButton = true;
      $scope.showRefreshButton = true;
      var checkList = new Array(); 

      //监视url变化。从而重新读取数据
      $scope.$watch('url', function (newVal, oldVal) {
        if (oldVal != newVal) {
          //设定全选为false
          $scope.checkallvalue = false;
          querySearch(0, "");
        }
      }); 

      //选择所有
      $scope.checkall = function () {
        if (angular.isDefined($scope.popupdata) && $scope.popupdata.length > 0) {
          angular.forEach($scope.popupdata, function (item, index) {
            $scope.changeChoose($scope.checkallvalue, item);
          });
        }
      } 

      //选择改变时事件
      $scope.changeChoose = function (check, data) {
        var index = findSelected(data);
        if (check) {
          if (index <= -1)
            checkList.push(data);
        } else {
          if (index > -1)
            checkList.splice(index, 1);
        }
      } 

      //通过data数据在数组中查询,并返回所在的索引,没有找到则返回-1
      function findSelected(data) {
        var indexvalue = -1;
        if (angular.isUndefined(checkList) || checkList.length <= 0 || primaryKeyFieldName == "")
          return indexvalue;
        angular.forEach(checkList, function (item, index) {
          if (indexvalue == -1) {
            if (item[primaryKeyFieldName] == data[primaryKeyFieldName]) {
              indexvalue = index;
            }
          }
        });
        return indexvalue;
      } 

      //判断是否选中
      $scope.isChecked = function (rowdata) {
        return findSelected(rowdata) > -1;
      } 

      //1、读取网络数据,分页,搜索
      function querySearch(index, searchText) {
        if ($scope.popupdata != null && $scope.popupdata.length > 0)
          $scope.popupdata = null;
        //初始化
        var params = { "SearchKey": searchText, "UserId": $rootScope.loginUserId };
        params = angular.extend(params, { "IsGetColumns": index > 0 ? false : true }); 

        //刷新或者查询的时候需要清空已选择项
        if (index > 0)
          checkList.splice(0, checkList.length); 

        $scope.loading = true;
        $(".no-data-div").hide(); 

        serverRequestwithformdata($http, $rootScope.SystemUrl + $scope.url + "/PopupList", $.param(params), function (data) {
          console.info(data);
          $scope.loading = false;
          if (data.status == "ok") {
            if (index <= 0) {
              $scope.title = data.windowTitle;
              $scope.columnlist = data.colums;
              $scope.showAddButton = data.ShowAdd;
              $scope.showRefreshButton = data.ShowRefresh;
              primaryKeyFieldName = data.primayKey;
              codeFieldName = data.codeField;
              //url 变化导致执行=>处理已勾选项=>赋值勾选项。
              if (checkList.length > 0)
                checkList.splice(0, checkList.length);
              if (angular.isDefined($scope.selectnode) && $scope.selectnode != null && $scope.selectnode.length > 0)
                checkList = $scope.selectnode;
            }
            $scope.data = data.records;
            var sum = data.records.length;
            $(".sum").text("共" + sum + "条数据");
            $scope.pages = Math.ceil(sum / $rootScope.PageSize);
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            $scope.sumPage = Math.ceil($scope.data.length / $rootScope.PageSize);
            for (var i = 0; i < $scope.newPages; i++) {
              $scope.pageList.push(i + 1);
            }
            if (sum == 0) {
              $(".no-data-div").show();
              $(".no-data-span").val("无数据");
            }
            $scope.setData();
            $(".pages").text("当前第" + $scope.selPage + "页" + "/" + "共" + $scope.sumPage + "页");
          } else {
            $(".no-data-div").show();
            $(".no-data-span").val(data.message);
          }
        }, function (data) {
          $scope.loading = false;
          $(".no-data-div").show();
          $(".no-data-span").val("访问错误");
        });
      } 

      //设置表格数据源(分页)
      $scope.setData = function () {
        //通过当前页数筛选出表格当前显示数据
        $scope.popupdata = $scope.data.slice(($rootScope.PageSize * ($scope.selPage - 1)), ($scope.selPage * $rootScope.PageSize));
        if (angular.isDefined($scope.popupdata) && $scope.popupdata.length > 0) {
          var indexvalue = 0;
          angular.forEach($scope.popupdata, function (item, index) {
            if (findSelected(item) > -1)
              indexvalue++;
          });
          $scope.checkallvalue = (indexvalue == $scope.popupdata.length);
        }
      } 

      //打印当前选中页索引
      $scope.selectPage = function (page) {
        if (page < 1 || page > $scope.pages)
          return;
        if (page > 2) {
          var newpageList = [];
          for (var i = (page - 3) ; i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)) ; i++) {
            newpageList.push(i + 1);
          }
          $scope.pageList = newpageList;
        }
        $scope.selPage = page;
        $scope.setData();
        $scope.isActivePage(page);
        $(".pages").text("当前第" + page + "页" + "/" + "共" + $scope.sumPage + "页");
      }; 

      //跳转
      $scope.jump = function () {
        var page = parseInt($(".jump-bar").val());
        if ($(".jump-bar").val() == 0) {
          swal("请输入跳转页数", "", "error");
          return;
        }
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) return;
        //最多显示分页数5
        if (page > 2) {
          //因为只显示5个页数,大于2页开始分页转换
          var newpageList = [];
          for (var i = (page - 3) ; i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)) ; i++) {
            newpageList.push(i + 1);
          }
          $scope.pageList = newpageList;
        }
        $scope.selPage = page;
        $scope.setData();
        $scope.isActivePage(page);
        $(".pages").text("当前第" + page + "页" + "/" + "共" + $scope.sumPage + "页");
      }; 

      //设置当前选中页样式
      $scope.isActivePage = function (page) {
        return $scope.selPage == page;
      }; 

      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.selPage - 1);
      } 

      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.selPage + 1);
      }; 

      //关闭弹出框
      function closewindow() {
        $(".pop-up").stop(true, false).fadeOut();
      } 

      //取消弹出框
      $scope.PopupCancel = function () {
        closewindow();
      } 

      //确定
      $scope.PopupOK = function () {
        if (primaryKeyFieldName == "" || codeFieldName == "") {
          swal("当前未配置返回信息", "", "error");
          return;
        }
        //获取选中的数据,并关闭弹出,然后返回填值
        if (angular.isUndefined(checkList) || checkList.length <= 0) {
          swal("请勾选要操作的数据", "", "error");
          return;
        }
        var allowMulti = false;
        if (angular.isDefined($scope.mulitselect)) {
          allowMulti = $scope.mulitselect;
        }
        var primaryKey = "";
        var codeKey = "";
        //只存在1个的情况
        if (checkList.length == 1) {
          primaryKey = checkList[0][primaryKeyFieldName];
          codeKey = checkList[0][codeFieldName];
        } else {
          //存在多个
          if (allowMulti == false) {
            primaryKey = checkList[0][primaryKeyFieldName];
            codeKey = checkList[0][codeFieldName];
          } else {
            angular.forEach(checkList, function (item, index) {
              primaryKey += item[primaryKeyFieldName] + ",";
              codeKey += item[codeFieldName] + ",";
            });
          }
        }
        if (primaryKey.endsWith(","))
          primaryKey = primaryKey.substring(0, primaryKey.length - 1);
        if (codeKey.endsWith(","))
          codeKey = codeKey.substring(0, codeKey.length - 1);
        closewindow();
        if ($scope.onCallback) {
          $scope.onCallback({ data: checkList, primaryKey: primaryKey, codeKey: codeKey, url: $scope.url });
        }
      } 

      //刷新
      $scope.PopupRefresh = function () {
        $("#searchText").val("");
        querySearch(1, "");
      } 

      //新增
      $scope.PopupAdd = function () {
        $location.path('/' + $scope.routepath).search({ id: '-1', type: $scope.routetype });
      } 

      //搜索
      $scope.PopupSearch = function () {
        querySearch(1, $("#searchText").val());
      }
    }
  };
}]);

模板的url 页面

<script type="text/javascript">
  $(function () {
    //全选
    $(".Pagingjump-check").click(function () {
      if (this.checked) {
        $(this).parents().parents().parents(".tDefault").find(".table-checked").each(function () {
          this.checked = true;
        })
      }
      if (!this.checked) {
        $(this).parents().parents().parents(".tDefault").find(".table-checked").each(function () {
          this.checked = false;
        })
      }
    });
  })
</script>
<div class="pop-up-content">
  <div class="pop-up-table-title">{{title}}</div>
  <div class="pop-up-table-search">
    <input id="searchText" type="text" class="input-search" size="30" placeholder="请输入查询条件">
    <ul class="middleFree block-button-panel-ul pop-up-table-search-btn">
      <li ng-click="PopupSearch()">
        <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="查询" class="cBlue" style="padding: 5px 10px !important">
          <span>查询</span>
        </a>
      </li>
    </ul>
  </div>
  <div>
    <div class="pop-up-table-panel" style="border-top: 1px solid #DFDFDF">
      <table class="tDefault pop-up-table search-block-process-table">
        <tr nf-if="columnlist.length>0" style="border-top:0px">
          <td style="width:30px !important">
            <input id="titleCheck-all" class="Pagingjump-check" name="checkRow" type="checkbox" ng-model="checkallvalue" ng-change="checkall()" >
          </td>
          <td class="table-width1" style="width:50px !important">序号</td>
          <td ng-repeat="column in columnlist |orderBy:column.PopupColumnOrder" width="{{column.PopupDefaultWidth}}" ng-click="col='{{column.ColumnName}}';" ng-show="{{column.IsPopoupColumn}}">{{column.DisplayColumnName}}</td>
        </tr>
        <tr ng-repeat="data in popupdata">
          <!--ng-checked-->
          <td><input class="table-checked" name="checkRow" type="checkbox" ng-model="data.selected" ng-change="changeChoose(data.selected,data)" ng-checked="isChecked(data)"></td>
          <td>{{ $index + 1 }}</td>
          <td class="table-textalign-left" ng-repeat="column in columnlist|orderBy:column.PopupColumnOrder" ng-show="{{column.IsPopoupColumn}}" datacolumn="{{column.ColumnName}}">{{data[column.ColumnName]}}</td>
        </tr>
      </table>
      <div class="no-data-div">
        <span class="no-data-span">无数据</span>
      </div>
      <div class="loading-page" style="height:300px !important;" ng-if="loading">
        <div class='uil-default-css' style='transform: scale(0.2); width: 100% !important; height: 300px !important;'>
          <div class="loadingpoint" style=' -webkit-transform:rotate(0deg) translate(0,-60px) ; transform:rotate(0deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(30deg) translate(0,-60px); transform: rotate(30deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(60deg) translate(0,-60px); transform: rotate(60deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(90deg) translate(0,-60px); transform: rotate(90deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(120deg) translate(0,-60px); transform: rotate(120deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(150deg) translate(0,-60px); transform: rotate(150deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(180deg) translate(0,-60px); transform: rotate(180deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(210deg) translate(0,-60px); transform: rotate(210deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(240deg) translate(0,-60px); transform: rotate(240deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(270deg) translate(0,-60px); transform: rotate(270deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(300deg) translate(0,-60px); transform: rotate(300deg) translate(0,-60px);'></div>
          <div class="loadingpoint" style=' -webkit-transform: rotate(330deg) translate(0,-60px); transform: rotate(330deg) translate(0,-60px);'></div>
        </div>
      </div>
    </div>
    <div class="block-button-panel2">
      <div class="table-pagingjump-div" style="bottom: 0px;width: 100%;height: 35px;padding: 4px;">
        <div class="Pagingjump-function-panel" style="float:right;width:auto">
          <nav>
            <ul class="pagination Pagingjump-function-ul Pagingjump-table-ul">
              <li>
                <input type="text" style="padding: 2px 2px 3px 2px!important;width:40px" class="jump-bar" size="5" value="" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" />
              </li>
              <li>
                <a ng-click="jump()" class="table-pagination-a">
                  <div class="fs1 iconb" data-icon=""></div>
                </a>
              </li>
            </ul>
          </nav>
        </div>
        <div class="Pagingjump-check-panel-table" style="float:right">
          <span class="sum">共0条数据</span>
          <span class="pages">当前第1页/共1页</span>
        </div>
        <div class="Pagingjump-function-panel" style="float:left">
          <nav>
            <ul class="pagination Pagingjump-function-ul Pagingjump-table-ul">
              <li>
                <a ng-click="Previous()" class="table-pagination-a">
                  <div class="fs1 iconb" data-icon=""></div>
                </a>
              </li>
              <li ng-repeat="page in pageList" ng-class="{active: isActivePage(page)}" class="table-pagination-a">
                <a ng-click="selectPage(page)">{{ page }}</a>
              </li>
              <li>
                <a ng-click="Next()" class="Pagingjump-function-nextpage table-pagination-a">
                  <div class="fs1 iconb" data-icon=""></div>
                </a>
              </li>
            </ul>
          </nav>
        </div>
      </div>
    </div>
    <div class="pop-up-button-panel">
      <div class="block-button-panel-left">
        <ul class="middleFree block-button-panel-ul">
          <li ng-click="PopupAdd()" ng-if="showAddButton">
            <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="新增" class="cBlue" style="padding: 5px 10px !important">
              <span>新增</span>
            </a>
          </li>
        </ul>
      </div>
      <div class="block-button-panel-right">
        <ul class="middleFree block-button-panel-ul">
          <li ng-click="PopupRefresh()" ng-if="showRefreshButton">
            <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="刷新" class="cBlue">
              <span>刷新</span>
            </a>
          </li>
          <li ng-click="PopupOK()">
            <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="确定" class="cBlue">
              <span>确定</span>
            </a>
          </li>
          <li ng-click="PopupCancel()">
            <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="取消" class="cRed">
              <span>取消</span>
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

调用:

//打开弹出框
    $scope.openpop = function (type) {
      $(".pop-up").stop(true, false).fadeIn();
      //根据绑定值进行读取操作
      if (type == "SearchHistory") {
        //请求数据即可.读取List接口 

      } else {
        //读取PopupList接口
        $scope.routetype = "ReturnPopup";
        if (type == "process")
          $scope.routepath = "ProcessDetail";
        else if (type == "productmodel")
          $scope.routepath = "ProductModelDetail";
        var temp = $cookies.get(type + "checkcache");
        if (angular.isDefined(temp) && temp != null)
          $scope.selectnode = jQuery.parseJSON(temp);
        $scope.urlpart = type;
      }
    } 

    $scope.popupcallback = function (data, primaryKey, codeKey, url) {
      //根据url存储data
      if (data != null & data.length > 0)
        $cookies.put(url + "checkcache", JSON.stringify(data));
      if (url == "process") {
        $scope.selectprocessNametip = codeKey;
        $scope.selectprocessIdtip = primaryKey;
      }
      else if (url == "productmodel") {
        $scope.selectproductNametip = codeKey;
        $scope.selectproductIdtip = primaryKey;
      }
    }
<!--表格弹框-->
  <div class="pop-up">
    <popup-table url="urlpart" routepath="routepath" routetype="routetype" on-callback="popupcallback(data,primaryKey, codeKey,url)" mulitselect="true" selectnode="selectnode"></popup-table>
  </div> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 百度Popup.js弹出框进化版 拖拽小框架发布 兼容IE6/7/8,Firefox,Chrome

    我们之前发布过这样的代码,其实问题不大,但这里的版本主要是增加一些功能,回调执行服务器端的方法,对于asp.net开发或ajax开发都是非常有价值的改进.先看下效果图: 原有百度的Popup.js在有 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

  • Popup弹出框添加数据实现方法

    本文实例为大家分享了Popup弹出框添加数据的具体代码,供大家参考,具体内容如下 逻辑 窗口P1中显示一组数据,并提供一个添加按钮 点击按钮,弹出新的浏览器窗口P2,在其中添加一条数据并提交后,窗口P2自动关闭 新添加数据动态添加到窗口P1中并被选中 所需知识:JS BOM 窗口对象:JS自执行函数 实现 下面在Django中简单实现下,因为比较简单,路由和视图就写在一起了. 1.路由和视图部分 from django.conf.urls import url from django.short

  • Android实现底部半透明弹出框PopUpWindow效果

    Android底部半透明弹出框PopUpWindow,供大家参考,具体内容如下 layout布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" androi

  • Android中自定义PopupWindow实现弹出框并带有动画效果

    使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { public Lost lost; public void onLost(Lost lost){ this.lost = lost; } private View conentView; public View getConentView() { return conentView; } public L

  • 详解angularjs popup-table 弹出框表格指令

    本文主要介绍了angularjs popup-table 弹出框表格指令,分享给大家,具体如下: //表格处理 app.directive('popupTable', ['$http', '$rootScope', '$cookies', '$location', function ($http, $rootScope, $cookies, $location) { return { restrict: 'E', templateUrl: 'popuptable_templete.html',

  • 详解用vue编写弹出框组件

    前言 最近研究了用vue编写弹出框的组件,发现其实这里面的门道还是有很多的.这篇文完全是用来记录总结下最近的学习成果,同时也希望能够帮得上正在学习纠结的你~ps:本文假设你已经了解vue2.0相关框架,因此适合有一定vue2.0基础的同学阅读. 设计组件的思考 其实单纯的编写一个弹出框组件并不难,写一个模板,然后用v-if或者v-show指令还控制组件的出现与消失.真正困扰我的是,这个组件的调用方式,这个问题纠结了我好久. 调研了下资料,有些人建议,直接把组件标签插进模板中,然后通过直接控制组件

  • JS中artdialog弹出框控件之提交表单思路详解

    artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口. 前言: 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应,因此你不必去考虑消息内容尺寸使用它.它的消息容器甚至能够根据宽度让文本居中或居左对齐--这一切全是XHTML+CSS原生实现. 完善的接口 它的接口完善,可以轻易与外部程序配合使用.如异步写入消息.控制位置.尺寸.显示与隐藏.关闭等. 细致的体验 如果不是在输入状态,它支持Esc快捷键关

  • Bootstrap弹出框(modal)垂直居中的问题及解决方案详解

    使用过bootstrap modal(模态框)组件的人都有一种困惑, 好好的一个弹出框怎么就无法垂直居中了呢?刚巧在做一个eit项目,由于此项目里面一些框架要遵循nttdata的一些规则,故用到了Bootstrap这个东东,第一次碰到这个东东,有很大抵触,觉得不好,但用起来我觉得和别的弹出框真没什么两样.废话少说,切入正题,Bootstrap弹出框垂直居中的问题,因为我拿到的弹出框样式并非垂直居中,而是top 10%,但页面长了,就显得特别恶心. 解决方案如下所示: 1.在css里,找到 .mo

  • js实现弹出框的拖拽效果实例代码详解

    具体代码如下所示: //HTML部分 <div class="wrap"></div> <div class="popUpBox"> <div class="layer-head"><div class="layer-head-text">弹出框</div><div class="layer-close"></div&

  • bootstrap响应式导航条模板使用详解(含下拉菜单,弹出框)

    本文实例为大家分享了bootstrap响应式导航条模板展示的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name=&quo

  • angularjs创建弹出框实现拖动效果

    本文实例介绍了angularjs创建弹出框实现拖动效果的相关代码,项目中需要将angular-ui-bootstrap中用到的弹出框,使之可拖动,分享给大家供大家参考,具体内容如下 运行效果图: 由于源文件中没有实现,需要自己实现指令,以下即为该指令,亲测可以实现. .directive('draggable', ['$document', function($document) { return function(scope, element, attr) { var startX = 0,

  • AngularJS $modal弹出框实例代码

    下面给大家说下$modal拥有一个方法:open,该方法的属性介绍: templateUrl:模态窗口的地址 template:用于显示html标签 scope:一个作用域为模态的内容使用(事实上,$modal会创建一个当前作用域的子作用域)默认为$rootScope controller:为$modal指定的控制器,初始化$scope,该控制器可用$modalInstance注入 resolve:定义一个成员并将他传递给$modal指定的控制器,相当于routes的一个reslove属性,如果

随机推荐