关于bootstrap日期转化,bootstrap-editable的简单使用,bootstrap-fileinput的使用

时间:2024-03-07 14:58:23

写本文章的目的是为了记录工作中遇到的问题,方便以后遇到可以迅速解决问题

 

数据库中为datetime类型,.net读取数据已Json格式发回给前台页面:例如:使用bootstrap表格插件Ⅹ

 

 formatter: function (value, row, index) {
                        var date = new Date(parseInt(value.slice(6)));//转化日期格式
                        return date.getFullYear() + \'-\' + parseInt(date.getMonth() + 1) + \'-\' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes()+":"+date.getSeconds();
                    }

 

 


 

使用bootstrap-editable时需要引用

<script src="~/Scripts/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<script src="~/Scripts/bootstraptable/dist/extensions/editable/bootstrap-table-editable.min.js"></script>

初始化时:

 {
                    field: \'SkuPurchasePrice\',
                    title: \'采购价\',
                    align: \'center\',
                    editable: {
                        type: \'text\',
                        title: \'采购价\',
                        validate: function (v) {
                            if (v < 0) return \'采购价不能小于0\';

                        }
                    }
                }, {
                    field: \'QtyAvailable\',
                    title: \'库存可用量\',
                    align: \'center\',
                    editable: {
                        type: \'text\',
                        title: \'库存可用量\',
                        validate: function (v) {
                            if (v < 0) return \'库存可用量不能小于0\';
                        },                     
                    },                    
                }

修改完成的点击事件:

 $("#tb_detailList").bootstrapTable({
                url: actionUrl,         //请求后台的URL(*)
                method: \'get\',                      //请求方式(*)
                toolbar: \'#toolbar\',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
                pagination: true,                   //是否显示分页(*)
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: dbQueryParams,//传递参数(*)
                sidePagination: "server",           //分页方式:client客户端分页,server服务端分页(*)
                pageNumber: 1,                       //初始化加载第一页,默认第一页
                pageSize: 10,                       //每页的记录行数(*)
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数(*)
                search: false,                       //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                //minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: false,                //是否启用点击选中行
                //height: 800,                        //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识,一般为主键列
                showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: tbColumns,              
                onPageChange: function (number, size) {
                    m_pagerow = (number - 1) * size;
                },
                onLoadSuccess: function () {  //加载成功时执行
                    $("[data-toggle=\'popover\']").popover();
                },
                onEditableSave: function (field, row, oldValue, $el) {//重点在这里的点击事件
                    $.ajax({
                        type: \'post\',
                        url: "@Url.Action("EditNumber")",//传给后台的地址进行数据的修改
                        dataType: \'JSON\',
                        data: { "row": JSON.stringify(row) },
                        success: function (data) {
                            if (data.ResultType == 0) {
                                toastr.success(data.Message);
                            }
                            else {
                                toastr.warning(data.Message);
                            }
                        },
                        error: function () {
                            toastr.error("出错了,请联系管理员");
                        }
                    });
                }
            });

后台修改数据的方法:

 public ActionResult EditNumber()
        {
            var row = Request.Params["row"];
            var model=JsonConvert.DeserializeObject<View_DetailSkuPriceStockVO>(row);
            var result = offerServer.View_DetailSkuPriceStock.editNumber(model);
            result.Message = result.Message ?? result.ResultType.GetName();
            return Json(result);
        }

 


 


 

bootstrap-fileinput使用:

引用

 <script src="~/Scripts/AdminLTE2.3.0/bootstrap/js/fileinput.min.js"></script>

初始化:

 <label class="control-label col-sm-1">商品上传</label>
                            <div class="col-sm-2"><input type="file" id="ProductsUpload" name="ProductsUpload" /></div>//这里应用了上传插件input
                            <div class="col-sm-1"><a href="~/ProductsExcel/DownloadTemplate?type=5" target="_blank">下载模板</a></div>
 function initFileInput(ctrlName) {
            var control = $(\'#\' + ctrlName);
            control.fileinput({
                language: \'zh\', //语言
                uploadUrl: "@Url.Action("ExcelLeadingIn")", //action
                autoReplace: true,
                maxFileCount: 1, //最大上传数量
                allowedFileExtensions: ["xlsx", "xls"],
                browseClass: "btn btn-primary", //按钮样式
                dropZoneEnabled: false,
                enctype: \'multipart/form-data\',
                showRemove: true, //是否显示删除按钮
                showBrowse: true, //显示浏览图片按钮
                msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
                msgInvalidFileExtension:"只支持.xlsx和.xls文件的格式上传",
                fileActionSettings: {
                    showUpload: false, //预览图片上传按钮
                    showRemove: false, //预览图片remove按钮
                    showZoom: false //关闭预览图片按钮
                }
            }).on(\'fileuploaded\', function (event, data, previewId, index) {//上传图触发事件
                if (data.response.state == 1) {
                    $(\'#excelLendingDiv h5\').html($(\'#excelLendingDiv h5\').html() + data.response.result+"<br/>");
                }
                else {
                    toastr.error(data.response.result);
                }
            }).on(\'filecleared\', function (event) {//remove触发事件
                control.show();
            });
        }

后台上传接受的方法:

 [HttpPost]
        public ActionResult ExcelLeadingIn()
        {         
            var files = Request.Files;
            string name=files.Keys[0].ToString();

            if (files != null && files.Count > 0)
            {              
                var file = files[0];                
                string path = Server.MapPath("~/Content/excel/") + file.FileName;//文件保存在当前域名下的Content/excel/中
                file.SaveAs(path);              
                string mess = OfferServer.ProductsDetail.InsertExcel(path,file.InputStream,name);
                return Json(new { state = 1, result = mess });
            }
            return Json(new { state = 0, result = "上传发生错误,请检查后重试" });
        }