html table 转json
max.bai
2018-01
最近django做接口平台,前端需要table转json格式,自己写了一个,记录一下
1. html table 设置
首先规范table格式
a. table一定要有thead th,tbody
b. 每个th 要有name属性,name的值将用来做json里么的key值
c. td 中需要获得值得控件添加jsonval 属性,不用设置值,这样脚本就知道要取哪个控件的值了,没有设置jsonval,值默认设置为空字符串
例子:
<table class="table" id="pcheck_items">
<thead>
<tr>
<th name="check_key">Key</th>
<th name="check_module">Validation</th>
<th name="check_value">Expect</th>
<th name="action"></th>
</tr>
</thead>
<tbody>
<tr>
<td><input josnval="" id="check_keypath" class="form-control" value="" /></td>
<td><select josnval="" class="form-control" id="pcheck_module"><option value="backend.utils.DefaultChecker.Contains">Contains</option><option value="backend.utils.DefaultChecker.End_with">End_with</option><option value="backend.utils.DefaultChecker.Equal">Equal</option><option value="backend.utils.DefaultChecker.Josn_check">Josn_check</option><option value="backe