我用的是easy-admin 后台,thinkphp6+layui ,上传功能是已经封装好的,我做了一点点小的改动实现了上传后拖拽排序功能
html代码部分:
<style>
.pic{ border: 1px solid #e2e2e2;
background-position: center;
float: left;
padding: 5px 0 5px 0;
padding-left: 10px;
padding-right: 10px;
margin-left: 8px;
margin-top: 15px;
}
.laypic1{
min-height: 36px;
margin-top: 10px;
}
</style>
<div class="layui-form-item">
<label class="layui-form-label required">购买须知</label>
<div class="layui-input-block layuimini-upload">
<input name="purchase" class="layui-input layui-col-xs6" placeholder="请上传购买须知" value="{$row.purchase}" id="picture-purchase">
<div class="layuimini-upload-btn">
<span><a class="layui-btn layui-btn-sm" data-upload="purchase" data-upload-number="more" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
<span><a class="layui-btn layui-btn-normal layui-btn-sm" id="select_purchase" data-upload-select="purchase" data-upload-number="more" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span>
</div>
</div>
</div>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="__STATIC__/admin/js/jquery-1.12.4.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
图片上传后通过如下放在了class名为layuimini-upload-btn div的后面
放图的部分节选,下面js有完整的
js部分代码:
sorts:function(uploadName){
var sort = new Array();
$('#laypic1-'+uploadName).find('.pic').each(function(index,element){
sort[index] = element.id;
//$(".oid"+element.id).html(index+1);
});
$("#picture-"+uploadName).val(sort.join('|'));
},
upload: function () {
var uploadList = document.querySelectorAll("[data-upload]");
var uploadSelectList = document.querySelectorAll("[data-upload-select]");
if (uploadList.length > 0) {
$.each(uploadList, function (i, v) {
var exts = $(this).attr('data-upload-exts'),
uploadName = $(this).attr('data-upload'),
uploadNumber = $(this).attr('data-upload-number'),
uploadSign = $(this).attr('data-upload-sign');
exts = exts || init.upload_exts;
uploadNumber = uploadNumber || 'one';
uploadSign = uploadSign || '|';
var elem = "input[name='" + uploadName + "']",
uploadElem = this;
// 监听上传事件
upload.render({
elem: this,
url: admin.url(init.upload_url),
accept: 'file',
exts: exts,
// 让多图上传模式下支持多选操作
multiple: (uploadNumber !== 'one') ? true : false,
done: function (res) {
if (res.code === 1) {
var url = res.data.url;
if (uploadNumber !== 'one') {
var oldUrl = $(elem).val();
if (oldUrl !== '') {
url = oldUrl + uploadSign + url;
}
}
$(elem).val(url);
$(elem).trigger("input");
admin.msg.success(res.msg);
} else {
admin.msg.error(res.msg);
}
return false;
}
});
// 监听上传input值变化
$(elem).bind("input propertychange", function (event) {
var urlString = $(this).val(),
urlArray = urlString.split(uploadSign),
uploadIcon = $(uploadElem).attr('data-upload-icon');
uploadIcon = uploadIcon || "file";
// console.log(uploadIcon)
$('#bing-' + uploadName).remove();
$('#laypic1-' + uploadName).remove();
if (urlString.length > 0) {
var parant = $(this).parent('div');
var liHtml = '';
$.each(urlArray, function (i, v) {
if(uploadIcon=="video"){
liHtml+=' <li><video src="' + v + '" class="avatar" controls="controls" style="width: 150px;height: 150px;">您的浏览器不支持视频播放</video><small class="uploads-delete-tip bg-red badge" data-upload-delete="' + uploadName + '" data-upload-url="' + v + '" data-upload-sign="' + uploadSign + '">×</small></li>\n';
}else{
// liHtml += '<li class="pic" id="'+i+'"><a><img src="' + v + '" data-image onerror="this.src=\'' + BASE_URL + 'admin/images/upload-icons/' + uploadIcon + '.png\';this.onerror=null"></a><small class="uploads-delete-tip bg-red badge" data-upload-delete="' + uploadName + '" data-upload-url="' + v + '" data-upload-sign="' + uploadSign + '">×</small></li>\n';
liHtml +='<div class="pic" id="' + v + '"><img class="show-img" src="' + v + '" onerror="this.src=\'' + BASE_URL + 'admin/images/upload-icons/' + uploadIcon + '.png\';this.onerror=null"><small class="uploads-delete-tip bg-red badge" style="margin-top: -44px;margin-left: -23px;" data-upload-delete="' + uploadName + '" data-upload-url="' + v + '" data-upload-sign="' + uploadSign + '">×</small></div>';
}
});
parant.after('<div id="laypic1-' + uploadName + '" class="laypic1">\n' + liHtml + '</div>');
// $("#laypic1-"+ uploadName).html(liHtml);
// parant.after('<ul id="bing-' + uploadName + '" class="layui-input-block layuimini-upload-show">\n' + liHtml + '</ul>');
}
$('#laypic1-' + uploadName).sortable().bind('sortupdate',function () {
var sort = $('#laypic1-' + uploadName).sortable("toArray");
admin.sorts(uploadName);
});
});
// 非空初始化图片显示
if ($(elem).val() !== '') {
$(elem).trigger("input");
}
});
// 监听上传文件的删除事件
$('body').on('click', '[data-upload-delete]', function () {
var uploadName = $(this).attr('data-upload-delete'),
deleteUrl = $(this).attr('data-upload-url'),
sign = $(this).attr('data-upload-sign');
var confirm = admin.msg.confirm('确定删除?', function () {
var elem = "input[name='" + uploadName + "']";
var currentUrl = $(elem).val();
var url = '';
if (currentUrl !== deleteUrl) {
url = currentUrl.search(deleteUrl) === 0 ? currentUrl.replace(deleteUrl + sign, '') : currentUrl.replace(sign + deleteUrl, '');
$(elem).val(url);
$(elem).trigger("input");
} else {
$(elem).val(url);
$('#bing-' + uploadName).remove();
$('#laypic1-' + uploadName).remove();
}
$.ajax({
url:admin.url(init.del_url),
type: "get",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
data: {url:deleteUrl},
timeout: 60000,
success: function (res) {
},
error: function (xhr, textstatus, thrown) {
admin.msg.error('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!', function () {
});
return false;
}
});
admin.msg.close(confirm);
});
return false;
});
}
if (uploadSelectList.length > 0) {
$.each(uploadSelectList, function (i, v) {
var exts = $(this).attr('data-upload-exts'),
uploadName = $(this).attr('data-upload-select'),
uploadNumber = $(this).attr('data-upload-number'),
uploadSign = $(this).attr('data-upload-sign');
exts = exts || init.upload_exts;
uploadNumber = uploadNumber || 'one';
uploadSign = uploadSign || '|';
var selectCheck = uploadNumber === 'one' ? 'radio' : 'checkbox';
var elem = "input[name='" + uploadName + "']",
uploadElem = $(this).attr('id');
tableSelect.render({
elem: "#" + uploadElem,
checkedKey: 'id',
searchType: 'more',
searchList: [
{searchKey: 'title', searchPlaceholder: '请输入文件名'},
],
table: {
url: admin.url('ajax/getUploadFiles'),
cols: [[
{type: selectCheck},
{field: 'id', title: 'ID'},
{field: 'url', minWidth: 80, search: false, title: '图片信息', imageHeight: 40, align: "center", templet: admin.table.image},
{field: 'original_name', width: 150, title: '文件原名', align: "center"},
{field: 'mime_type', width: 120, title: 'mime类型', align: "center"},
{field: 'create_time', width: 200, title: '创建时间', align: "center", search: 'range'},
]]
},
done: function (e, data) {
var urlArray = [];
$.each(data.data, function (index, val) {
urlArray.push(val.url)
});
var url = urlArray.join(uploadSign);
admin.msg.success('选择成功', function () {
$(elem).val(url);
$(elem).trigger("input");
});
}
})
});
}
},
拖拽的部分单独截图拿出来,开始
结束
如果你是放在其它后台下用的也是layui 那么功能自行实现,不喜勿喷!!!本文只是记录项目中遇到的问题及最后解决的办法