goods-edit.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. $(document).ready(function() {
  2. initFormData2();
  3. bindPictureClick();
  4. })
  5. /**
  6. * 渲染&回显商品类型
  7. * @param {Object} goodsType 类型
  8. */
  9. function initSelectData(goodsType) {
  10. //加载商品类型
  11. var res = getData('/goods/type');
  12. $.each(res.data, function(index, item) {
  13. $('#goodsType').append(new Option(item.type, item.id)); // 下拉菜单里添加元素
  14. });
  15. $("#goodsType").each(function() {
  16. // this代表的是<option></option>,对option再进行遍历
  17. $(this).children("option").each(function() {
  18. // 判断需要对那个选项进行回显,这里用值比较
  19. if ($(this).text() == goodsType) {
  20. // 进行回显
  21. $(this).attr("selected", "selected");
  22. }
  23. });
  24. })
  25. layui.form.render("select");
  26. }
  27. /**
  28. * 表单数据回显,可以用Ajax请求,成功后回调回显
  29. */
  30. function initFormData2() {
  31. layui.use(['form'], function() {
  32. var form = layui.form;
  33. var goods = layui.sessionData('goods-update')
  34. goods = goods.goods;
  35. form.val("goods-form", {
  36. "goodsName": goods.goodsName,
  37. "description": goods.description,
  38. "cost": goods.cost,
  39. "id": goods.id,
  40. "ingredients": goods.ingredients,
  41. "isMeat": goods.isMeat,
  42. "price": goods.price,
  43. "stock": goods.stock,
  44. "taste": goods.taste,
  45. "weight": goods.weight
  46. })
  47. initSelectData(goods.goodsType);
  48. initPictures(goods.id);
  49. });
  50. }
  51. /**
  52. * 回显商品图片
  53. * @param {Object} goodsId 商品id
  54. */
  55. function initPictures(goodsId) {
  56. var e = getData('/goods/picture/get', {
  57. goodsId: goodsId
  58. });
  59. var pictures = e.data;
  60. pictures.forEach(picture => {
  61. var img = new Image();
  62. img.src = getServerIPPrefix()+picture.picUrl;
  63. // $(img).addClass('prePicture')
  64. // img.id=picture.id
  65. $("#goodsPictures").append(img)
  66. })
  67. //渲染轮播图
  68. // layui.use('carousel', function() {
  69. // var carousel = layui.carousel;
  70. // //建造实例
  71. // carousel.render({
  72. // elem: '#picCarousel',
  73. // width: '150px', //设置容器宽度
  74. // height: '150px',
  75. // arrow: 'hover' ,//始终显示箭头
  76. // indicator:'none'
  77. // });
  78. // });
  79. renderCarousel('#picCarousel');
  80. }
  81. function renderCarousel(id){
  82. //渲染轮播图
  83. layui.use('carousel', function() {
  84. var carousel = layui.carousel;
  85. //建造实例
  86. carousel.render({
  87. elem: id,
  88. width: '150px', //设置容器宽度
  89. height: '150px',
  90. arrow: 'hover' ,//始终显示箭头
  91. indicator:'none'
  92. });
  93. });
  94. }
  95. function bindPictureClick(){
  96. $("#goodsPictures").find("img").click(function(){
  97. console.log($(this).attr('id'))
  98. })
  99. }
  100. function initFormData() {
  101. var goods = layui.sessionData('goods-update')
  102. goods = goods.goods;
  103. $("#goodsName").val(goods.goodsName)
  104. $("#goodsType").val(goods.goodsType)
  105. $("#description").val(goods.description)
  106. $("#cost").val(goods.cost)
  107. $("#id").val(goods.id)
  108. $("#ingredients").val(goods.ingredients)
  109. $("#isMeat").val(goods.isMeat)
  110. $("#price").val(goods.price)
  111. $("#stock").val(goods.stock)
  112. $("#taste").val(goods.taste)
  113. $("#weight").val(goods.weight)
  114. // $("#goodsType option").each(function(){
  115. // var value=$(this).attr("value")
  116. // // console.log(goods.goodsType==value)
  117. // if(goods.goodsType==value){
  118. // $(this).attr("selected")
  119. // }
  120. // })
  121. }