notice_select.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {extend name="app/shop/view/base.html"/}
  2. {block name="resources"}
  3. <style>
  4. .notice-list {
  5. padding: 0 20px;
  6. }
  7. </style>
  8. {/block}
  9. {block name="body"}
  10. <div class="notice-list">
  11. <!-- 列表 -->
  12. <table id="notice_list" lay-filter="notice_list"></table>
  13. </div>
  14. {/block}
  15. {block name="script"}
  16. <script type="text/html" id="checkbox">
  17. {{# if($.inArray(d.id.toString(), selected_id_arr) != -1){ }}
  18. <input type="checkbox" data-notice-id="{{d.id}}" name="notice_checkbox" lay-skin="primary" lay-filter="notice_checkbox" checked>
  19. {{# }else{ }}
  20. <input type="checkbox" data-notice-id="{{d.id}}" name="notice_checkbox" lay-skin="primary" lay-filter="notice_checkbox">
  21. {{# } }}
  22. <input type="hidden" data-notice-id="{{d.id}}" name="notice_json" value='{{ JSON.stringify(d) }}' />
  23. </script>
  24. <script>
  25. var table, form, laytpl,
  26. select_id = "{$select_id}", //选中商品id
  27. selected_id_arr = select_id.length ? select_id.split(',') : [],
  28. select_list = [], //选中商品所有数据
  29. goodsIdArr = selected_id_arr;
  30. $(function () {
  31. layui.use(['form', 'laytpl'], function () {
  32. form = layui.form;
  33. laytpl = layui.laytpl;
  34. table = new Table({
  35. elem: '#notice_list',
  36. url: ns.url("shop/notice/index"),
  37. cols: [
  38. [
  39. {
  40. unresize: 'false',
  41. width: '10%',
  42. templet: '#checkbox'
  43. }, {
  44. width: '55%',
  45. title: '公告标题',
  46. unresize: 'false',
  47. templet: function(data) {
  48. var html = data.is_top ? '<span class="required">[ 置顶 ] </span>' : '';
  49. html += data.title;
  50. return html;
  51. }
  52. }, {
  53. width: '35%',
  54. title: '创建时间',
  55. unresize: 'false',
  56. templet: function(data) {
  57. return ns.time_to_date(data.create_time);
  58. }
  59. }
  60. ]
  61. ],
  62. callback : function () {
  63. // 更新商品复选框状态
  64. for (var i=0;i<goodsIdArr.length;i++) {
  65. var selected_notices = $("input[name='notice_checkbox'][data-notice-id='" + goodsIdArr[i] + "']");
  66. if (selected_notices.length) {
  67. $("input[name='notice_checkbox'][data-notice-id='" + goodsIdArr[i] + "']").prop("checked", true);
  68. }
  69. }
  70. form.render();
  71. initData();
  72. }
  73. });
  74. // 勾选商品
  75. form.on('checkbox(notice_checkbox)', function(data) {
  76. var notice_id = $(data.elem).attr("data-notice-id"), json = {};
  77. form.render();
  78. var noticeLen = $("input[name='notice_checkbox'][data-notice-id="+ notice_id +"]:checked").length;
  79. if (noticeLen){
  80. json = JSON.parse($("input[name='notice_json'][data-notice-id="+ notice_id +"]").val());
  81. delete json.LAY_INDEX;
  82. delete json.LAY_TABLE_INDEX;
  83. delete json.create_time;
  84. select_list.push(json);
  85. goodsIdArr.push(notice_id);
  86. } else{
  87. for (var i = 0; i < select_list.length; i++) {
  88. if (select_list[i].id == notice_id) {
  89. select_list.splice(i, 1);
  90. }
  91. }
  92. goodsIdArr.remove(notice_id);
  93. }
  94. $.unique(select_list);
  95. });
  96. //初始化数据
  97. function initData(){
  98. var noticeLen = $("input[name='notice_checkbox'][data-notice-id]:checked").length;
  99. for (var i = 0; i < noticeLen; i++){
  100. var noticeId = $("input[name='notice_checkbox'][data-notice-id]:checked").eq(i).attr("data-notice-id");
  101. var ident = false;
  102. for (var k = 0; k < select_list.length; k++){
  103. if(select_list[k].id == noticeId){
  104. ident = true;
  105. break;
  106. }
  107. }
  108. if (ident) return;
  109. json = JSON.parse($("input[name='notice_json'][data-notice-id="+ noticeId +"]").val());
  110. delete json.LAY_INDEX;
  111. delete json.LAY_TABLE_INDEX;
  112. delete json.create_time;
  113. select_list.push(json);
  114. }
  115. }
  116. });
  117. });
  118. function selectNotice(callback) {
  119. var res = select_list;
  120. callback(res);
  121. }
  122. Array.prototype.indexOf = function(val) {
  123. for (var i = 0; i < this.length; i++) {
  124. if (this[i].id){
  125. if (this[i].id == parseInt(val)) return i;
  126. } else {
  127. if (this[i] == val) return i;
  128. }
  129. }
  130. return -1;
  131. };
  132. Array.prototype.remove = function(val) {
  133. var index = this.indexOf(val);
  134. if (index > -1) {
  135. this.splice(index, 1);
  136. }
  137. };
  138. select_list.__proto__ = Array.prototype;
  139. function removeDuplicates(arr){
  140. if (!Array.isArray(arr)) {
  141. console.log('type error!');
  142. return
  143. }
  144. var array = [];
  145. for (var i = 0; i < arr.length; i++) {
  146. if (array.indexOf(arr[i]) === -1) {
  147. array.push(arr[i])
  148. }
  149. }
  150. return array;
  151. }
  152. </script>
  153. {/block}