aj商品增删改查

来源:互联网 发布:福彩红五图库永久域名 编辑:程序博客网 时间:2024/06/02 23:47
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Title</title>  
  6.     <style>  
  7.         table {  
  8.             border-collapse: collapse;  
  9.         }  
  10.   
  11.         td, th {  
  12.             width: 200px;  
  13.             border: 1px solid gainsboro;  
  14.             text-align: center;  
  15.             padding: 20px;  
  16.         }  
  17.   
  18.         button {  
  19.             width: 100px;  
  20.             height: 40px;  
  21.             background: orange;  
  22.             color: white;  
  23.             border: 0px;  
  24.             border-radius: 5px;  
  25.         }  
  26.   
  27.         .search {  
  28.             background: ghostwhite;  
  29.             border: 1px solid gainsboro;  
  30.             border-radius: 5px;  
  31.             width: 1450px;  
  32.             height: 50px;  
  33.             line-height: 50px;  
  34.             margin-bottom: 10px;  
  35.         }  
  36.   
  37.         .search input {  
  38.             width: 200px;  
  39.             height: 30px;  
  40.             color: #999;  
  41.             border-radius: 5px;  
  42.             margin-left: 20px;  
  43.             border: 1px solid gainsboro;  
  44.         }  
  45.   
  46.         .search button {  
  47.             float: right;  
  48.             margin-top: 5px;  
  49.             margin-right: 10px;  
  50.             background: red;  
  51.         }  
  52.   
  53.         .active {  
  54.             color: red;  
  55.         }  
  56.     </style>  
  57.     <script src="angular.js"></script>  
  58.     <script>  
  59.         var falg=false;  
  60.         var data =  
  61.                 [{"id": 1234, "name": 'ipad', "price": 3400, "count": 10},  
  62.                     {"id": 1244, "name": 'aphone', "price": 6400, "count": 100},  
  63.                     {"id": 1334, "name": 'mypad', "price": 4400, "count": 20},  
  64.                     {"id": 8234, "name": 'zpad', "price": 8400, "count": 130},  
  65.                 ];  
  66.         var myapp = angular.module("myapp", []);  
  67.         myapp.controller("myCtrl", function ($scope) {  
  68.             $scope.ycxiugai=false;  
  69.             $scope.yctianjia=false;  
  70.   
  71.             /**排污*/  
  72.             $scope.px1=function(){  
  73.                 alert($scope.paixu);  
  74.   
  75.                 if($scope.paixu==2){  
  76.                     falg=true;  
  77.                 }else{  
  78.                     falg=false;  
  79.                 }  
  80.   
  81.                 data.sort(function(a,b){  
  82.                     if(falg){  
  83.                         return a.count< b.count?1:-1;  
  84.                     }else{  
  85.                         return a.count>b.count?1:-1;  
  86.                     }  
  87.                 });  
  88.             }  
  89.   
  90.             $scope.tianjia=function(){  
  91.                 $scope.yctianjia=true;  
  92.             }  
  93.             $scope.xiugai=function(index){  
  94.                 var jihe=$scope.data[index];  
  95.                 $scope.x_id=jihe.id,  
  96.                 $scope.x_name="",  
  97.                 $scope.x_price="",  
  98.                 $scope.x_count="",  
  99.                 $scope.ycxiugai=true;  
  100.                 $scope.index=index;  
  101.             }  
  102.   
  103.             $scope.data = data;  
  104.             //设置页面默认显示  
  105.             data.sort(function(a,b){  
  106.                 if(falg){  
  107.                     return a.count< b.count?1:-1;  
  108.                 }else{  
  109.                     return a.count>b.count?1:-1;  
  110.                 }  
  111.             });  
  112.             $scope.show = true;  
  113.             //设置checkbox默认不选中  
  114.             $scope.chk = false;  
  115.             $scope.xuanz = false;  
  116.             $scope.check = function (val) {  
  117.                 /*判断是否选中,然后控制下方的是否选中*/  
  118.                 if (val) {  
  119.                     $scope.xuanz = true;  
  120.                 } else {  
  121.                     $scope.xuanz = false;  
  122.                 }  
  123.             }  
  124.             $scope.search;  
  125.             /*删除按钮*/  
  126.             $scope.remove = function (index) {  
  127.                 /*判断是否确认删除*/  
  128.                 var b = confirm("确认删除");  
  129.                 if (b) {  
  130.                     $scope.data.splice(index, 1);  
  131.                 }  
  132.             }  
  133.             $scope.del = function () {  
  134.                 /*判断是否确认删除*/  
  135.                 if ($scope.xuanz || $scope.checkD) {  
  136.                     var b = confirm("确认删除");  
  137.                     if (b) {  
  138.                         //删除所有商品信息  
  139.                         if ($scope.chk) {  
  140.                             $scope.data.splice(0, $scope.data.length);  
  141.                             //让商品信息管理页面为空白界面  
  142.                             $scope.show = false;  
  143.                         } else {  
  144.                             for (var i = $scope.xuan1.length - 1; i >= 0; i--) {  
  145.                                 console.log($scope.xuan1[i]);  
  146.                                 for (var j = 0; j < $scope.data.length; j++) {  
  147.                                     console.log($scope.data[j].id == $scope.xuan1[i]);  
  148.                                     if ($scope.data[j].id == $scope.xuan1[i]) {  
  149.                                         $scope.data.splice(j, 1);  
  150.                                     }  
  151.                                 }  
  152.                             }  
  153.                         }  
  154.                     }  
  155.                 } else {  
  156.                     alert("先进行选中要删除的商品信息");  
  157.                 }  
  158.             }  
  159.             /**删除全部*/  
  160.              $scope.shanchu=function(){  
  161.                  var b = confirm("确认删除");  
  162.                  if(b){  
  163.                      $scope.data.splice(0, $scope.data.length);  
  164.                      //让商品信息管理页面为空白界面  
  165.                      $scope.show = false;  
  166.                  }  
  167.   
  168.              }  
  169.   
  170.             /**添加*/  
  171.             $scope.tianjiaan=function(){  
  172.   
  173.                 if ($scope.t_id == undefined || $scope.t_id == "") {  
  174.                     alert("id不能为空!");  
  175.                     return;  
  176.                 }  
  177.   
  178.                 if ($scope.t_name == undefined || $scope.t_name == "") {  
  179.                     alert("name不能为空!");  
  180.                     return;  
  181.                 }  
  182.   
  183.                 if ($scope.t_price == undefined || $scope.t_price == "") {  
  184.                     alert("price不能为空!");  
  185.                     return;  
  186.                 }  
  187.   
  188.                 if ($scope.t_count == undefined || $scope.t_count == "") {  
  189.                     alert("count不能为空!");  
  190.                     return;  
  191.                 }  
  192.   
  193.                 if (!/^\d+$/.test($scope.t_id)) {  
  194.                     alert("id必须是整数!");  
  195.                     return;  
  196.                 }  
  197.   
  198.                 if (!/^\d+$/.test($scope.t_price)) {  
  199.                     alert("价格必须是整数!");  
  200.                     return;  
  201.                 }  
  202.                 if (!/^\d+$/.test($scope.t_count)) {  
  203.                     alert("数量必须是整数!");  
  204.                     return;  
  205.                 }  
  206.   
  207.                 $scope.data.push({  
  208.                     id:$scope.t_id,  
  209.                     name:$scope.t_name,  
  210.                     price:$scope.t_price,  
  211.                     count:$scope.t_count  
  212.                 });  
  213.                 data.sort(function(a,b){  
  214.                     if(falg){  
  215.                         return a.count< b.count?1:-1;  
  216.                     }else{  
  217.                         return a.count>b.count?1:-1;  
  218.                     }  
  219.                 });  
  220.                         $scope.t_id="",  
  221.                         $scope.t_name="",  
  222.                         $scope.t_price="",  
  223.                         $scope.t_count=""  
  224.                 $scope.yctianjia=false;  
  225.   
  226.             }  
  227.   
  228.   
  229.             /**排序*/  
  230.             $scope.px=function(){  
  231.                 if($scope.paixu==2){  
  232.                     falg=true;  
  233.                 }else{  
  234.                     falg=false;  
  235.                 }  
  236.   
  237.                 data.sort(function(a,b){  
  238.                    if(falg){  
  239.                        return a.count> b.count?1:-1;  
  240.                    }else{  
  241.                        return a.count<b.count?1:-1;  
  242.                    }  
  243.                 });  
  244.             }  
  245.   
  246.             $scope.xiugaian=function(){  
  247.                 if ($scope.x_id == undefined || $scope.x_id == "") {  
  248.                     alert("id不能为空!");  
  249.                     return;  
  250.                 }  
  251.   
  252.                 if ($scope.x_name == undefined || $scope.x_name == "") {  
  253.                     alert("name不能为空!");  
  254.                     return;  
  255.                 }  
  256.   
  257.                 if ($scope.x_price == undefined || $scope.x_price == "") {  
  258.                     alert("price不能为空!");  
  259.                     return;  
  260.                 }  
  261.   
  262.                 if ($scope.x_count == undefined || $scope.x_count == "") {  
  263.                     alert("count不能为空!");  
  264.                     return;  
  265.                 }  
  266.   
  267.                 if (!/^\d+$/.test($scope.x_id)) {  
  268.                     alert("id必须是整数!");  
  269.                     return;  
  270.                 }  
  271.   
  272.                 if (!/^\d+$/.test($scope.x_price)) {  
  273.                     alert("价格必须是整数!");  
  274.                     return;  
  275.                 }  
  276.                 if (!/^\d+$/.test($scope.x_count)) {  
  277.                     alert("数量必须是整数!");  
  278.                     return;  
  279.                 }  
  280.                 $scope.ycxiugai=false;  
  281.                 $scope.data[$scope.index].name=$scope.x_name;  
  282.                 $scope.data[$scope.index].price=$scope.x_price;  
  283.                 $scope.data[$scope.index].count=$scope.x_count;  
  284.                 data.sort(function(a,b){  
  285.                     if(falg){  
  286.                         return a.count< b.count?1:-1;  
  287.                     }else{  
  288.                         return a.count>b.count?1:-1;  
  289.                     }  
  290.                 });  
  291.             }  
  292.   
  293.   
  294.   
  295.             $scope.checkD = false;  
  296.             $scope.xuan1 = [];  
  297.             $scope.xuan = function (xuanl, id) {  
  298.                 console.log(id);  
  299.                 $scope.checkD = xuanl;  
  300.                 if (xuanl) {  
  301.                     $scope.xuan1.push(id);  
  302.                 }  
  303.             }  
  304.             //判断的功能,默认是名字排序  
  305.   
  306.         })  
  307.     </script>  
  308. </head>  
  309. <body ng-app="myapp" ng-controller="myCtrl">  
  310. <div class="search">  
  311.     <input type="text" placeholder="输入关键字…" ng-model="search">  
  312.     <select ng-model="paixu" ng-change="px1()">  
  313.         <option value="">库存正序</option>  
  314.         <option value="2">库存倒序</option>  
  315.     </select>  
  316.     <button ng-click="del()">批量删除</button>  
  317.     <button ng-click="tianjia()">添加</button>  
  318.     <button ng-click="shanchu()">删除全部</button>  
  319. </div>  
  320. <!--用来遍历对象并渲染到页面中-->  
  321. <table ng-show="show">  
  322.     <thead>  
  323.     <th><input type="checkbox" ng-model="chk" ng-click="check(chk)"></th>  
  324.     <th ng-click="sort1('id')" ng-class="{active:active=='id'}">商品编号</th>  
  325.     <th ng-click="sort1('name')" ng-class="{active:active=='name'}">商品名称</th>  
  326.     <th ng-click="sort1('price')" ng-class="{active:active=='price'}">商品价格</th>  
  327.     <th ng-click="sort1('count')" ng-class="{active:active=='count'}">商品库存</th>  
  328.     <th>数据删除</th>  
  329.     <th>数据修改</th>  
  330.     </thead>  
  331.     <tbody>  
  332.     <!--实现模糊查询-->  
  333.     <tr ng-repeat="item in data| filter:search">  
  334.         <td><input type="checkbox" ng-model="xuanz" ng-click="xuan(xuanz,item.id)"></td>  
  335.         <td>{{item.id}}</td>  
  336.         <td> {{item.name}}</td>  
  337.         <!--商品价格前面加“¥”-->  
  338.         <td>{{item.price | currency:"¥"}}</td>  
  339.         <td>{{item.count}}</td>  
  340.         <td>  
  341.             <button ng-click="remove($index)">删除</button>  
  342.         </td>  
  343.         <td>  
  344.             <button ng-click="xiugai($index)">修改</button>  
  345.         </td>  
  346.     </tr>  
  347.     </tbody>  
  348. </table>  
  349. <div ng-show="ycxiugai">  
  350.     <p>修改商品</p>  
  351.     <p>商品编号:<input type="text" ng-model="x_id"/></p>  
  352.   
  353.     <p>商品名称:<input type="text" ng-model="x_name"/></p>  
  354.   
  355.     <p>商品价格:<input type="text" ng-model="x_price"/></p>  
  356.   
  357.     <p>商品库存:<input type="text" ng-model="x_count"/></p>  
  358.   
  359.     <p>  
  360.         <button ng-click="xiugaian($index)">修改</button>  
  361.     </p>  
  362. </div>  
  363.   
  364. <div ng-show="yctianjia">  
  365.     <p>添加商品</p>  
  366.     <p>商品编号:<input type="text" ng-model="t_id"/></p>  
  367.   
  368.     <p>商品名称:<input type="text" ng-model="t_name"/></p>  
  369.   
  370.     <p>商品价格:<input type="text" ng-model="t_price"/></p>  
  371.   
  372.     <p>商品库存:<input type="text" ng-model="t_count"/></p>  
  373.   
  374.     <p>  
  375.         <button ng-click="tianjiaan($index)">添加</button>  
  376.     </p>  
  377. </div>  
  378. </body>  
  379. </html>  
原创粉丝点击