datagrid的editor长度限制
目前官网上没有推荐方法,这里自己造一个
这里使用的方法是把参数嫁接在style里,然后取出style 格式化
// 当前选中行
var rowIndex = undefined
// 收集选中行的所有输入框参数
var collParam = []
$(document).ready(_ => {
initElementTable()
})
function initElementTable() {
$("#el-table").datagrid({
fit: true,
rownumbers:true,
singleSelect:true,
checkOnSelect:false,
selectOnCheck:false,
toolbar: "#el-butoons",
cloumns:[[
{
fileld:"id",
title:"ID",
width: 100,
hidden: true
},{
fileld:"name",
title:"NAME",
width: 100
},{
fileld:"class",
title:"CLASS",
width: 100
},{
fileld:"remark",
title:"REMARK",
width: 100,
// 将参数嫁接在style中
styler: (value, row, index) =>{
return "size:30;color: #F00"
}
editor: {type: 'text', option {required: false}}
}
]],
onClickRow: (index, row) {
if(rowIndex != undefined) {
$("#el-table").datagrid("endEdit", rowIndex).datagrid("clearSelections");
rowIndex = undefined
}
if(rowIndex == undefined) {
$("#el-table").datagrid("beginEdit", index);
rowIndex = index
// 调用监听函数
adjustTextInput("el-table", index)
}
}
})
}
/**
* 监听输入框长度
* 基于cloumn中的style参数读取
* 必须在datagrid编辑状态下有效
* tableId 表的id
* index 选中的当前行
*/
function adjustTextInput(tableId, index) {
// 获取当前行的所有编辑框
let rowEdits = $("#"+ tableId).datagrid("getEditors",index)
rowEdits.forEach((x, i)=> {
// 参数赋空
collParam[i] = {}
// 获取className
let className = x.target[0].className + ""
// 只有text的长度小于24,其他状态需要根据class自行判断,number类型无法使用监听
if(className.length < 24) {
try {
// 获取到 column 的样式,也就是嫁接的那个styler offsetParent取决于框架生成的深度,可以打印一下.attributes.style之前的返回值,确定是否是我们设置的style
let style = x.target[0].offsetParent.offsetParent.offsetParent.attributes.style
// 此时,获取到的style是一个参数而不是具体的值,需要将值取出来
let nodeValue = $(style)[0].nodeValue
// 将style样式参数转化成json格式
while(true) {
if(nodeValue.lastIndexOf(";") == nodeValue.length -1) {
// 如果最后一位是“;” 则将最后一位删除
nodeValue.subString(0,nodeValue.length -1)
}
let name = nodeValue.subString(0,nodeValue.indexOf(":"))
let value = nodeValue.subString(nodeValue.indexOf(":")+1, nodeValue.indexOf(";") == -1? nodeValue.length : nodeValue.indexOf(";"))
collParam[i][name] = value
if(nodeValue.indexOf(";")== -1) {
break;
}
nodeValue = nodeValue.subString(nodeValue.indexOf(";")+1, nodeValue.length)
}
} catch(err) {
}
// 给每一个输入框绑定输入事件
x.target.on("input", data => {
// 截取字段
data.currentTarget.value = data.currentTarget.value.slice(0,collParam[i].size)
})
}
})
}