Common methods of bootstrap table (time control export dynamic drop down box form validation check and get information) code sharing
- 2021-07-15 03:42:53
- OfStack
1. bootstrap-table Click single line to select
$('#gzrwTable').on('click-row.bs.table', function(e, row, $element) {
$('.success').removeClass('success');// Before clearance 1 The selected status of the selected row in the next operation
$($element).addClass('success');// Select the row to add the selected status
});
2. bootstrap-table to get selected row information
function getSelectedRow() {
var index = $('#gzrwTable').find('tr.success').data('index');
return $('#gzrwTable').bootstrapTable('getData')[index];
}
3. The time control fills in the default current time information
var date = new Date();
var mon = date.getMonth() + 1;
var day = date.getDate();
var nowDay = date.getFullYear() + "-"
+ (mon < 10 ? "0" + mon : mon) + "-"
+ (day < 10 ? "0" + day : day);
$("#endTime").val(nowDay);
4. bootstrap-table Verify form information based on name value
function checkForm(formId) {
$(formId).bootstrapValidator({
message : 'This value is not valid',
feedbackIcons : {
valid : 'glyphicon glyphicon-ok',
invalid : 'glyphicon glyphicon-remove',
validating : 'glyphicon glyphicon-refresh'
},
fields : {
task : {
group : '.col-sm-10',// Corresponding foreground input Adj. class Occupied width
validators : {
notEmpty : {
message : ' Please fill in the task content! '
}
}
},
tel : {
group : '.col-sm-4',// Corresponding foreground input Adj. class Occupied width
validators : {
notEmpty : {
message : ' Please fill in the telephone number! '
},
phone : {
country : "CN",
message : ' Phone number format error '
}
}
},
area : {
group : '.col-sm-4',// Corresponding foreground input Adj. class Occupied width
validators : {
numeric : {
message : ' Please fill in the numbers! '
}
}
},
endtime : {
group : '.col-sm-4',// Corresponding foreground input Adj. class Occupied width
validators : {
notEmpty : {
message : ' Please select the deadline! '
}
}
},
}
});
}
// Get form validation data
var bootstrapValidator = $("#addTaskForm").data('bootstrapValidator');
// Validation form
bootstrapValidator.validate();
// Judge whether all verifications pass To pass re-validation, pass the execution of the ajax
if (!bootstrapValidator.isValid()) {
return;
}
5. Dynamically load the contents of the drop-down box. Multiple choices and single choices are all 1 sample
function setUser() {
$("#receiver")[0].options.length = 0;
$.ajax({
type : 'POST',
url : $.el.Register.AppUrl + "gzrw/selectUser",
dataType : 'json',
success : function(data) {
$("#receiver")[0].options.add(new Option(' Please select ', ''));
for (var i = 0; i < data.length; i++) {
$("#receiver")[0].options.add(new Option(data[i].name,
data[i].id));
}
// Drop-down box content refresh
$("#receiver").selectpicker('refresh');
},
error : function(e) {
}
});
}
6. Export Events
$("#btnExport").click(function() {
var tableNum = $("#sgnqTable thead tr th").length;
$("#sgnqTable").tableExport({
type : 'excel', // 'csv', 'txt', 'sql', 'json', 'xml', 'excel',
// 'doc', 'png' or 'pdf'
fileName : ' Table name ',
escape : 'false',
ignoreColumn : [ tableNum - 1, tableNum - 4 ],// Columns not exported
});
});
Let's share the time processing of uploading the whole form of bootstrapt-table with you
In js, use $('# addUserForm'). serialize (),//Get all the data in the form and send it to the foreground (controller)
$.ajax({ type : "POST",
url : $.el.Register.AppUrl + "path",
data: $('# addUserForm'). serialize (),//Get all the data in the form
dataType : 'json',
async : false,
success : function(msg) { },
error : function(error) { } });
At this time, if there is a time type in the form, the time type of the foreground (entity) cannot be received because all the time types passed are string types
Solution:
(1) You can add @ DateTimeFormat to the field in the entity entity (pattern = "yyyy-MM-dd")
(2) In controller, use an String to connect this variable (it can't be the same as the field name) and convert it into a time type and reuse it
public String addTask(User user (Entity Object) ,
String dateStr (Used for connection time) )
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date date = sdf.parse(dateStr,pos);
gzrw.setEndtime(date);// Adding time to an entity }