This article will introduce to you how to use dataTable to directly enter table row data in the Bootstrap development framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

When developing Winform, we can often use table controls to directly enter data. However, we rarely see it on the Web. In fact, we can also use the dataTable object to directly enter data. Entering table row data can improve the convenience of data entry, especially when it comes to detailed data for some simple businesses. It seems to be more convenient and taller than pop-up window entry. This article mainly introduces the use of dataTable to directly enter table row data in the Bootstrap development framework. [Related recommendations: "bootstrap Tutorial"]
1. Direct data entry based on tables and Winform interface review
Directly start the Web interface Before entering the table row data, let's first take a look at the processing of the Winform interface. For example, in process management, I use the following interface for data processing of the reimbursement business table with master-slave details.

This kind of detailed form can be added and edited directly on the grid control Griview.
As for the Web interface, if we want to maintain a layout similar to this, we can also use dataTable to directly enter table row data.

When the above interface processes detailed data, you can directly use the new record, enter the data directly in the input box, and then save it. After saving, the data becomes read-only. , if you need to modify it, you can also click the edit button to modify it.
These detailed data only exist in JS objects and have not been saved to the backend database. We can obtain all the added data for submission during the final save (as shown in the OK button on the interface above). That’s it.
After submitting these data, we can use the Bootstrap Table plug-in to display the data in the viewing interface.

2. Implementation of using dataTable to directly enter table row data on the Web
The above interface shows how to use dataTable on the Web dataTable directly inputs table row data and data display. Here we begin to introduce their interface and implementation code.
The interface part mainly deals with this detail.

The HTML code of the interface view is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <div class = "portlet light portlet-fit " >
<div class = "portlet-title" >
<div class = "caption" >
<i class = "icon-settings font-red" ></i>
<span class = "caption-subject font-red sbold uppercase" >明细清单</span>
</div>
</div>
<div class = "portlet-body" >
<div class = "table-toolbar" >
<div class = "row" >
<div class = "col-md-6" >
<div class = "btn-group" >
<button id= "detail_editable_1_new" class = "btn green" >
新增记录
<i class = "fa fa-plus" ></i>
</button>
</div>
</div>
</div>
</div>
<table class = "table table-striped table-hover table-bordered" id= "detail_editable_1" >
<thead>
<tr>
<th>序号</th>
<th> 费用类型 </th>
<th> 发生时间 </th>
<th> 费用金额(元) </th>
<th> 费用说明 </th>
<th> 编辑 </th>
<th> 删除 </th>
</tr>
</thead>
<tbody>
@*<tr>
<td> 1 </td>
<td> 交通费 </td>
<td> 2018-10-01 </td>
<td> 2000 </td>
<td> 备注信息 </td>
<td>
<a class = "edit" href= "javascript:;" > 编辑 </a>
</td>
<td>
<a class = "delete" href= "javascript:;" > 删除 </a>
</td>
</tr>*@
</tbody>
</table>
</div>
</div>
|
Copy after login
The main one is the tag with ID detail_editable_1. This is the table that carries detailed information. We can define the header information we need, and the content of the input box can be dynamically added through the object of the dataTable plug-in. .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var table = $('#detail_editable_1');
var oTable = table.dataTable({
"lengthMenu" : [
[5, 15, 20, -1],
[5, 15, 20, "All" ]
],
"language" : {
url: '
},
"pageLength" : 5,
"columnDefs" : [{
'orderable': true,
'targets': [0]
}, {
"searchable" : true,
"targets" : [0]
}],
"order" : [
[0, "asc" ]
]
});
|
Copy after login
Editing row records means dynamically adding some Input controls so that users can enter data, as shown in the following code.
1 2 3 4 5 6 7 8 9 10 11 12 | function editRow(oTable, nRow) {
var aData = oTable.fnGetData(nRow);
var jqTds = $('>td', nRow);
jqTds[0].innerHTML = '<input type= "text" class = "form-control input-small" value= "' + aData[0] + '" readonly>';
jqTds[1].innerHTML = '<input type= "text" class = "form-control input-small" value= "' + aData[1] + '" >';
jqTds[2].innerHTML = '<input type= "date" class = "form-control input-small" value= "' + aData[2] + '" >';
jqTds[3].innerHTML = '<input type= "number" class = "form-control input-small" value= "' + aData[3] + '" >';
jqTds[4].innerHTML = '<input type= "text" class = "form-control input-small" value= "' + aData[4] + '" >';
jqTds[5].innerHTML = '<a class = "edit" href= "" >保存</a>';
jqTds[6].innerHTML = '<a class = "cancel" href= "" >取消</a>';
}
|
Copy after login
After saving the data, update the record into the corresponding TD object, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var objList = [];
function saveRow(oTable, nRow) {
var jqInputs = $('input', nRow);
oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
oTable.fnUpdate(jqInputs[4].value, nRow, 4, false);
oTable.fnUpdate('<a class = "edit" href= "" >编辑</a>', nRow, 5, false);
oTable.fnUpdate('<a class = "delete" href= "" >删除</a>', nRow, 6, false);
oTable.fnDraw();
}
|
Copy after login
Several action buttons on the interface, such as add, edit, save, delete and other button processing events are as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | var addRow = 1;
$('#detail_editable_1_new').click( function (e) {
e.preventDefault();
if (nNew && nEditing) {
if (confirm( "前面记录没有保存,您是否需要保存?" )) {
saveRow(oTable, nEditing);
nEditing = null;
nNew = false;
} else {
oTable.fnDeleteRow(nEditing);
nEditing = null;
nNew = false;
return ;
}
}
var aiNew = oTable.fnAddData([addRow++, '', '', '', '', '', '']);
var nRow = oTable.fnGetNodes(aiNew[0]);
editRow(oTable, nRow);
nEditing = nRow;
nNew = true;
});
table.on('click', '. delete ', function (e) {
e.preventDefault();
if (confirm( "您确认要删除该行记录吗?" ) == false) {
return ;
}
var nRow = $(this).parents('tr')[0];
var aData = oTable.fnGetData(nRow);
var found = false;
$.each(objList, function (i, item) {
if (item[ "seq" ] == aData[0]) {
found = true;
objList.splice(i, 1);
}
});
oTable.fnDeleteRow(nRow);
});
table.on('click', '.cancel', function (e) {
e.preventDefault();
if (nNew) {
oTable.fnDeleteRow(nEditing);
nEditing = null;
nNew = false;
} else {
restoreRow(oTable, nEditing);
nEditing = null;
}
});
table.on('click', '.edit', function (e) {
e.preventDefault();
nNew = false;
var nRow = $(this).parents('tr')[0];
if (nEditing !== null && nEditing != nRow) {
restoreRow(oTable, nEditing);
editRow(oTable, nRow);
nEditing = nRow;
} else if (nEditing == nRow && this.innerHTML == "保存" ) {
saveRow(oTable, nEditing);
nEditing = null;
} else {
editRow(oTable, nRow);
nEditing = nRow;
}
});
}
|
Copy after login
In the last step, when submitting data, we traverse the entire table, obtain the data of each row, and put them into the JSON object list, and then submit it to the background for input. The following is the list of acquisition Data JS code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function GetData() {
var list = [];
var trs = table.fnGetNodes();
for ( var i = 0; i < trs.length; i++) {
var data = table.fnGetData(trs[i]);
var obj = {};
obj[ "FeeType" ] = data[1];
obj[ "OccurTime" ] = data[2];
obj[ "FeeAmount" ] = data[3];
obj[ "FeeDescription" ] = data[4];
list.push(obj);
}
return list;
};
|
Copy after login
After obtaining the detailed data of the form, we determine how to submit it to the MVC backend interface for processing. The following is the JS code for submitting detailed data to the MVC backend in the business.

