vue实现四则运算封装组件

你的名字 2021-09-27 22:54 684阅读 0赞
  1. <template>
  2. <div>
  3. <el-row class="tableRowAlign">
  4. <el-col :span="24">
  5. <span>指标算法:</span>
  6. <el-button type="primary" @click="formulaSymbol('+',1)">+</el-button>
  7. <el-button type="primary" @click="formulaSymbol('-',1)">-</el-button>
  8. <el-button type="primary" @click="formulaSymbol('*',1)">*</el-button>
  9. <el-button type="primary" @click="formulaSymbol('/',1)">/</el-button>
  10. <el-button type="primary" @click="formulaSymbol('(',2)">(</el-button>
  11. <el-button type="primary" @click="formulaSymbol(')',2)">)</el-button>
  12. <el-button type="primary" @click="validateFormula" icon="el-icon-check">规则验证</el-button>
  13. </el-col>
  14. </el-row>
  15. <el-row class="tableRowAlign">
  16. <el-col :span="24">
  17. <el-tag
  18. style="background-color:#009fff;height: 28px"
  19. v-for="tag in tags"
  20. :key="tag.name"
  21. closable
  22. type="primary"
  23. @close="handleClose(tag)"
  24. :type="tag.type">
  25. {
  26. {tag.name}}
  27. </el-tag>
  28. </el-col>
  29. </el-row>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. tags: [],
  37. dBExpress:[],
  38. uIExpress:[]
  39. };
  40. },
  41. methods: {
  42. getDBExpress(){
  43. let dbStr=''
  44. for (let i = 0; i < this.dBExpress.length; i++) {
  45. dbStr += this.dBExpress[i]
  46. }
  47. return dbStr
  48. },
  49. getUIExpress(){
  50. let uiStr=''
  51. for (let i = 0; i < this.uIExpress.length; i++) {
  52. uiStr += this.uIExpress[i]
  53. }
  54. return uiStr
  55. },
  56. //验证公式是否符合规则
  57. validateFormula () {
  58. debugger
  59. let dbStr = this.getDBExpress()
  60. let result = this.fondamentalJudge(dbStr)
  61. if (!result){
  62. this.$message.warning({
  63. message:'规则不符合要求!'
  64. })
  65. }else{
  66. this.$message.info({
  67. message:'规则正确!'
  68. })
  69. }
  70. console.log(result)
  71. return result
  72. },
  73. handleClose (tag) {
  74. debugger
  75. this.tags.splice(this.tags.indexOf(tag), 1);
  76. this.dBExpress.splice(this.tags.indexOf(tag), 1); //删除记录db
  77. this.uIExpress.splice(this.tags.indexOf(tag), 1); //删除记录用户显示的
  78. },
  79. formulaSymbol(param,type){
  80. if (type===1){ //现在是符号上一个不能是符号
  81. if (this.tags.length===0) { //第一个就是符号
  82. this.$message.warning({
  83. message: '规则不符合要求!'
  84. })
  85. return
  86. }else {
  87. let tagType= this.tags[this.tags.length-1].type
  88. if (type===tagType){
  89. this.$message.warning({
  90. message: '规则不符合要求!'
  91. })
  92. return
  93. }
  94. }
  95. }
  96. this.dBExpress.push(param) //记录db
  97. this.uIExpress.push(param) //记录用户显示的
  98. let tag={
  99. name:param,
  100. type:type,
  101. times:new Date().getTime()
  102. }
  103. this.tags.push(tag) //tab标签显示
  104. },
  105. //调用改组件的页面给该组件传递参数
  106. neTypeClick(param){
  107. //清空数组
  108. this.dBExpress=[]
  109. this.uIExpress=[]
  110. this.tags=[]
  111. },
  112. //调用改组件的页面给该组件传递参数
  113. setFondamentalRules(param){
  114. debugger
  115. this.dBExpress.push('{'+param.kpiID+'}') //记录db
  116. this.uIExpress.push('{'+param.label+'}') //记录用户显示的
  117. if (this.tags.length > 0) {
  118. let tagType = this.tags[this.tags.length - 1].type
  119. if (-1 === tagType) {
  120. this.$message.warning({
  121. message: '规则不符合要求!'
  122. })
  123. return
  124. }
  125. }
  126. let tag={
  127. name:param.label,
  128. kpiID:param.kpiID,
  129. type:param.type,
  130. times:new Date().getTime()
  131. }
  132. this.tags.push(tag) //tab标签显示
  133. },
  134. //判断四则运算的函数
  135. fondamentalJudge (string) {
  136. //去除左边及右边的括号(这个是我的公式里面有特殊字符)
  137. let str= string.replace('{', '').replace('}','')
  138. // 剔除空白符
  139. string = str.replace(/\s/g, '')
  140. // 错误情况,空字符串
  141. if ('' === string) {
  142. return false
  143. }
  144. // 错误情况,运算符连续
  145. if (/[\+\-\*\/]{2,}/.test(string)) {
  146. return false
  147. }
  148. // 空括号
  149. if (/\(\)/.test(string)) {
  150. return false
  151. }
  152. // 最后一个符号是加、减、乘、除符号
  153. if (/[\+\-\*\/]$/.test(string)) {
  154. return false
  155. }
  156. // 错误情况,括号不配对
  157. var stack = []
  158. for (var i = 0, item; i < string.length; i++) {
  159. item = string.charAt(i)
  160. if ('(' === item) {
  161. stack.push('(')
  162. } else if (')' === item) {
  163. if (stack.length > 0) {
  164. stack.pop()
  165. } else {
  166. return false
  167. }
  168. }
  169. }
  170. if (0 !== stack.length) {
  171. return false
  172. }
  173. // 错误情况,(后面是运算符
  174. if (/\([\+\-\*\/]/.test(string)) {
  175. return false
  176. }
  177. // 错误情况,)前面是运算符
  178. if (/[\+\-\*\/]\)/.test(string)) {
  179. return false
  180. }
  181. // 错误情况,(前面不是运算符
  182. if (/[^\+\-\*\/]\(/.test(string)) {
  183. return false
  184. }
  185. // 错误情况,)后面不是运算符
  186. if (/\)[^\+\-\*\/]/.test(string)) {
  187. return false
  188. }
  189. // **错误情况最后一个字符是**+-*/
  190. if (/\*[\+\-\*\/]$/.test(string)) {
  191. return false
  192. }
  193. return true
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped>
  199. @import '../../assets/style/querystyle.css';
  200. .el-tag + .el-tag {
  201. margin-left: 2px;
  202. margin-top: 10px;
  203. }
  204. span {
  205. color: #d9d9d9;
  206. //font-size: medium;
  207. }
  208. </style>

组件结果:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,684人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue封装组件的过程

    vue组件的定义 ● 组件(Component)是Vue.js最强大的功能之一 ● 组件可以扩展HTML元素,封装可重用代码 ● 在较高层面上,组件是自定义元素,Vue.

    相关 Vue封装树形菜单组件

    csdn终于更新完成了哈,ok,步入正题 像这种树形结构的核心思想就是:递归思想,知道使用递归,其实这个例子函数的封装也就很简单喽  实现步骤: