• Vue基础案例-成绩显示


     成绩排序应该按照降序排

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <div id="app">搜索:<input type="text" v-model="mincore">~<input type="text" v-model="maxcore"><br> 排名方式:
    11. <input type="radio" v-model="chose" id="总分" value="总分" @click="choice">总分 <input type="radio" v-model="chose" id="'数学" value="数学" @click="choice">数学 <input type="radio" v-model="chose" id='语文' value="语文" @click="choice">语文 <input type="radio"
    12. v-model="chose" id="英语" value="英语" @click="choice">英语
    13. <table>
    14. <thead>
    15. <th>排名</th>
    16. <th>姓名</th>
    17. <th>数学</th>
    18. <th>语文</th>
    19. <th>英语</th>
    20. <th>总分</th>
    21. </thead>
    22. <tbody>
    23. <tr v-for="(item,index) in showcore">
    24. <td>{{index+1}}</td>
    25. <td>{{item.name}}</td>
    26. <td>{{item.math}}</td>
    27. <td>{{item.chinese}}</td>
    28. <td>{{item.eng}}</td>
    29. <td>{{item.total}}</td>
    30. </tr>
    31. </tbody>
    32. </table>
    33. </div>
    34. <script src="/demo3_824/js文件/vue.js"></script>
    35. <script>
    36. var data = {
    37. message: [{
    38. name: '张三',
    39. math: 97,
    40. chinese: 89,
    41. eng: 67
    42. }, {
    43. name: '王五',
    44. math: 72,
    45. chinese: 55,
    46. eng: 78
    47. }, {
    48. name: '赵茜',
    49. math: 32,
    50. chinese: 45,
    51. eng: 78
    52. }, {
    53. name: '孙素',
    54. math: 45,
    55. chinese: 67,
    56. eng: 89
    57. }],
    58. mincore: 0,
    59. maxcore: 300,
    60. chose: '总分',
    61. //比较函数
    62. compare(p) {
    63. return function(m, n) {
    64. var a = m[p]
    65. var b = n[p]
    66. return a - b
    67. }
    68. },
    69. }
    70. const app = new Vue({
    71. el: '#app',
    72. data: data,
    73. computed: {
    74. showcore() {
    75. let showcores = []
    76. for (let i = 0; i < this.message.length; i++) {
    77. total = this.message[i].math + this.message[i].chinese + this.message[i].eng
    78. if (total >= this.mincore && total < this.maxcore) {
    79. //添加total属性
    80. showcores.push(Object.assign(this.message[i], {
    81. total: total
    82. }))
    83. }
    84. }
    85. //默认排序
    86. showcores.sort(this.compare('total'))
    87. return showcores
    88. }
    89. },
    90. methods: {
    91. //排序
    92. choice(event) {
    93. console.log(3);
    94. if (event.target.value == '数学') {
    95. this.showcore.sort(this.compare('math'))
    96. }
    97. if (event.target.value == '语文') {
    98. this.showcore.sort(this.compare('chinese'))
    99. }
    100. if (event.target.value == '英语') {
    101. this.showcore.sort(this.compare('eng'))
    102. }
    103. if (event.target.value == '总分') {
    104. this.showcore.sort(this.compare('total'))
    105. }
    106. }
    107. }
    108. })
    109. </script>
    110. </body>
    111. </html>

    1、给数组中的所有对象追加一个属性

    1. let arr = []
    2. let lilst = [{
    3. name: '小红',
    4. math: 40,
    5. en: 89
    6. }, {
    7. name: '小红',
    8. math: 40,
    9. en: 89
    10. }]
    11. lilst.map((item, index) => {
    12. console.log(item);
    13. let result = item.math + item.en
    14. arr.push(Object.assign(item, {
    15. total: result
    16. }))
    17. })
    18. console.log(arr);

     2、sort方法对对象数组实现排序

    sort()方法汇改变原数组,默认按照字符进行排序

    也可以选择他的某一顺序进行排序

    1. let arr = []
    2. let lilst = [{
    3. name: '小红',
    4. math: 80,
    5. en: 82
    6. }, {
    7. name: '小红',
    8. math: 79,
    9. en: 89
    10. }]
    11. lilst.map((item, index) => {
    12. console.log(item);
    13. let result = item.math + item.en
    14. arr.push(Object.assign(item, {
    15. total: result
    16. }))
    17. })
    18. //比较函数
    19. function compare(p) {
    20. return function(m, n) {
    21. var a = m[p]
    22. var b = n[p]
    23. return a - b //升序
    24. }
    25. }
    26. arr.sort(compare('math'))
    27. console.log(lilst, arr);

     3、数组排序

    1. let arr = [2, 5, 7, 2, 523, 435, 64]
    2. function compare(a, b) {
    3. return b - a //降序
    4. }
    5. arr.sort((a, b) => {
    6. return a - b
    7. })
    8. console.log(arr);

    4、sort按照时间进行排序

    1. let arr = [{
    2. "applyId": "1909101016056138768202215",
    3. "createTime": "2019-09-10 10:16:02",
    4. "status": 0,
    5. "userName": "李某某"
    6. }, {
    7. "applyId": "1909101027225522012190631",
    8. "createTime": "2019-09-10 10:07:19",
    9. "status": 1,
    10. "userName": "李某某"
    11. }, {
    12. "applyId": "1909101027225562448906248",
    13. "createTime": "2019-09-10 10:27:19",
    14. "status": 3,
    15. "userName": "李某某"
    16. }]
    17. arr.sort((a, b) => {
    18. return Date.parse(a.createTime) - Date.parse(b.createTime)
    19. })
    20. console.log(arr)

     注意:此时需要将时间转换为时间戳在进行比较

    5、input表单标签

    type:类型    disabled:表示此表单控件不可用   name:控件的名称,与表单数据一起提交

    placeholder:提示用户输入框的格式(与label元素区别) value:空间的初始值

    radio单选按钮:如果name一样的话,name这些radio就是一组一组的,只能有一个被选中

    如果想多个选中,可以将name换成不同的名字,也可以直接使用复选按钮

    1. //name相同才能实现单选按钮。
    2. <input type="radio" name="question1" value="boy">
    3. <input type="radio" name="question1" value="girl">
    4. //如果用v-model绑定,则无需name参数。
    5. <input type="radio" v-model="rank" value="total">
    6. <input type="radio" v-model="rank" value="math">
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <style>
    9. .active {
    10. color: orange
    11. }
    12. </style>
    13. </head>
    14. <body>
    15. <div id="app">
    16. <div>
    17. <span>搜索:</span><input type="text" v-model="min"><span>~</span><input type="text" v-model="max">
    18. </div>
    19. <div>
    20. <span>排名方式:</span>
    21. <input type="radio" value="total" v-model="rank">总分
    22. <input type="radio" value="math" v-model="rank">数学
    23. <input type="radio" value="chinese" v-model="rank">语文
    24. <input type="radio" value="english" v-model="rank">英语
    25. </div>
    26. <table>
    27. <thead>
    28. <tr>
    29. <th>排名</th>
    30. <th>姓名</th>
    31. <th>数学</th>
    32. <th>语文</th>
    33. <th>英语</th>
    34. <th>总分</th>
    35. </tr>
    36. </thead>
    37. <!-- 先筛选再排序,遍历排序后的数组 -->
    38. <tr v-for="(item,index) in sortScoreData" :key="index">
    39. <td>{{index+1}}</td>
    40. <td>{{item.name}}</td>
    41. <td>{{item.math}}</td>
    42. <td>{{item.chinese}}</td>
    43. <td>{{item.english}}</td>
    44. <td>{{oneTotal(item)}}</td>
    45. </tr>
    46. </table>
    47. </div>
    48. <script src="/demo3_824/js文件/vue.js"></script>
    49. <script>
    50. let data = {
    51. scoresData: [{
    52. name: '张三',
    53. math: 97,
    54. chinese: 89,
    55. english: 67
    56. }, {
    57. name: '李四',
    58. math: 67,
    59. chinese: 52,
    60. english: 98
    61. }, {
    62. name: '王五',
    63. math: 72,
    64. chinese: 87,
    65. english: 89
    66. }, {
    67. name: '赵钱',
    68. math: 92,
    69. chinese: 87,
    70. english: 59
    71. }, {
    72. name: '孙李',
    73. math: 47,
    74. chinese: 85,
    75. english: 92
    76. }],
    77. rank: "total", //记录当前用户选择的排序方式
    78. min: 0,
    79. max: 300
    80. }
    81. const app = new Vue({
    82. el: '#app',
    83. data: data,
    84. computed: {
    85. oneTotal() {
    86. return function(item) {
    87. return item.math + item.chinese + item.english
    88. }
    89. },
    90. //计算数得到筛选后的数组
    91. filterScoreData() {
    92. return this.scoresData.filter(item => {
    93. if (this.rank == 'total') {
    94. return (this.oneTotal(item)) > this.min && (this.oneTotal(item)) <= this.max
    95. }
    96. return item[this.rank] >= this.min && item[this.rank] <= this.max
    97. })
    98. },
    99. //计算属性得到排序后的数组
    100. sortScoreData() {
    101. switch (this.rank) {
    102. case 'total':
    103. return this.filterScoreData.sort((a, b) => (this.oneTotal(b)) - (this.oneTotal(a)))
    104. break;
    105. case 'math':
    106. return this.filterScoreData.sort((a, b) => b.math - a.math)
    107. break;
    108. case 'chinese':
    109. return this.filterScoreData.sort((a, b) => b.chinese - a.chinese)
    110. break;
    111. case 'english':
    112. return this.filterScoreData.sort((a, b) => b.english - a.english)
    113. break;
    114. }
    115. }
    116. }
    117. })
    118. </script>
    119. </body>
    120. </html>

  • 相关阅读:
    Python应用程序:从Android日志到Excel文件的智能过滤和输出
    开始使用Filebeat
    【Unity3D】拖尾TrailRenderer
    使用 R 构建复杂设计调查加权(Survey-Weighted) Cox 模型的列线图
    Kudu知识点
    SpringCloud 微服务与远程调用测试
    做个清醒的程序员之要不要做程序员
    DoozyUI⭐️二十一、Input:按键输入监听组件
    [免费专栏] ATTACK安全之检测车机中ADB远程调试控制Android系统攻击
    Flink基础概念入门
  • 原文地址:https://blog.csdn.net/qq_45387575/article/details/126814850