Skip to content

Commit

Permalink
优化弹窗的数据绑定和读取
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Aug 28, 2024
1 parent ede5c57 commit 9379905
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
63 changes: 56 additions & 7 deletions resource/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,57 @@ function editData(id, url, tpl, name) {
}).show(tpl, (id > 0 ? '编辑' : '添加') + name);
}

String.prototype.toHtmlEntities = function () {
return this.replace(/./gm, function (s) {
// return "&#" + s.charCodeAt(0) + ";";
return (s.match(/[a-z0-9\s]+/i)) ? s : '&#' + s.charCodeAt(0) + ';';
});
};

String.prototype.fromHtmlEntities = function () {
return this.replace(/&#\d+;/gm, function (s) {
return String.fromCharCode(s.match(/\d+/gm)[0]);
})
};

function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}


function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]'
}

//绑定数据
function bindData(body, data) {
for (var i in data) {
var field = body.find('[name=' + i + ']')
if (isObject(data[i])) {
for (var k in data[i]) {
var field = body.find('[name="' + i + '[' + k + ']"]')
if (field.attr('type') == 'radio') {
field.each(function () {
if ($(this).val() == data[i][k]) {
$(this).prop('checked', true)
}
})
} else if (field.attr('type') == 'checkbox') {

if (data[i][k] == $(this).val()) {
$(this).prop('checked', true)
}

} else {
field.val(data[i][k]);
}
}
continue;
}
var suffix = ''
if (isArray(data[i])) {
suffix = '[]'
}
var field = body.find('[name="' + i + suffix + '"]')
if (field.attr('type') == 'radio') {
field.each(function () {
if ($(this).val() == data[i]) {
Expand All @@ -85,7 +132,7 @@ function bindData(body, data) {
}
})
} else {
body.find('[name=' + i + ']').val(data[i]);
field.val(data[i]);
}
}
}
Expand All @@ -96,22 +143,24 @@ function getData(body) {
var fields = body.find('[name]');
for (var i = 0; i < fields.length; i++) {
var field = fields.eq(i)
var name = field.attr('name')
if (data[name] != undefined) continue;
if (field.attr('type') == 'radio') {
field.each(function () {
body.find('[name=' + name + ']').each(function () {
if ($(this).prop('checked')) {
data[$(this).attr('name')] = $(this).val();
data[name] = $(this).val();
}
})
} else if (field.attr('type') == 'checkbox') {
var val = []
field.each(function () {
body.find('[name="' + name + '"]').each(function () {
if ($(this).prop('checked')) {
val.push($(this).val());
}
})
data[field.attr('name')] = val
data[name] = val
} else {
data[field.attr('name')] = field.val();
data[name] = field.val();
}
}
return data;
Expand Down
1 change: 1 addition & 0 deletions src/application/admin/view/index/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{block name="body"}

{include file="public/bread" menu="Board" section="主面板" title=""/}

<div id="page-wrapper">
{foreach $notices as $key => $notice}
<div class="alert alert-{$notice.type|default='warning'} alert-dismissible fade show" role="alert">
Expand Down

0 comments on commit 9379905

Please sign in to comment.