dever 6 年之前
父節點
當前提交
0d42995fbc
共有 3 個文件被更改,包括 84 次插入45 次删除
  1. 2 2
      database/admin.php
  2. 73 41
      src/Database.php
  3. 9 2
      src/Lib/Input.php

+ 2 - 2
database/admin.php

@@ -132,8 +132,8 @@ return array
 			'desc' 		=> '请输入管理员名',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
-			# 自动完成功能,第一个参数是请求的地址,请自行实现,第二个参数是要使用的字段,共有id和value两个选择,id会特殊处理,value则直接把当前值写入,第三个参数是直接替换当前的值
-			//'autocomplete' => array('auth.role?json=1', 'value', 'manage/role-check#name'),
+			# 自动完成功能,第一个参数是请求的地址,请自行实现,第二个参数是要使用的字段,共有id和value两个选择,id会特殊处理,value则直接把当前值写入,第三个参数是直接替换当前的值,第四个参数是当前表里的字段名,一般为分类的id
+			//'autocomplete' => array('auth.role?json=1', 'value', 'manage/role-check#name', 'cate_id'),
 			//'autocomplete' => array('auth.role?json=1'),
 			'search'	=> 'order,fulltext',
 			'list'		=> true,

+ 73 - 41
src/Database.php

@@ -70,6 +70,7 @@ class Database
         $this->menu = Dever::input('menu');
         $this->menu_id = Dever::input('menu_id');
         $this->id = Dever::input('update_where_id', Dever::input('where_id'));
+        $this->name = $this->project . '/' . $this->table;
 
         $this->top = Dever::input('top');
         if ($this->top) {
@@ -77,6 +78,23 @@ class Database
         }
     }
 
+    /**
+     * 提供给异步获取数据的接口
+     *
+     * @return array
+     */
+    public function getAjax($data)
+    {
+        $key = Dever::input('key');
+        $value = Dever::input('value');
+        $source = Dever::input('source');
+        
+        $config = Dever::db($source)->config['struct'][$key];
+        $config['value'] = $value;
+        $config['option'] = $data;
+        return $this->create_update_html($key, $config);
+    }
+
     /**
      * url
      *
@@ -2107,30 +2125,14 @@ class Database
                     }
                 }
                 # 处理show模式下的class
-                $class = $this->update_class($k, $v);
+                $class = $this->update_class($k, $v, '', $data);
 
                 $result[$k] = '<div class="form-group ' . $class . '"><div class="margin-top">';
 
-                $v['ext'] = $v['col'] = $k;
-
-                $v['update'] = $v['update'] ? $v['update'] : 'text';
-
-                if (isset($v['match']) && $v['match'] != 'option') {
-                    if (is_string($v['match']) && strpos($v['match'], '/') !== false) {
-                        $v['valid'] = 'validate[required,custom[' . $k . ']]';
-                    } else {
-                        $v['valid'] = 'validate[required]';
-                    }
-                }
-
-                if (is_string($v['update']) && strpos($v['update'], 'copy.') !== false) {
-                    $v['value'] = $v['update'];
-                    $v['update'] = 'hidden';
-                }
-
-                $this->update_html($v, $k, $k, $data, $result);
+                $result[$k] .= $this->create_update_html($k, $v, $data);
 
                 $result[$k] .= '</div></div>';
+
             } elseif (!$col && isset($v['class'])) {
                 # 分割线
                 $v['name'] = $this->value($v['name'], array('test' => 1));
@@ -2152,19 +2154,39 @@ class Database
      *
      * @return string
      */
