How to realize the Calculator function in WeChat Mini Programs's Development
Today, the editor will share with you the relevant knowledge points about how to realize the calculator function in WeChat Mini Programs's development. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
First, WeChat Mini Programs development tool interface
II. Directory structure
The directory structure of entering the page for the first time is as follows:
Problems that need to be paid attention to
(1) all the new page files added need to be configured in app.json, otherwise the page will report an error.
(2) how it works by adding the event bindtap= "btnClick" id= "{{n9}}" to the click event.
In js code, you can get data through this.data.n9, and the data is defined in js
By filling in id, in the specific function, event.target.id to determine how much id is, to distinguish. It can be realized, click on different tags, and then carry on the business logic. If you need to access the data, it is through this.data.xx.
Calculator's wxml page
{{screenNum}} 9 8 7 + 6 54-3 21 * 0 AC = / / pages/cal/cal.jsPage (initial data of {/ * page * / data: {n 0: 0, n 1: 1, n 2: 2, n 3: 3, n 4: 4, n 5: 5, n 6: 6, n 7: 7 N8: 8, N9: 9, na: "+", nb: "-", nc: "*", nd: "/", screenNum: 0, screenStr: 0, is_num:1}, / * Lifecycle function-listen to page load * / onLoad: function (options) {} / * Lifecycle function-first rendering of listening page completed * / onReady: function () {}, / * Lifecycle function-listening page display * / onShow: function () {}, / * Lifecycle function-listening page hiding * / onHide: function () {} / * Lifecycle function-listens to page unload * / onUnload: function () {}, / * page related event handler-listens to user drop-down action * / onPullDownRefresh: function () {}, / * Handler function for pull-down event on page * / onReachBottom: function () {} / * users click on the upper right corner to share * / onShareAppMessage: function () {}, btnClick:function (event) {/ / console.log ("you pressed the key" + event.target.id) / / console.log ("Last" + this.data.is_num); var op= ""; var data=0; var last_is_num = this.data.is_num / / what if is entered this time (event.target.id = = "9" | | event.target.id = = "8" | | event.target.id = = "7" | | event.target.id = = "6" | | event.target.id = = "5" | | event.target.id = "4" | | event.target.id = = "3" | | event.target.id = = "2" | | event.target.id = = "1" | | event.target.id = "0") {data = event.target.id This.setData ({is_num: 1});} if (event.target.id = = "+" | | event.target.id = = "-" | | event.target.id = = "*" | | event.target.id = = "/") {op = event.target.id; this.setData ({is_num: 0}) } if (last_is_num==1) {/ / if the last time is a digital if (op = "") {/ / this time it is a digital if (this.data.screenNumeric if 0) {this.setData ({screenNum: this.data.screenNum + data}); this.setData ({screenStr: this.data.screenStr + data});} else {this.setData ({screenNum: data}) This.setData ({screenStr: data});}} else {this.setData ({screenNum: this.data.screenNum + op}); this.setData ({screenStr: this.data.screenStr + "," + op+ ","}) }} else {/ / Last is not digital if (data! = 0) {/ / this time it is digital this.setData ({screenNum: this.data.screenNum + data}); this.setData ({screenStr: this.data.screenStr + data});} else {return;}} / / console.log (op+ "aaaaa" + data) / / console.log ("now is" + this.data.is_num); / / console.log ("screenNum" + this.data.screenNum); / / console.log (this.data.screenStr);}, btnJs:function () {console.log (this.data.screenNum); console.log (this.data.screenStr); var result=0; var strs = new Array (); / / define an array strs = this.data.screenStr.split (",") / / character segmentation for (var I = 0; I < strs.length; I +) {/ / console.log (strs [I] + I); / / character output if (strs [I] = = "+") {result = parseInt (strs [I-1]) + parseInt (strs [I + 1]) } if (strs [I] = = "-") {result = strs [I-1]-strs [I + 1];} if (strs [I] = "*") {result = strs [I-1] * strs [I + 1];} if (strs [I] = "/") {result = strs [I-1] / strs [I + 1] }} console.log ("result:" + result); this.setData ({screenNum: result}); this.setData ({screenStr: result});}, btnClear:function () {/ / restore the tag to the default state this.setData ({screenNum: 0}); this.setData ({screenStr: 0}); this.setData ({is_num: 1}) }}) these are all the contents of the article "how to achieve Calculator function in WeChat Mini Programs's Development". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.