antd框架表单数据获取设置

34次阅读

共计 2032 个字符,预计需要花费 6 分钟才能阅读完成。

// 设置列表的表单资源数据
// data 为获取的数据源,columns 对应表头的数组队列
setDataSource = (data, columns) => {

const dataSource = []
for (var i = 0; i < data.length; i++) {const colObj = {}
  colObj.key = i
  for (var k = 0; k < columns.length; k++) {if (columns[k].includes('CNT')) {colObj[columns[k]] = milliFormat(data[i][k])
    } else if (columns[k].includes('RATE')) {colObj[columns[k]] = toPercent(data[i][k])
    } else {colObj[columns[k]] = data[i][k]
    }
  }
  dataSource.push(colObj)
}
return dataSource

}

// 设置列表的表头数据

export const setColumns = (columnsData, pointFilter,type) => {

const columns = []
for (var i = 0; i < columnsData.length; i++) {let obj = {}
  obj.title = columnsData[i]
  obj.dataIndex = columnsData[i]
  obj.key = columnsData[i]
  obj.align = 'center'
  let filters = []
  if (type === 'compare') {
    obj.width = 100
    obj.fixed = 'left'
  }
  if (typeof (pointFilter[i][0]) == 'string') {for (var k = 0; k < pointFilter[i].length; k++) {let filter = {}
      filter.text = pointFilter[i][k]
      filter.value = pointFilter[i][k]
      filters.push(filter)
    }
    obj.filters = filters
    obj.onFilter = (value, record) => record[obj.dataIndex].indexOf(value) === 0
    obj.filterMutiple = true
    obj.sorter = function (a, b) {return a[obj.dataIndex].localeCompare(b[obj.dataIndex])
    }
  }
  if (columnsData[i].includes('CNT')) {obj.sorter = function (a, b) {return toNumber(a[obj.dataIndex]) - toNumber(b[obj.dataIndex])
    }
  } else if (columnsData[i].includes('RATE')) {obj.sorter = function (a, b) {return parseFloat(a[obj.dataIndex].slice(0, -1)) - parseFloat(b[obj.dataIndex].slice(0, -1))
    }
  }
  if (columnsData[i] == 'BUF_RATE') {obj.render = function (text, record, index) {const data = parseFloat(text.slice(0, -1))
      if (data > 8) {return (<span className="yellow">{record.BUF_RATE}</span>)
      } else if (data > 4 && data <= 8) {return (<span className="red">{record.BUF_RATE}</span>)
      } else {return (<span>{record.BUF_RATE}</span>)
      }
    }
  }
  columns.push(obj)
}
return columns

}

// pointData [‘ 河北省 ’,’ 电信 ’,45246,0.32546,0.005456],[…]
// columndata [‘province’, ‘stp’, cnt, buffer, abort]
// 生成关于省份的去重的数组
export const initTablePoints = (pointData, columnData) => {

let initPoints = []
for (var k = 0; k < columnData.length; k++) {let point = []
    for (var i = 0; i < pointData.length; i++) {point.push(pointData[i][k])
    }
    var pointMap = [...new Set(point)]
    initPoints.push(pointMap)

}
return initPoints

}
最终效果图:

正文完
 0