-    public function update_class($name, $param, $prefix = '')
+    public function update_class($name, $param, $prefix = '', $data = array())
     {
         $class = '';
         $key = 'show';
         if (isset($param[$key]) && $param[$key]) {
             $parent = array();
             $child = array();
+            if (is_string($param[$key])) {
+                parse_str($param[$key], $param[$key]);
+            }
             if (is_array($param[$key])) {
                 foreach ($param[$key] as $k => $v) {
-                    $this->controlShow($name, $parent, $child, $key, $v, $prefix);
+                    if ($k == 'no') {
+                        $child[$k . '_' . $v] = $key . '_no';
+                    } elseif (strpos($v, '/')) {
+                        $parent[$k] = $key . '_' . $k;
+                        $k = $k . '_ajax';
+                        $value = '';
+                        if (isset($data[$name])) {
+                            $value = $data[$name];
+                        }
+                        $v = str_replace('?', '?key='.$name.'&value='.$value.'&source='.$this->name.'&', $v);
+                        $child[$k] = $key . '_' . $k .'" dever-ajax="'.Dever::url($v).'"';
+                    } else {
+                        $show = $k . '_' . $v;
+                        if ($prefix) {
+                            $k = str_replace($name, $k, $prefix);
+                            $show = str_replace($name, $show, $prefix);
+                        }
+                        $parent[$k] = $key . '_' . $k;
+                        $child[$show] = $key . '_' . $show;
+                    }
                 }
-            } else {
-                $this->controlShow($name, $parent, $child, $key, $param[$key], $prefix);
             }
 
             $class = implode(' ', $parent) . ' ' . implode(' ', $child);
@@ -2177,24 +2199,34 @@ class Database
     }
 
     /**
-     * controlShow
+     * update_html
      *
-     * @return mixed
+     * @return string
      */
-    private function controlShow($name, &$parent, &$child, $key, $show, $prefix = '')
+    public function create_update_html($key, $value, $data = array())
     {
-        if (strpos($show, '=') !== false) {
-            $temp = explode('=', $show);
-            $show = str_replace('=', '_', $show);
-            if ($prefix) {
-                $temp[0] = str_replace($name, $temp[0], $prefix);
-                $show = str_replace($name, $show, $prefix);
+        $result = '';
+
+        $value['ext'] = $value['col'] = $key;
+
+        $value['update'] = $value['update'] ? $value['update'] : 'text';
+
+        if (isset($value['match']) && $value['match'] != 'option') {
+            if (is_string($value['match']) && strpos($value['match'], '/') !== false) {
+                $value['valid'] = 'validate[required,custom[' . $key . ']]';
+            } else {
+                $value['valid'] = 'validate[required]';
             }
-            $parent[$temp[0]] = $key . '_' . $temp[0];
-            $child[$show] = $key . '_' . $show;
-        } elseif ($show == 'no') {
-            $child[$show] = $key . '_no';
         }
+
+        if (is_string($value['update']) && strpos($value['update'], 'copy.') !== false) {
+            $value['value'] = $value['update'];
+            $value['update'] = 'hidden';
+        }
+
+        $this->update_html($value, $key, $key, $data, $result);
+
+        return $result;
     }
 
     /**
@@ -2205,7 +2237,7 @@ class Database
     public function update_html($param, $key, $index, $data, &$result)
     {
         if (isset($param['name']) && $param['update'] != 'hidden' && isset($param['update'])) {
-            $result[$key] .= Html::pageLeft($param);
+            $result .= Html::pageLeft($param);
         }
 
         if ($param['update']) {
@@ -2257,7 +2289,7 @@ class Database
             $method = $param['update'];
 
             if (is_array($method)) {
-                $result[$key] .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="dever_form_add" toggle_key="' . $key . '">增加一条</a></li></ul>';
+                $result .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="dever_form_add" toggle_key="' . $key . '">增加一条</a></li></ul>';
 
                 $tab = '';
 
@@ -2293,16 +2325,16 @@ class Database
                     $tab = '<li class="active"><a href="javascript:;" id="tab-' . $key . '-child-1" toggle_key="' . $key . '" toggle_id="1" toggle_child=".dever_' . $param['col'] . '_child" toggle="#' . $key . '-child-1" onclick="showToggle($(this))">第1条</a></li>';
                 }
 
-                $result[$key] = str_replace('{tab}', $tab, $result[$key]);
+                $result = str_replace('{tab}', $tab, $result);
             } else {
                 if (isset($param['update_input']) && isset($data[$index . '_input'])) {
                     $param['update_input_data'] = explode(',', $data[$index . '_input']);
                 }
-                $result[$key] .= Html::$method($param);
+                $result .= Html::$method($param);
             }
 
             if (isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password')) {
-                $result[$key] .= Html::desc($param['desc']);
+                $result .= Html::desc($param['desc']);
             }
         }
     }

+ 9 - 2
src/Lib/Input.php

@@ -358,7 +358,7 @@ class Input
                 $js = '';
                 if (isset($param['control']) && is_string($param['control'])) {
                     //print_r($param);die;
-                    $js = ' show_id="' . $param['control'] . '" onclick="inputShow($(this), \'' . $param['control'] . '\')"';
+                    $js = ' show_id="' . $param['control'] . '" onclick="inputShow($(this), \'' . $param['control'] . '\', \'input\')"';
                 } elseif (isset($param['bind'])) {
                     $param['bind'][2] = isset($param['bind'][2]) ? str_replace('"', '\'', json_encode($param['bind'][2], true)) : false;
                     $js = $param['bind'][0] . '="' . $param['bind'][1] . '(\'' . $param['name'] . '\', $(this), ' . $param['bind'][2] . ')" ';
@@ -520,7 +520,10 @@ class Input
             //$param['name'] .= '_parent';
         }
 
-        if (isset($param['bind'])) {
+        if (isset($param['control']) && is_string($param['control'])) {
+            //print_r($param);die;
+            $change = ' show_id="' . $param['control'] . '" onchange="inputShow($(this), \'' . $param['control'] . '\')"';
+        } elseif (isset($param['bind'])) {
             $param['bind'][2] = isset($param['bind'][2]) ? str_replace('"', '\'', json_encode($param['bind'][2], true)) : false;
             $change = $param['bind'][0] . '="' . $param['bind'][1] . '($(this), ' . $param['bind'][2] . ')" ';
 
@@ -620,6 +623,10 @@ class Input
 
                 $auto .= ' onload="' . $param['bind'][1] . '($(this), ' . $param['bind'][2] . ')" ';
             }
+
+            if (isset($param['autocomplete'][3])) {
+                $auto .= ' complete_cate="' . $param['autocomplete'][3] . '" ';
+            }
             return '<input ' . $auto . ' type="' . (isset($param['update']) ? $param['update'] : 'text') . '" class="autoload_text update_value ' . (isset($param['class']) ? $param['class'] : 'form-control') . ' ' . (isset($param['valid']) ? $param['valid'] : '') . '" value="' . $value . '" name="' . $param['name'] . '" id="' . $param['name'] . '_value" autocomplete="new'.time().'" placeholder="' . (isset($param['placeholder']) ? $param['placeholder'] : '') . '"/>' . $input;
         }
     }