SQLTable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. var SQL_Object = null;
  2. function init_SQLTable(obj) {
  3. var qry = $(obj).attr('data-query');
  4. if (qry==='') qry = $(obj).attr('data-query-default');
  5. SQL_Object = {
  6. module: $(obj).attr('data-module'),
  7. sql: $(obj).attr('data-sql'),
  8. where: $(obj).attr('data-where'),
  9. query: qry,
  10. order_field: $(obj).attr('data-order-field'),
  11. order_dir: $(obj).attr('data-order-dir'),
  12. pager_limit: $(obj).attr('data-pager-limit'),
  13. actual_page: $(obj).attr('data-active-page'),
  14. template: $(obj).attr('data-template'),
  15. on_delete: $(obj).attr('data-on-delete'),
  16. id_field: $(obj).attr('data-id-field'),
  17. search: $(obj).attr('data-search'),
  18. keywords: $(obj).attr('data-keywords'),
  19. table_name: $(obj).attr('id')
  20. };
  21. $(obj).attr('data-action','');
  22. $(obj).attr('data-id','');
  23. query_builder(SQL_Object,obj);
  24. }
  25. function query_builder(sql_obj,obj) {
  26. var table_body = $(obj).children('tbody');
  27. $.getJSON(sql_obj.module+'list/',sql_obj,function(response) {
  28. $(table_body).html(response.list);
  29. $('.datepicker').datepicker({
  30. autoclose: true
  31. });
  32. $(".select2").select2();
  33. $('.sql-paginator').html(response.paginator);
  34. });
  35. }
  36. function paginator_load_page(page,table) {
  37. var table_obj = $(table);
  38. $(table_obj).attr('data-active-page',page);
  39. init_SQLTable(table_obj);
  40. }
  41. function reorder_SQLTable(field,obj) {
  42. var order = $(obj).parent('th').parent('tr').parent('thead').parent('table').attr('data-order-dir');
  43. $(obj).parent('th').parent('tr').children('th').children('a').removeClass('active-order');
  44. $(obj).addClass('active-order');
  45. if (field!=='' && order!=='') {
  46. var new_order = '';
  47. var table_obj = $(obj).parent('th').parent('tr').parent('thead').parent('table');
  48. if (order==='ASC') {
  49. new_order = 'DESC';
  50. $(obj).children('i').removeClass('fa-sort-asc');
  51. $(obj).children('i').addClass('fa-sort-desc');
  52. }
  53. else {
  54. new_order = 'ASC';
  55. $(obj).children('i').removeClass('fa-sort-desc');
  56. $(obj).children('i').addClass('fa-sort-asc');
  57. }
  58. $(table_obj).attr('data-order-field',field);
  59. $(table_obj).attr('data-order-dir',new_order);
  60. init_SQLTable(table_obj);
  61. }
  62. else {
  63. return false;
  64. }
  65. }
  66. function quicksearch_SQLTable(obj,table) {
  67. var keyword = $(obj).parent('div').children('input').val();
  68. var search_field = $(obj).attr('data-search-field');
  69. var table_obj = $(table);
  70. $(table_obj).attr('data-keywords',keyword);
  71. //var sfields = search_field.split(',');
  72. var qry = " "+$(obj).attr('data-search-type')+" (";
  73. keyword = keyword.toLowerCase();
  74. temp = keyword.split(" ");
  75. $.each(temp,function(index,value) {
  76. if (index===0) {
  77. qry+= search_field+" like '%"+value+"%'";
  78. }
  79. else {
  80. qry+= " AND "+search_field+" like '%"+value+"%'";
  81. }
  82. });
  83. qry+= ')';
  84. $(table_obj).attr('data-search',qry);
  85. $(table_obj).attr('data-active-page','0');
  86. init_SQLTable(table_obj);
  87. }
  88. function search_SQLTable(obj) {
  89. var search_field_type = '';
  90. var field_name = '';
  91. var operator = '';
  92. var qry = '';
  93. var tempval = '';
  94. var hash = '';
  95. var table_obj = $(obj).parent('div').parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  96. $(obj).parent('div').parent('div').parent('div').children('.search-field').each(function() {
  97. search_field_type = $(this).attr('data-search-type');
  98. operator = $(this).attr('data-search-operator');
  99. field_name = $(this).attr('data-search-field');
  100. field_name = field_name.replace('_min','');
  101. field_name = field_name.replace('_max','');
  102. hash = '#'+$(this).attr('data-search-field');
  103. tempval = $(hash).val();
  104. if (search_field_type==='date') {
  105. tempval = tempval.replace(/\./g,'-');
  106. }
  107. if (tempval && tempval!=='0' && tempval!=='') {
  108. if (search_field_type==='text') {
  109. qry+= " AND lower("+field_name+") LIKE lower('%"+tempval+"%')";
  110. }
  111. else {
  112. qry+= " AND "+field_name+operator+"'"+tempval+"'";
  113. }
  114. }
  115. });
  116. $(table_obj).attr('data-query',qry);
  117. init_SQLTable(table_obj);
  118. }
  119. function search_SQLTable_normal(obj) {
  120. var search_field_type = '';
  121. var field_name = '';
  122. var operator = '';
  123. var qry = '';
  124. var tempval = '';
  125. var hash = '';
  126. var table_obj = $(obj).parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  127. $(obj).parent('div').parent('div').children('.search-field').each(function() {
  128. search_field_type = $(this).attr('data-search-type');
  129. operator = $(this).attr('data-search-operator');
  130. field_name = $(this).attr('data-search-field');
  131. field_name = field_name.replace('_min','');
  132. field_name = field_name.replace('_max','');
  133. hash = '#'+$(this).attr('data-search-field');
  134. tempval = $(hash).val();
  135. if (search_field_type==='date') {
  136. tempval = tempval.replace(/\./g,'-');
  137. }
  138. if (tempval && tempval!=='0' && tempval!=='') {
  139. if (search_field_type==='text') {
  140. qry+= " AND lower("+field_name+") LIKE lower('%"+tempval+"%')";
  141. }
  142. else {
  143. qry+= " AND "+field_name+operator+"'"+tempval+"'";
  144. }
  145. }
  146. });
  147. $(table_obj).attr('data-query',qry);
  148. init_SQLTable(table_obj);
  149. }
  150. function detailed_search_SQLTable(obj) {
  151. var search_field_type = '';
  152. var field_name = '';
  153. var operator = '';
  154. var qry = '';
  155. var tempval = '';
  156. var hash = '';
  157. var table_obj = $(obj).parent('div').parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  158. $(obj).parent('div').parent('div').children('div').children('div').children('.tab-pane').each(function() {
  159. $(this).children('.search-field-detailed').each(function() {
  160. search_field_type = $(this).attr('data-search-type');
  161. operator = $(this).attr('data-search-operator');
  162. field_name = $(this).attr('data-search-field');
  163. field_name = field_name.replace('_min','');
  164. field_name = field_name.replace('_max','');
  165. hash = '#'+$(this).attr('data-search-field')+'_detail';
  166. tempval = $(hash).val();
  167. if (search_field_type==='date') {
  168. tempval = tempval.replace(/\./g,'-');
  169. }
  170. if (tempval && tempval!=='0' && tempval!=='') {
  171. if (search_field_type==='text') {
  172. qry+= " AND lower("+field_name+") LIKE lower('%"+tempval+"%')";
  173. }
  174. else {
  175. qry+= " AND "+field_name+operator+"'"+tempval+"'";
  176. }
  177. }
  178. });
  179. });
  180. $(table_obj).attr('data-query',qry);
  181. init_SQLTable(table_obj);
  182. }
  183. function delete_search_SQLTable(obj) {
  184. var table_obj = $(obj).parent('div').parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  185. $(table_obj).attr('data-query','');
  186. init_SQLTable(table_obj);
  187. }
  188. function delete_search_SQLTable_normal(obj) {
  189. var table_obj = $(obj).parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  190. $(table_obj).attr('data-query','');
  191. init_SQLTable(table_obj);
  192. }
  193. function detailed_delete_search_SQLTable(obj) {
  194. var table_obj = $(obj).parent('div').parent('div').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  195. $(table_obj).attr('data-query','');
  196. $('.form-control').val('');
  197. init_SQLTable(table_obj);
  198. }
  199. function deleterow_SQLTable(obj,id) {
  200. var table_obj = $(obj).parent('td').parent('tr').parent('tbody').parent('table');
  201. if (window.confirm('Valóban törölni szeretné a sort?')) {
  202. $(table_obj).attr('data-action','delete');
  203. $(table_obj).attr('data-id',id);
  204. init_SQLTable(table_obj);
  205. $(table_obj).children('thead').children('.message').children('th').children('.callout-danger').show('fast');
  206. }
  207. }
  208. function deleterow_alternate_SQLTable(obj,id) {
  209. if (window.confirm('Valóban törölni szeretné a sort?')) {
  210. $(obj).attr('data-action','delete');
  211. $(obj).attr('data-id',id);
  212. init_SQLTable(obj);
  213. $(obj).children('thead').children('.message').children('th').children('.callout-danger').show('fast');
  214. }
  215. }
  216. function undeleterow_SQLTable(obj,id) {
  217. var table_obj = $(obj).parent('td').parent('tr').parent('tbody').parent('table');
  218. if (window.confirm('Valóban vissza szeretné állítani ezt a sort?')) {
  219. $(table_obj).attr('data-action','undelete');
  220. $(table_obj).attr('data-id',id);
  221. init_SQLTable(table_obj);
  222. $(table_obj).children('thead').children('.message').children('th').children('.callout-danger').show('fast');
  223. }
  224. }
  225. function add_SQLTable_filter(type,filter,obj) {
  226. if (filter!=='' && type!=='') {
  227. var table_obj = $(obj).parent('li').parent('ul').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  228. $(table_obj).attr('data-filter-type',type);
  229. $(table_obj).attr('data-filter-command',filter);
  230. $(obj).parent('li').parent('ul').parent('div').children('.active-button').html(filter);
  231. init_SQLTable(table_obj);
  232. }
  233. else {
  234. return false;
  235. }
  236. }
  237. function add_SQLTable_filter_expand(type,filter,obj) {
  238. if (filter!=='' && type!=='') {
  239. var table_obj = $(obj).parent('li').parent('ul').parent('div').parent('th').parent('tr').parent('thead').parent('table');
  240. $(table_obj).attr('data-filter-type',type);
  241. $(table_obj).attr('data-filter-command',filter);
  242. $(obj).parent('li').parent('ul').parent('div').children('.active-button').html(filter);
  243. init_SQLTable(table_obj);
  244. }
  245. else {
  246. return false;
  247. }
  248. }
  249. function add_SQLTable_limit(limit,obj,table) {
  250. if (limit!=='') {
  251. var table_obj = $(table);
  252. $(table_obj).attr('data-pager-limit',limit);
  253. $(obj).parent('li').parent('ul').parent('div').children('.active-button').html(limit+' sor');
  254. init_SQLTable(table_obj);
  255. }
  256. else {
  257. return false;
  258. }
  259. }
  260. function show_row_SQLTable(obj,id) {
  261. if (id!=='') {
  262. var url = $(obj).attr('data-location');
  263. window.location=url+'?id='+id;
  264. }
  265. else {
  266. return false;
  267. }
  268. }
  269. function set_SQLTable_module(module,obj) {
  270. if (module!=='') {
  271. $('#module_active').html(module);
  272. $.getJSON('/admin/properties/setmodule/',{module: module},function(response) {
  273. $('#field_list').html(response.list);
  274. $('#field_active').html(response.active);
  275. var sql_statement = " AND property_module='"+module+"' AND property_field='"+response.active+"'";
  276. $('#properties').attr('data-query-default',sql_statement);
  277. init_SQLTable($('#properties'));
  278. });
  279. }
  280. else {
  281. return false;
  282. }
  283. }
  284. function set_SQLTable_field(field,obj) {
  285. if (field!=='') {
  286. $('#field_active').html(field);
  287. var module = $('#module_active').html();
  288. var sql_statement = " AND property_module='"+module+"' AND property_field='"+field+"'";
  289. $('#properties').attr('data-query-default',sql_statement);
  290. $.post('/admin/properties/setfield/',{field: field},function(response) {});
  291. init_SQLTable($('#properties'));
  292. }
  293. else {
  294. return false;
  295. }
  296. }
  297. function show_superfilter() {
  298. $('.superfilter').toggle();
  299. }
  300. function set_superfilter_active(obj) {
  301. $(obj).parent('li').children('input[type="checkbox"]').attr('checked','true');
  302. }
  303. function super_check_all(obj) {
  304. if ($(obj).is(':checked')) {
  305. //alert('ok');
  306. $(obj).parent('li').parent('ul').children('li').children('.supercheck').prop('checked',true);
  307. }
  308. else {
  309. $(obj).parent('li').parent('ul').children('li').children('.supercheck').removeAttr('checked');
  310. }
  311. }
  312. function supersearch(obj,table) {
  313. //var base_keyword = $(obj).children('.superfield').val();
  314. var base_keyword = $('#search_field_keyword').val();
  315. var sql = "SELECT * FROM viapan_persons WHERE person_status='1'";
  316. $(obj).children('ul').children('li').each(function() {
  317. if ($(this).children('.supermini').val()!='Nincs') {
  318. if ($(this).children('.supercheck').val()==='person_born_date') {
  319. if ($('#person_born_date_min').val()!=='') {
  320. sql+= " AND person_born_date>='"+$('#person_born_date_min').val()+"'";
  321. }
  322. if ($('#person_born_date_min').val()!=='') {
  323. sql+= " AND person_born_date<='"+$('#person_born_date_max').val()+"'";
  324. }
  325. }
  326. else {
  327. if ($(this).children('.supermini').val()!=='') {
  328. //sql+= " AND lower(person_tags) LIKE lower('%"+$(this).children('.supermini').val()+"%')";
  329. //sql+= " AND "+$(this).children('.supercheck').val()+" LIKE '%"+$(this).children('.supermini').val()+"%'";
  330. }
  331. }
  332. }
  333. });
  334. sql+= " AND lower(person_tags) LIKE lower('%"+base_keyword+"%')";
  335. $(table).attr('data-forced-sql',sql);
  336. init_SQLTable($(table));
  337. }
  338. function supersearch_clear(table) {
  339. $(table).attr('data-forced-sql','reset');
  340. init_SQLTable($(table));
  341. $('.superfield').val('');
  342. $('#search_field_keyword').val('');
  343. $('.supermini').val('');
  344. }
  345. function open_group(group,obj) {
  346. //$(obj).addClass('colsep-active');
  347. $(group).each(function() {
  348. if ($(this).hasClass('collapse-hide')) {
  349. $(this).removeClass('collapse-hide');
  350. }
  351. else {
  352. $(this).addClass('collapse-hide');
  353. }
  354. });
  355. }
  356. function super_ban(obj) {
  357. var inp = $(obj).parent('li').children('.supermini');
  358. //alert($(inp).attr('disabled'));
  359. if ($(inp).attr('disabled')==='disabled') {
  360. $(inp).val('');
  361. $(inp).removeAttr('disabled');
  362. }
  363. else {
  364. $(inp).val('Nincs');
  365. $(inp).attr('disabled','true');
  366. }
  367. }
  368. $('document').ready(function() {
  369. $('.SQLTable').each(function() {
  370. init_SQLTable($(this));
  371. });
  372. $('.search').on('keydown',function(event) {
  373. if (event.which === 13) {
  374. $('#gosearch').trigger('click');
  375. }
  376. });
  377. $('.det-search-inp').on('keydown',function(event) {
  378. if (event.which === 13) {
  379. $('#detailed_search_go').trigger('click');
  380. }
  381. });
  382. });