table-select.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>table下拉选择器</title>
  6. <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
  7. <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
  8. </head>
  9. <body>
  10. <div class="layuimini-container">
  11. <div class="layuimini-main">
  12. <blockquote class="layui-elem-quote">
  13. table下拉选择器使用开源项目:TableSelect<br>
  14. 在开源的基础上修改支持多条件搜索、宽以及高的自定义<br>
  15. <a href="https://gitee.com/lolicode/layui_component_tableselect" target="_blank" class="layui-btn layui-btn-danger">TableSelect</a>
  16. </blockquote>
  17. <form class="layui-form" action="" style="padding:20px;">
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">多选</label>
  20. <div class="layui-input-inline">
  21. <input type="text" name="" placeholder="请输入" autocomplete="off" class="layui-input" id="demo" value="刘晓军,张恒" ts-selected="002,003">
  22. </div>
  23. <div class="layui-form-mid layui-word-aux">本地演示数据,分页选中和其他页一样,这不是BUG</div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label class="layui-form-label">单选</label>
  27. <div class="layui-input-inline">
  28. <input type="text" name="" placeholder="请输入" autocomplete="off" class="layui-input" id="demo2">
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">多搜索条件</label>
  33. <div class="layui-input-inline">
  34. <input type="text" name="" placeholder="请输入" autocomplete="off" class="layui-input" id="demo3">
  35. </div>
  36. </div>
  37. </form>
  38. <pre class="layui-code">
  39. //开始使用
  40. var tableSelect = layui.tableSelect;
  41. tableSelect.render({
  42. elem: '#demo', //定义输入框input对象 必填
  43. checkedKey: 'id', //表格的唯一建值,非常重要,影响到选中状态 必填
  44. searchKey: 'keyword', //搜索输入框的name值 默认keyword
  45. searchPlaceholder: '关键词搜索', //搜索输入框的提示文字 默认关键词搜索
  46. height:'400', //自定义高度
  47. width:'900', //自定义宽度
  48. table: { //定义表格参数,与LAYUI的TABLE模块一致,只是无需再定义表格elem
  49. url:'',
  50. cols: [
  51. [
  52. ]
  53. ]
  54. },
  55. done: function (elem, data) {
  56. //选择完后的回调,包含2个返回值 elem:返回之前input对象;data:表格返回的选中的数据 []
  57. //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
  58. }
  59. })
  60. //默认值
  61. 只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
  62. //多条件搜索
  63. tableSelect.render({
  64. elem: '#demo3',
  65. checkedKey: 'id',
  66. searchType: 'more', //开启多条件搜索
  67. searchList: [
  68. {searchKey: 'name', searchPlaceholder: '搜索用户姓名'}, //搜索条件1
  69. {searchKey: 'sex', searchPlaceholder: '搜索用户性别'}, //搜索条件2
  70. ],
  71. table: {
  72. url: '../api/tableSelect.json',
  73. cols: [
  74. [
  75. { type: 'radio' },
  76. { field: 'id', title: 'ID' },
  77. { field: 'username', title: '姓名' },
  78. { field: 'sex', title: '性别' }
  79. ]
  80. ]
  81. },
  82. done: function (elem, data) {
  83. //选择完后的回调,包含2个返回值 elem:返回之前input对象;data:表格返回的选中的数据 []
  84. //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
  85. }
  86. })
  87. </pre>
  88. </div>
  89. </div>
  90. <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
  91. <script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
  92. <script>
  93. layui.use(['table', 'form', 'tableSelect'], function () {
  94. var $ = layui.jquery,
  95. table = layui.table,
  96. form = layui.form,
  97. tableSelect = layui.tableSelect;
  98. tableSelect.render({
  99. elem: '#demo',
  100. searchKey: 'my',
  101. checkedKey: 'id',
  102. searchPlaceholder: '自定义文字和name',
  103. table: {
  104. url: '../api/tableSelect.json',
  105. cols: [
  106. [
  107. {type: 'checkbox'},
  108. {field: 'id', title: 'ID', width: 100},
  109. {field: 'username', title: '姓名', width: 300},
  110. {field: 'sex', title: '性别', width: 100}
  111. ]
  112. ]
  113. },
  114. done: function (elem, data) {
  115. var NEWJSON = []
  116. layui.each(data.data, function (index, item) {
  117. NEWJSON.push(item.username)
  118. })
  119. elem.val(NEWJSON.join(","))
  120. }
  121. })
  122. tableSelect.render({
  123. elem: '#demo2',
  124. checkedKey: 'id',
  125. table: {
  126. url: '../api/tableSelect.json',
  127. cols: [
  128. [
  129. {type: 'radio'},
  130. {field: 'id', title: 'ID'},
  131. {field: 'username', title: '姓名'},
  132. {field: 'sex', title: '性别'}
  133. ]
  134. ]
  135. },
  136. done: function (elem, data) {
  137. var NEWJSON = []
  138. layui.each(data.data, function (index, item) {
  139. NEWJSON.push(item.username)
  140. })
  141. elem.val(NEWJSON.join(","))
  142. }
  143. })
  144. tableSelect.render({
  145. elem: '#demo3',
  146. checkedKey: 'id',
  147. height:'400',
  148. width:'900',
  149. searchType: 'more',
  150. searchList: [
  151. {searchKey: 'name', searchPlaceholder: '搜索用户姓名'},
  152. {searchKey: 'sex', searchPlaceholder: '搜索用户性别'},
  153. ],
  154. table: {
  155. url: '../api/tableSelect.json',
  156. cols: [
  157. [
  158. {type: 'radio'},
  159. {field: 'id', title: 'ID'},
  160. {field: 'username', title: '姓名'},
  161. {field: 'sex', title: '性别'}
  162. ]
  163. ]
  164. },
  165. done: function (elem, data) {
  166. var NEWJSON = []
  167. layui.each(data.data, function (index, item) {
  168. NEWJSON.push(item.username)
  169. })
  170. elem.val(NEWJSON.join(","))
  171. }
  172. })
  173. });
  174. </script>
  175. </body>
  176. </html>