美文网首页vueelementUIVue+ElementUI
Vue+ElementUI项目笔记(四、表单+计算)

Vue+ElementUI项目笔记(四、表单+计算)

作者: 怪兽别跑biubiubi | 来源:发表于2017-11-05 21:51 被阅读0次

    模板

    <template>
     <div id='volume'>
        <el-table :data="tableData" style="width: 100%" align='center' show-summary>
            <el-table-column type="index">
            </el-table-column>
            <el-table-column label="长(cm)" property="length" align='center'>
                <template slot-scope="scope">
                    <div slot="reference" >
                        <el-input size="medium" v-model="scope.row.length" ref='length'></el-input>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="宽(cm)" property="width" align='center'>
                <template slot-scope="scope">
                    <div slot="reference" >
                        <el-input size="medium" v-model="scope.row.width" ref='width'></el-input>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="高(cm)" property="height" align='center'>
                <template slot-scope="scope">
                    <div slot="reference" >
                        <el-input size="medium" v-model="scope.row.height" ref='height'></el-input>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="件数" property="quantity" align='center'>
                <template slot-scope="scope">
                    <div slot="reference" >
                        <el-input size="medium" v-model="scope.row.quantity" ref='quantity' @keyup.enter.native="volumeAdd(scope.$index,scope.row)">
                            
                        </el-input>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="计算体积(m3)" align='center' property="volume">
                <template slot-scope="scope">
                    <div slot="reference" ref='volume'>
                        <p>{{ scope.row.volume }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="计算重量(kg)" align='center' property="volume_weight">
                <template slot-scope="scope">
                    <div slot="reference" >
                        <p>{{ scope.row.volume_weight }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align='center'>
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
        </el-table>
    </div>
    </template>
    

    交互

    <script>
        引入计算插件
       const {Decimal} = require("../../utils/count.js")
       export default {
    name: 'volume',
    data() {
        return {
            tableData: [{
                
            }],
            getLength:null,
        }
    },
    methods: {
        handleDelete(index, row) {
            console.log(index, row);
            if(this.tableData.length > 1){
                this.tableData.splice(index, 1);
            }
            
        },
        回车事件
        volumeAdd:function(index,row){
            获取长、宽、高、件数、体积、重量的value值
            this.dataList = {
                length:this.$refs.length.value,
                width:this.$refs.width.value,
                height:this.$refs.height.value,
                quantity:this.$refs.quantity.value,
                volume:'',
                volume_weight:''
            };
                       
            let {length:l, width:w, height:h, quantity:n} = this.dataList;
             if(l && w && h && n){
                计算体积
                this.dataList.volume = Decimal.add(1).mul(l).mul(w).mul(h).mul(n).div(1000000).toNumber()
                计算重量        
                this.dataList.volume_weight = Math.round(Decimal.add(1).mul(l).mul(w).mul(h).mul(n).div(6000).toNumber())
                输入体积        
                row.volume = this.dataList.volume
                输入重量
                row.volume_weight = this.dataList.volume_weight
                
            }
            最后一条数据回车时添加一条数据
            if(index == this.tableData.length-1){
                this.tableData.push(
                    {
                        length: '',
                        width: '',
                        height: '',
                        piece: '',
                        volume:'',
                        volume_weight:''
                    }
                )
            }
         }
      }
    }
    </script>
    

    count.js计算插件

      /** 小数计算 
      * @example: 
      * 0.1+0.2     //0.30000000000000004 
      * var a=Decimal('0.1');var b=Decimal('0.2'); 
      * a.add(b).toNumber()    //0.3 
      * 四舍五入,保留一位小数 
      * a.add(b).add(0.14).toNumber(1)  //0.4 
      * Decimal.add(0.1,0.2,0.3).toNumber()  //0.6 
      * Decimal.add([0.1,0.2,0.3]).toNumber()  //0.6 
      * (0.1+0.2+0.3)*2/0.5      //2.4000000000000004 
       * Decimal.add([0.1,0.2,0.3]).mul(2).div(0.5).toNumber() //2.4 
        * */
    
    (function (ROOT, factory) {
    if (typeof exports === 'object') {
    // Node.  
    module.exports = factory();
    } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.  
    define(factory);
    } else {
    // Browser globals (root is window)  
    ROOT.Decimal = factory();
    }
     }((0, eval)(this), function () {
      var DECIMAL_SEPARATOR = '.';
    
      // Decimal  
        var Decimal = function (num) {
    if (!this || this.constructor != Decimal) {
      return new Decimal(num);
    }
    
    if (num instanceof Decimal) {
      return num;
    }
    
    this.internal = String(num);
    this.as_int = as_integer(this.internal);
    
    this.add = function (target) {
      var operands = [this, new Decimal(target)];
      operands.sort(function (x, y) {
        return x.as_int.exp - y.as_int.exp
      });
    
      var smallest = operands[0].as_int.exp;
      var biggest = operands[1].as_int.exp;
    
      var x = Number(format(operands[1].as_int.value, biggest - smallest));
      var y = Number(operands[0].as_int.value);
    
      var result = String(x + y);
    
      return Decimal(format(result, smallest));
    };
    
    this.sub = function (target) {
      return Decimal(this.add(target * -1));
    };
    
    this.mul = function (target) {
      target = new Decimal(target);
      var result = String(this.as_int.value * target.as_int.value);
      var exp = this.as_int.exp + target.as_int.exp;
    
      return Decimal(format(result, exp));
    };
    
    this.div = function (target) {
      target = new Decimal(target);
    
      var smallest = Math.min(this.as_int.exp, target.as_int.exp);
    
      var x = Decimal.mul(Math.pow(10, Math.abs(smallest)), this);
      var y = Decimal.mul(Math.pow(10, Math.abs(smallest)), target);
      return Decimal(x / y);
    };
    
    this.toString = function (precision) {
      if (isNumber(precision)) {
        return '' + toFixed(Number(this.internal), precision);
      }
      return this.internal;
    };
    
    this.toNumber = function (precision) {
      if (isNumber(precision)) {
        return toFixed(Number(this.internal), precision);
      }
      return Number(this.internal);
    }
    };
    
    var as_integer = function (number) {
    number = String(number);
    
    var value,
      exp,
      tokens = number.split(DECIMAL_SEPARATOR),
      integer = tokens[0],
      fractional = tokens[1];
    
    if (!fractional) {
      var trailing_zeros = integer.match(/0+$/);
    
      if (trailing_zeros) {
        var length = trailing_zeros[0].length;
        value = integer.substr(0, integer.length - length);
        exp = length;
      } else {
        value = integer;
        exp = 0;
      }
    } else {
      value = parseInt(number.split(DECIMAL_SEPARATOR).join(''), 10);
      exp = fractional.length * -1;
    }
    
    return {
      'value': value,
      'exp': exp
    };
    };
    
    
     // Helpers  
    var neg_exp = function (str, position) {
    position = Math.abs(position);
    
    var offset = position - str.length;
    var sep = DECIMAL_SEPARATOR;
    
    if (offset >= 0) {
      str = zero(offset) + str;
      sep = '0.';
    }
    
    var length = str.length;
    var head = str.substr(0, length - position);
    var tail = str.substring(length - position, length);
    return head + sep + tail;
    };
    
     var pos_exp = function (str, exp) {
    var zeros = zero(exp);
    return String(str + zeros);
    };
    
     var format = function (num, exp) {
    num = String(num);
    var func = exp >= 0 ? pos_exp : neg_exp;
    return func(num, exp);
     };
    
     var zero = function (exp) {
    return new Array(exp + 1).join('0');
     };
    
      var methods = ['add', 'mul', 'sub', 'div'];
     for (var i = 0; i < methods.length; i++) {
    (function (method) {
      Decimal[method] = function () {
        var args = [].slice.call(arguments);
        if (isArray(args[0])) {
          args = args[0];
        }
        if (args.length == 1) {
          return new Decimal(args[0]);
        }
        var option = args[args.length - 1];
    
        var sum = new Decimal(args[0]),
          index = 1;
        while (index < args.length) {
          sum = sum[method](args[index]);
          index++;
        }
        return sum;
      };
    })(methods[i]);
     }
    
     var toFixed = function (number, precision) {
    var multiplier = Math.pow(10, precision + 1),
      wholeNumber = Math.floor(number * multiplier);
    return Math.round(wholeNumber / 10) * 10 / multiplier;
      };
     var isNumber = function (o) {
    return Object.prototype.toString.call(o).slice(8, -1) === 'Number';
     };
      var isArray = function (o) {
    return Object.prototype.toString.call(o).slice(8, -1) === 'Array';
      };
    var isObject = function (o) {
    return Object.prototype.toString.call(o).slice(8, -1) === 'Object';
     };
    return Decimal;
     }));  
    

    相关文章

      网友评论

        本文标题:Vue+ElementUI项目笔记(四、表单+计算)

        本文链接:https://www.haomeiwen.com/subject/yiwhmxtx.html