在进行下一步之前,还有一个重要的测试要做:参数化。其实目前进度基本上9和10已经ok了。
我们希望传入表格数据是灵活可变的,这就包括了:
对于表格来说,列是比较重要的。
原来的一些关键变量全部注释掉,用一个jinja变量替代,主要要使用safe过滤器。
var table = new Tabulator("#example-table", {
height:205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
data:[], //assign data to table
layout:"fitColumns", //fit columns to width of table (optional)
addRowPos: "selected",
columns:
// [ //Define Table Columns
// {formatter:"rowSelection", titleFormatter:"rowSelection", hozAlign:"center", headerSort:false, cellClick:function(e, cell){
// cell.getRow().toggleSelect();
// }},
// {title:"Name", field:"name", width:150,editor:MyCellEdit01},
// {title:"Habit", field:"habit",editor:MyCellEdit01},
// {title:"Create Time", field:"create_time", sorter:"date", hozAlign:"center"},
// {title:"Update Time", field:"update_time", sorter:"date", hozAlign:"center"},
// ],
// rowClick:function(e, row){
// _curRow = row;
// }
[[jinja_test2 |safe]]
});
在视图端,对应的给到响应的数据。所以,将关键变量存在数据库中,然后拼成所需的字符串即可。这样表格的创建就是完全动态的。
@app.route('/jinja_test/', methods=['GET','POST'])
def jinja_test():
jinja_test2 =''' [ //Define Table Columns
{formatter:"rowSelection", titleFormatter:"rowSelection", hozAlign:"center", headerSort:false, cellClick:function(e, cell){
cell.getRow().toggleSelect();
}},
{title:"Name", field:"name", width:150,editor:MyCellEdit01},
{title:"Habit", field:"habit",editor:MyCellEdit01},
{title:"Create Time", field:"create_time", sorter:"date", hozAlign:"center"},
{title:"Update Time", field:"update_time", sorter:"date", hozAlign:"center"},
],
rowClick:function(e, row){
_curRow = row;
}'''
jinja_refresh_button =''''''
return render_template('jinja_test.html',
jinja_refresh_button=jinja_refresh_button,jinja_test2=jinja_test2)
这块有些超出我的预料:
我通过Tabulator自带的,根据ID获取表格的方法来找到对象,然后进行 destroy
function destable(){
the_table = Tabulator.findTable("#example-table")[0]
the_table.destroy()
}
然后将重建表格,以及一系列的事件放在新的函数里
function build_table(){
var MyCellEdit01 = function(cell, onRendered, success, cancel){
//cell - the cell component for the editable cell
//onRendered - function to call when the editor has been rendered
//success - function to call to pass thesuccessfully updated value to Tabulator
//cancel - function to call to abort the edit and return to a normal cell
//create and style input
// var cellValue = luxon.DateTime.fromFormat(cell.getValue(), "dd/MM/yyyy").toFormat("yyyy-MM-dd"),
var cellValue = cell.getValue()
input = document.createElement("input");
row = cell.getRow()
$(row.getElement()).css("background-color",'')
$(row.getElement()).css("color", "")
// input.setAttribute("type", "date");
input.style.padding = "4px";
input.style.width = "100%";
input.style.boxSizing = "border-box";
input.value = cellValue;
onRendered(function(){
input.focus();
input.style.height = "100%";
input.style.background ='yellow'
});
function onChange(){
if(input.value != cellValue){
// the_id = cell.getData().id
// alert(the_id )
// success(alert(input.value));
row_id = cell.getData().id
colname = cell.getColumn().getField()
// row = cell.getRow()
// row.style.background ='red'
filter_dict = {}
attr_dict = {}
filter_dict['tid'] = row_id
attr_dict[colname] = input.value
attr_dict['is_enable'] = 1
attr_dict['update_time']= getDatetime()
// alert(colname)
para_dict = {}
para_dict['filter_dict'] = filter_dict
para_dict['attr_dict'] = attr_dict
row.update({'update_time' :attr_dict['update_time']})
post_json('/update_a_cell/', para_dict, $(cell.getElement()))
success(input.value);
// cellValue = input.value
}else{
cancel();
}
}
//submit new value on blur or change
input.addEventListener("blur", onChange);
//submit new value on enter
input.addEventListener("keydown", function(e){
if(e.keyCode == 13){
onChange();
}
if(e.keyCode == 27){
cancel();
}
});
return input;
};

通过Destroy Table和Build Table两个按钮来模拟未来的表格重置事件,看起来是ok的。