diff --git a/module/datatable/js/ajaxcustom.ui.js b/module/datatable/js/ajaxcustom.ui.js index f7d1245b14..66951803a0 100644 --- a/module/datatable/js/ajaxcustom.ui.js +++ b/module/datatable/js/ajaxcustom.ui.js @@ -1,8 +1,36 @@ -function saveCustomCols() +$(() => { - $.ajaxSubmit - ({ - url: ajaxSaveUrl, - data: {} + const btn = document.querySelector('#ajax-save'); + btn.addEventListener('click', () => { + const formData = []; + let index = 0; + const types = ['left', 'no', 'right']; + const getSelector = (value) => `[data-zin-gid="${formGID}"] .drag-ul.${value}-cols`; + const colsList = types.map(x => document.querySelector(getSelector(x))); + + for(let i = 0; i < colsList.length; i++) + { + const cols = colsList[i]; + if(!cols) continue; + + const children = Array.from(cols.children); + for(let j = 0; j < children.length; j++) + { + const li = children[j]; + const checkbox = li.querySelector('input[type="checkbox"]'); + const input = li.querySelector('input[type="text"]'); + formData.push({ + id: li.dataset.key, + order: ++index, + show: checkbox.checked, + width: input.value + 'px', + fixed: types[i], + }); + } + } + + fetch(ajaxSaveUrl, {method: 'post', body: JSON.stringify(formData)}) + .then(res => res.json()) + .then((json) => {if(json.result === 'success') loadTable();}); }); -} +}); diff --git a/module/datatable/ui/ajaxcustom.html.php b/module/datatable/ui/ajaxcustom.html.php index 744df78448..de7dbffe54 100644 --- a/module/datatable/ui/ajaxcustom.html.php +++ b/module/datatable/ui/ajaxcustom.html.php @@ -14,7 +14,6 @@ namespace zin; global $lang, $app; $app->loadLang('datatable'); -$formGID = null; jsVar('ajaxSaveUrl', $this->createLink('datatable', 'ajaxSave', "module={$module}&method={$method}")); function buildItem(array $item): wg @@ -65,7 +64,6 @@ function getDefaultConfig(string $name): array function buildBody(array $cols): form { - global $formGID; $itemsList = array( 'left' => array(), 'no' => array(), @@ -75,6 +73,7 @@ function buildBody(array $cols): form foreach($cols as $col) { if($col['type']) $col = array_merge(getDefaultConfig($col['type']), $col); + if(!isset($col['fixed']) || empty($col['fixed'])) $col['fixed'] = 'no'; $itemsList[$col['fixed']][] = array( 'required' => isset($col['required']) && $col['required'] === true, 'title' => $col['title'], @@ -101,48 +100,10 @@ function buildBody(array $cols): form } $body->setProp('data-zin-gid', $body->gid); - $formGID = $body->gid; + jsVar('formGID', $body->gid); return $body; } -function submitFunc(): string -{ - global $formGID; - - return << '[data-zin-gid="{$formGID}"] .drag-ul.' + value + '-cols'; - const colsList = types.map(x => document.querySelector(getSelector(x))); - - for(let i = 0; i < colsList.length; i++) - { - const cols = colsList[i]; - if(!cols) continue; - - const children = Array.from(cols.children); - for(let j = 0; j < children.length; j++) - { - const li = children[j]; - const checkbox = li.querySelector('input[type="checkbox"]'); - const input = li.querySelector('input[type="text"]'); - formData.push({ - id: li.dataset.key, - order: ++index, - show: checkbox.checked, - width: input.value + 'px', - fixed: types[i], - }); - } - } - - fetch(ajaxSaveUrl, {method: 'post', body: JSON.stringify(formData)}) - .then(res => res.json()) - .then((json) => {if(json.result === 'success') loadTable();}); - FUNC; -} - setClass('edit-cols'); set::title($lang->datatable->custom); to::header(span($lang->datatable->customTip, setClass('text-gray', 'text-md'))); @@ -154,8 +115,7 @@ to::footer ( item ( - set(array('text' => $lang->save, 'type' => 'primary', 'class' => 'w-28', 'data-dismiss' => 'modal')), - on::click($this->submitFunc()) + set(array('text' => $lang->save, 'type' => 'primary', 'class' => 'w-28', 'data-dismiss' => 'modal', 'id' => 'ajax-save')), ) ) );