前言

这篇讲解小工具,例如时间等等

一、时间戳转时间或者日期

function timeConvert(timestamp,num){//num:0 YYYY-MM-DD  num:1  YYYY-MM-DD hh:mm:ss // timestamp:时间戳 
            timestamp = timestamp+'';
            timestamp = timestamp.length==10?timestamp*1000:timestamp;
            var date = new Date(timestamp);
            var y = date.getFullYear();  
            var m = date.getMonth() + 1;  
            m = m < 10 ? ('0' + m) : m;  
            var d = date.getDate();  
            d = d < 10 ? ('0' + d) : d;  
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var minute = date.getMinutes();
            var second = date.getSeconds();
            minute = minute < 10 ? ('0' + minute) : minute;  
            second = second < 10 ? ('0' + second) : second; 
            if(num==0){
                return y + '-' + m + '-' + d;  
            }else{
                return y + '-' + m + '-' + d +' '+ h +':'+ minute +':' + second;  
            }
}

二、百分比转数字

export function toPoint(percent) {
  var str = 0
  if (!isEmpty(percent)) {
    str = percent.replace('%', '').trim()
    str = str / 100
  }
  return str
}

三、判断字符是否为空的方法

export function isEmpty(obj) {
  if (typeof obj === 'undefined' || obj == null || obj === '') {
    return true
  } else {
    return false
  }
}

四、时间排序

export function dateFormat(fmt, date) {
  let ret
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加,必须转化成字符串
  }
  for (const k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
    }
  }
  return fmt
}

五、给数组排序

export function getSortFun(order, sortBy) {
  var ordAlpah = (order === 'asc') ? '>' : '<'
  return new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1')
}

六、取字符串字符串的最后几位

export function sulast(str) {
  var name
  if (str !== null || str !== '') {
    name = str.substring(str.lastIndexOf('.') + 1)
  }
  return name
}

七、小数转整数

Math.round(row.oldQty)

八、数组重组成新的数组

// _selectData是一个大数组
const mvValue = []
      for (var value of _selectData) {
        var materialNum = value.materialNum
        var vendorCode = value.vendorCode
        mvValue.push({ 'materialNum': materialNum, 'vendorCode': vendorCode })
      }

九、id通过方法传参来灵活控制dom元素

// 调用方
dataBootstrapTable(json, dataColumnsArray1(), 'dataDetail1')
// 实现方法
function dataBootstrapTable(json, columnsArray, dataDetailId) {
    const tableId = '#' + dataDetailId;
    $( tableId ).bootstrapTable({
	// 逻辑代码  
})
}

十、js加载div局部刷新

$(".form-group").load(location.href+" .form-group");

十一、js手动操作dom元素

// 赋值
data.elem.parentElement.parentElement.nextElementSibling.children[1].children[0].value = '不可编辑'
// 不可编辑
data.elem.parentElement.parentElement.nextElementSibling.children[1].children[0].readOnly = true

十二、加一行,减一行

html

<!-- 一行代码 -->
<div data-id="vipLimitLifeTimeTwo" style="text-align: center;">
                            <div class="layui-inline">
                                <label class="layui-form-label">字段</label>
                                <div class="layui-input-inline">
                                    <select name="many_rule_table_column_two_name" style="width: 150px;" lay-verify="required">
                                    </select>
                                </div>
                                <div class="layui-input-inline">
                                    <select name="rule_table_column_where" style="width: 50px;"
                                            lay-filter="clickColumSelect" lay-verify="required">
                                        <option value="">请选择</option>
                                        <option value="1">等于</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">范围</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="rule_table_column_range"
                                           style="width: 100px;display: inline;" autocomplete="off"
                                           class="layui-input"
                                           onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)"
                                           onblur="this.v();" maxlength="9" lay-verify="required|ckColumnRange">
                                </div>
                            </div>
                            <i class="icon-minus" style="font-size: 1.5em;cursor: pointer;padding: 8px;"
                               onclick="changeVipLimit(this, false, 2)" onselectstart="return false;"></i>
</div>

js

// 定义变量
let vipLimitLifeTimeHtml = null;
$(function () {
	vipLimitLifeTimeHtml = '<div id="'+zero+'" data-id="vipLimitLifeTime" style="text-align: center;" >' + $('[data-id="vipLimitLifeTime"]').html() + '</div>';
})
// 获取某一个dom元素
let $div = $(element).parent();
$vipLimitLifeTime = $(vipLimitLifeTimeHtml);
// 追加
div.after($vipLimitLifeTime);

效果
image.png