Angularjs+路由表格的增删改查综合

login.html登录页面
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

        <script>

        </script>
    </head>
    <body>
        <center>
            <h3>注册页面</h3>
            <table border="1 solid blue" cellpadding="10" cellspacing="0">
                <tbody>
                    <tr>
                        <th>用户名:</th>
                        <td><input ng-model="name" type="text" placeholder="请输入用户名"/> </td>
                    </tr>
                    <tr>
                        <th>密 码:</th>
                        <td><input type="text" placeholder="请输入密码"/> </td>
                    </tr>
                    <tr>
                        <th>年 龄:</th>
                        <td><input type="text" placeholder="请输入年龄"/> </td>
                    </tr>
                    <tr>
                        <th>性 别:</th>
                        <td>男<input type="radio" value="name" id="name" /> 女<input type="radio" value="男" id="name2" /></td>
                    </tr>
                    <tr align="center">
                        <td colspan="2"><input ng-click="login()" type="button" value="注册"/></td>
                    </tr>
            </table>
        </center>
    </body>
</html>

main.html   主页面表格
    <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <center>
            <h3>主页面</h3>
            <div>
                姓名:<input type="text" placeholder="用户名" size="8" ng-model="gen"/>&nbsp;&nbsp;
                年龄:<select ng-model="size">
                        <option>--请选择--</option>
                        <option>11-20</option>
                        <option>21-30</option>
                        <option>31-40</option>
                        <option>41-50</option>
                        <option>51-60</option>
                    </select>&nbsp;&nbsp;
                <button ng-click="deleteSel()">批量删除</button><br/><br/>
            </div>
            <table border="1 solid blue" cellpadding="10" cellspacing="0">
                <thead>
                    <tr>
                        <th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
                        <th>id</th>
                        <th>姓名</th>
                        <th>密码</th>
                        <th>年龄</th>
                        <th>性别</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="user in users | filter:{name:gen}" ng-if="ageSize(user.age,size)">
                        <td><input type="checkbox" ng-click="checkSelect($index)" ng-model="user.state"/></td>
                        <td>{{user.id}}</td>
                        <td>{{user.name}}</td>
                        <td>{{user.pwd}}</td>
                        <td>{{user.age}}</td>
                        <td>{{user.sex}}</td>
                        <td><button>删除</button></td>
                    </tr>
                </tbody>
            </table>
        </center>
    </body>
</html>

    text3.html核心页面
    <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .leftSide{
                width: 20%;
                display: inline-block;
                background-color: red;
                height: 600px;
                float: left
            }
            .rightSide{
                width: 80%;
                display: inline-block;
                background-color:#b2d235;
                height: 600px;
                float: right
            }
            li {
                list-style-type: none;
                font-size: 30px;
                padding: 37px 0px 37px 100px;
                border: 1px solid blue;
                margin-left: -40px;
            }
            li a {
                text-decoration: none;
            }
        </style>
        <!-- 1.导入库文件 -->
        <script src="angular.js"></script>
