0
0

screentemp.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <style>
  2. </style>
  3. <div id="{$name}">
  4. <input type="hidden" id="ele-{$name}" name="{$name}" :value="value">
  5. <table class="table table-bordered">
  6. <thead>
  7. <tr>
  8. <th>序号</th>
  9. <th>绑定模块</th>
  10. <th>预览模块</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <tr v-for="(item,index) in contents" :key="index">
  15. <th>{{item.id}}</th>
  16. <th>
  17. <select class="form-control" v-model="item.mid" @change="changeModule">
  18. <option value="0">选择模块</option>
  19. <option v-for="(item2,index2) in options" :key="index2" :value="item2.id">{{item2.title}}</option>
  20. </select>
  21. </th>
  22. <th>
  23. <img v-if="item.img" :src="item.img" style="width: 40px;height: 40px;" onclick="open_img(this)" alt="">
  24. </th>
  25. </tr>
  26. </tbody>
  27. </table>
  28. <a href="javascript:;" @click="addBtn" class="btn btn-primary btn-sm">添加行</a>
  29. <a href="javascript:;" v-if="contents.length > 1" @click="delBtn" class="btn btn-danger btn-sm">删除行</a>
  30. </div>
  31. <script>
  32. new Vue({
  33. el: '#{$name}',
  34. data: function() {
  35. return {
  36. options: {:json_encode($options)},
  37. contents: [],
  38. value: ''
  39. }
  40. },
  41. computed: {
  42. scontent() {
  43. const vals = JSON.parse(JSON.stringify(this.contents));
  44. return vals;
  45. },
  46. },
  47. watch: {
  48. scontent: function (newVal, oldVal) {
  49. this.formatContent();
  50. const vv = [];
  51. this.contents.forEach((item) => {
  52. vv.push({
  53. id: item.id,
  54. mid: item.mid,
  55. })
  56. });
  57. this.value = vv.length > 0 ? JSON.stringify(vv):'';
  58. $('#ele-{$name}').val(this.value);
  59. }
  60. },
  61. created(){
  62. const vals = "{$val}";
  63. this.value = vals;
  64. const valss = vals?JSON.parse(vals):[];
  65. const cc = [];
  66. valss.forEach((item) => {
  67. let img = '';
  68. this.options.forEach((item2) => {
  69. if(item.mid == item2.id){
  70. img = item2.img;
  71. }
  72. });
  73. cc.push({
  74. id: item.id,
  75. mid: item.mid,
  76. img: img,
  77. })
  78. });
  79. if(cc.length == 0){
  80. cc.push({
  81. id: 1,
  82. mid: 0,
  83. img: '',
  84. })
  85. }
  86. this.contents = cc;
  87. console.log('11111',this.value,this.options, this.contents);
  88. },
  89. methods:{
  90. addBtn(){
  91. const newId = this.getMaxId() + 1;
  92. this.contents.push({
  93. id: newId,
  94. mid: 0,
  95. img: '',
  96. })
  97. },
  98. // 获取当前最大的id值
  99. getMaxId(){
  100. let id = 0;
  101. this.contents.forEach((item) => {
  102. if(item.id > id){
  103. id = item.id;
  104. }
  105. });
  106. return id;
  107. },
  108. delBtn(){
  109. console.log('contents',this.contents);
  110. const maxId = this.getMaxId();
  111. if(maxId > 1){
  112. const cc = this.contents.filter((item) => {
  113. return item.id != maxId;
  114. });
  115. this.contents = cc;
  116. }
  117. },
  118. changeModule(){
  119. console.log('contents',this.contents);
  120. },
  121. formatContent(){
  122. this.contents.forEach((item) => {
  123. const info = item;
  124. this.options.forEach((item2) => {
  125. if(item2.id == item.mid){
  126. info.img = item2.img;
  127. }
  128. });
  129. })
  130. },
  131. }
  132. })
  133. </script>