The C# processing logic code of the background MVC controller is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[HttpPost]
public ActionResult SaveApply(JObject param)
{
dynamic obj = param;
if (obj != null)
{
var result = new CommonResult();
if (obj.info != null)
{
var info = (JObject.FromObject(obj.info)).ToObject<ReimbursementInfo>();
List<ReimbursementDetailInfo> details = null;
if (obj.details != null)
{
details = (JArray.FromObject(obj.details)).ToObject<List<ReimbursementDetailInfo>>();
}
if (info != null)
{
OnBeforeInsert(info);
bool succeed = BLLFactory<Reimbursement>.Instance.Insert(info);
if (succeed)
{
if (details != null)
{
foreach ( var detailInfo in details)
{
detailInfo.Apply_ID = info.Apply_ID;
detailInfo.Header_ID = info.ID;
BLLFactory<ReimbursementDetail>.Instance.InsertUpdate(detailInfo, detailInfo.ID);
}
}
result.Success = succeed;
}
}
}
return ToJsonContent(result);
}
else
{
throw new MyApiException( "传递参数错误" );
}
}
|
Copy after login
For the submitted data, the object information is converted using JObject, and for the detailed list, JArray.FromObject is used for conversion. The other part is how to save the interface of the main table and the detailed table.
The above processing logic and code are to process the front-end acquisition, submission processing, and back-end interface processing of the detailed table. The entire process is mainly used to introduce the use of dataTable to directly enter table row data in the Bootstrap development framework.
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of A brief discussion on how to directly enter table row data in Bootstrap (1). For more information, please follow other related articles on the PHP Chinese website!