<script src="angular-route.js"></script>

        <script>
            /*2.注入路由服务*/
            var app = angular.module("myApp",['ngRoute']);
            //3.配置路由规则
            app.config(["$routeProvider",function($routeProvider){
                //使用路由服务对象,配置路由规则
                $routeProvider
                    .when("/login",{
                        controller:"loginCtrl",
                        templateUrl:"login.html"
                    })
                    .when("/main",{
                        controller:"mainCtrl",
                        templateUrl:"main.html"
                    })
                    .when("/game",{
                        controller:"gameCtrl",
                        templateUrl:"game.html"
                    })
                    .when("/mine",{
                        controller:"mineCtrl",
                        templateUrl:"mine.html"
                    })
                    .when("/setting",{
                        controller:"settingCtrl",
                        templateUrl:"setting.html"
                    })
                    .otherwise({redirectTo:"/login"});
            }]);
            //主控制器
            app.controller("myCtrl",function($scope){

            });
            //注册页面控制器
            app.controller("loginCtrl",function($scope){
                $scope.name = "";
                $scope.login = function(){
                    if($scope.name == null || $scope.name == ""){
                        alert("用户名不能为空");
                    }
                }
            });
            //主页面控制器
            app.controller("mainCtrl",function($scope){
                $scope.users = [{
                    id:1,
                    name:"张三",
                    pwd:"111",
                    age:22,
                    sex:"男",
                    state:false
                },{
                    id:2,
                    name:"李四",
                    pwd:"222",
                    age:22,
                    sex:"男",
                    state:false
                },{
                    id:3,
                    name:"王五",
                    pwd:"333",
                    age:44,
                    sex:"男",
                    state:false
                },{
                    id:4,
                    name:"赵六",
                    pwd:"444",
                    age:55,
                    sex:"男",
                    state:false
                }];

                $scope.deleteSel = function(){
                    //定义空数组,保存选中项的name
                    var arr = [];
                    //遍历数据源,把选中项的名字添加到数组中。
                    for(index in $scope.users){
                        if($scope.users[index].state){
                            //$scope.users.splice(index,1);
                            arr.push($scope.users[index].name);
                        }
                    }
                    //遍历含有选中项name属性的数组。有多少个被选中,数据源就会被遍历多少遍。
                    if(arr.length>0){
                        for(i in arr){
                            //对比选中项的名字在数组中的角标,根据角标删除当前对象,删一个数据源少一个。
                            for(i2 in $scope.users){
                                if(arr[i] == $scope.users[i2].name){
                                    $scope.users.splice(i2,1);
                                }
                            }
                        }
                    }else{
                        alert("请选择");
                    }
                }

                //全选方法
                $scope.selectAll = false;
                $scope.selectAllFun = function(){
                    if($scope.selectAll){
                        //alert("afsd");
                        for(index in $scope.users){
                            $scope.users[index].state = true;
                        }
                    }else{
                        for(index in $scope.users){
                            $scope.users[index].state = false;
                        }
                    }
                }

                //检测是否全选
                $scope.checkSelect = function(index){
                    var temp = 0;
                    if($scope.users[index].state == true){
                        alert("asdf");
                        temp++;
                    }else{
                        temp--;
                    }
                    if(temp == $scope.users.length){
                        $scope.selectAll = true;
                    }else{
                        $scope.selectAll = false;
                    }

                    var haha = false;
                    for(i in $scope.users){
                        if($scope.users[i].state == true){

                        }else{
                            haha = true;
                        }
                    }
                    if(haha){
                        $scope.selectAll = false;
                    }else{
                        $scope.selectAll = true;
                    }
                }

                //判断年龄范围
                $scope.size = "--请选择--";
                $scope.ageSize = function(age,size){
                    if(size == "--请选择--"){
                        return true;
                    }else{
                        var arr = size.split("-");
                        var ageMin = arr[0];
                        var ageMax = arr[1];
                        if(age>ageMin && age<ageMax){
                            return true;
                        }else{
                            return false;
                        }
                    }
                }
            });

        </script>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <div class="leftSide">
            <ul>
                <li><a href="#/login">登录</a></li>
                <li><a href="#/main">首页</a></li>
                <li><a href="#/game">游戏</a></li>
                <li><a href="#/mine">我的</a></li>
                <li><a href="#/setting">设置</a></li>
            </ul>
        </div>
        <div class="rightSide" ng-view>

        </div>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个简单的示例代码,用于展示如何使用Vue组件化、Vue路由、Vuex和Axios来实现增删改查功能。 首先,我们需要安装Vue、Vue Router、Vuex和Axios,你可以使用npm或yarn来安装它们。 接下来,我们创建一个Vue组件,用于显示数据列表和执行增删改查操作。在这个组件,我们将使用Vuex来管理数据状态和执行异步操作,使用Axios来进行HTTP请求。 ```html <template> <div> <h1>Todo List</h1> <form @submit.prevent="addTodo"> <input v-model="newTodo" placeholder="Add new todo" /> <button type="submit">Add</button> </form> <ul> <li v-for="(todo, index) in todos" :key="todo.id"> <input type="checkbox" :checked="todo.completed" @change="toggleTodo(todo)" /> <span>{{ todo.text }}</span> <button @click="removeTodo(index)">Remove</button> </li> </ul> </div> </template> <script> import { mapGetters, mapActions } from 'vuex'; export default { computed: { ...mapGetters(['todos']), }, methods: { ...mapActions(['addTodo', 'removeTodo', 'toggleTodo']), }, data() { return { newTodo: '', }; }, }; </script> ``` 接下来,我们使用Vue Router来创建路由路由组件,用于在不同的URL路径下显示不同的组件。在这个例子,我们将创建一个路由,用于显示TodoList组件。 ```javascript import Vue from 'vue'; import VueRouter from 'vue-router'; import TodoList from './components/TodoList.vue'; Vue.use(VueRouter); const routes = [ { path: '/', component: TodoList, }, ]; const router = new VueRouter({ mode: 'history', routes, }); export default router; ``` 最后,我们使用Vuex来管理TodoList组件的状态和操作。我们将创建一个store对象,包含state、mutations、actions和getters。 ```javascript import Vue from 'vue'; import Vuex from 'vuex'; import axios from 'axios'; Vue.use(Vuex); const store = new Vuex.Store({ state: { todos: [], }, mutations: { SET_TODOS(state, todos) { state.todos = todos; }, ADD_TODO(state, todo) { state.todos.push(todo); }, REMOVE_TODO(state, index) { state.todos.splice(index, 1); }, TOGGLE_TODO(state, todo) { todo.completed = !todo.completed; }, }, actions: { async fetchTodos({ commit }) { const response = await axios.get('/api/todos'); commit('SET_TODOS', response.data); }, async addTodo({ commit }, text) { const response = await axios.post('/api/todos', { text, completed: false }); commit('ADD_TODO', response.data); }, async removeTodo({ commit }, index) { await axios.delete(`/api/todos/${state.todos[index].id}`); commit('REMOVE_TODO', index); }, async toggleTodo({ commit }, todo) { await axios.patch(`/api/todos/${todo.id}`, { completed: !todo.completed }); commit('TOGGLE_TODO', todo); }, }, getters: { todos: state => state.todos, }, }); export default store; ``` 现在我们已经完成了使用Vue组件化、Vue路由、Vuex和Axios实现增删改查功能的示例代码。当然,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值