table-select.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>table下拉选择器</title>
  6. <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
  7. <link rel="stylesheet" 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. done: function (elem, data) {
  53. //选择完后的回调,包含2个返回值 elem:返回之前input对象;data:表格返回的选中的数据 []
  54. //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
  55. }
  56. })
  57. //默认值
  58. 只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
  59. //多条件搜索
  60. tableSelect.render({
  61. elem: '#demo3',
  62. checkedKey: 'id',
  63. searchType: 'more', //开启多条件搜索
  64. searchList: [
  65. {searchKey: 'name', searchPlaceholder: '搜索用户姓名'}, //搜索条件1
  66. {searchKey: 'sex', searchPlaceholder: '搜索用户性别'}, //搜索条件2
  67. ],
  68. table: {
  69. url: '../api/tableSelect.json',
  70. cols: [[
  71. { type: 'radio' },
  72. { field: 'id', title: 'ID' },
  73. { field: 'username', title: '姓名' },
  74. { field: 'sex', title: '性别' }
  75. ]]
  76. },
  77. done: function (elem, data) {
  78. //选择完后的回调,包含2个返回值 elem:返回之前input对象;data:表格返回的选中的数据 []
  79. //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
  80. }
  81. })
  82. </pre>
  83. </div>
  84. </div>
  85. <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
  86. <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
  87. <script>
  88. layui.use(['table', 'form', 'tableSelect'], function () {
  89. var $ = layui.jquery,
  90. table = layui.table,
  91. form = layui.form,
  92. tableSelect = layui.tableSelect;
  93. tableSelect.render({
  94. elem: '#demo',
  95. searchKey: 'my',
  96. checkedKey: 'id',
  97. searchPlaceholder: '自定义文字和name',
  98. table: {
  99. url: '../api/tableSelect.json',
  100. cols: [[
  101. { type: 'checkbox' },
  102. { field: 'id', title: 'ID', width: 100 },
  103. { field: 'username', title: '姓名', width: 300 },
  104. { field: 'sex', title: '性别', width: 100 }
  105. ]]
  106. },
  107. done: function (elem, data) {
  108. var NEWJSON = []
  109. layui.each(data.data, function (index, item) {
  110. NEWJSON.push(item.username)
  111. })
  112. elem.val(NEWJSON.join(","))
  113. }
  114. })
  115. tableSelect.render({
  116. elem: '#demo2',
  117. checkedKey: 'id',
  118. table: {
  119. url: '../api/tableSelect.json',
  120. cols: [[
  121. { type: 'radio' },
  122. { field: 'id', title: 'ID' },
  123. { field: 'username', title: '姓名' },
  124. { field: 'sex', title: '性别' }
  125. ]]
  126. },
  127. done: function (elem, data) {
  128. var NEWJSON = []
  129. layui.each(data.data, function (index, item) {
  130. NEWJSON.push(item.username)
  131. })
  132. elem.val(NEWJSON.join(","))
  133. }
  134. })
  135. tableSelect.render({
  136. elem: '#demo3',
  137. checkedKey: 'id',
  138. height:'400',
  139. width:'900',
  140. searchType: 'more',
  141. searchList: [
  142. {searchKey: 'name', searchPlaceholder: '搜索用户姓名'},
  143. {searchKey: 'sex', searchPlaceholder: '搜索用户性别'},
  144. ],
  145. table: {
  146. url: '../api/tableSelect.json',
  147. cols: [[
  148. { type: 'radio' },
  149. { field: 'id', title: 'ID' },
  150. { field: 'username', title: '姓名' },
  151. { field: 'sex', title: '性别' }
  152. ]]
  153. },
  154. done: function (elem, data) {
  155. var NEWJSON = []
  156. layui.each(data.data, function (index, item) {
  157. NEWJSON.push(item.username)
  158. })
  159. elem.val(NEWJSON.join(","))
  160. }
  161. })
  162. });
  163. </script>
  164. </body>
  165. </html>