LinkcatWidget.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace app\common\widget;
  3. use yii\base\Widget;
  4. use app\assets\CoreAsset;
  5. use app\assets\PluginsAsset;
  6. use app\modules\cms\models\Category;
  7. use yii\helpers\Html;
  8. use Yii;
  9. class LinkcatWidget extends Widget
  10. {
  11. public $model;
  12. public $level;
  13. public $content_model_id;
  14. public $name;
  15. public $value;
  16. public $id;
  17. public $blockClass='';
  18. public $unitClass = '';
  19. public $selectClass = '';
  20. public $emptyTxts = [];
  21. public $dataUrl = '';
  22. public $selectedIds=[0,0,0,0];
  23. public $style;
  24. public $blockStyle;
  25. public $unitStyle;
  26. public $space;
  27. public $is_open;
  28. public $select_default;
  29. public $required;
  30. public function init()
  31. {
  32. if(empty($this->name))
  33. {
  34. echo '缺少NAME';
  35. exit;
  36. }
  37. if(empty($this->level))
  38. {
  39. echo '请设置层级';
  40. exit;
  41. }
  42. if(empty($this->content_model_id))
  43. {
  44. echo '请设置内容模型';
  45. exit;
  46. }
  47. $this->dataUrl = Yii::$app->controller->createRealUrl('ajax/nextlinkcat');
  48. if($this->value)$this->selectedIds = Category::getParentCatIdRs($this->value);
  49. }
  50. public function run()
  51. {
  52. $this->registerClientScript();
  53. if(!empty($this->model))
  54. {
  55. $modelName = strtolower(basename(str_replace("\\",DIRECTORY_SEPARATOR,get_class($this->model))));
  56. if(empty($this->id)) $this->id = $modelName.'-'.$this->name;
  57. echo Html::activeTextInput($this->model, $this->name, ['value'=>$this->value,'id'=>$this->id,'style'=>'display:none','class'=>$this->required.' layui-input', 'lay-verify'=>$this->required, 'data-level'=>$this->level,'data-verifyid'=>$this->id."_".$this->level]);
  58. }
  59. else
  60. {
  61. if(empty($this->id)) $this->id = str_replace(array('[',']'),array('-',''),$this->name);
  62. else $this->id = str_replace(array('[',']'),array('-',''),$this->id);
  63. echo "<input type='hidden' value='".$this->value."' id='".$this->id."' name='".$this->name."' class='".$this->required." layui-input' lay-verify='".$this->required."' data-level='".$this->level."' data-verifyid='".$this->id."_".$this->level."'>";
  64. }
  65. echo "<div id='".$this->id."_block' class='".$this->blockClass."' style='".$this->blockStyle."' >";
  66. for($i=1;$i<=$this->level;$i++)
  67. {
  68. echo "<div class='".$this->unitClass."' style='".$this->unitStyle."'>";
  69. echo "<select lay-ignore class='".$this->id."_".$i." ".$this->selectClass."' id='".$this->id."_".$i."' name='".$this->id."_".$i."' >";
  70. echo "<option value=''>".$this->emptyTxts[$i-1]."</option>";
  71. echo "</select>";
  72. echo "</div>";
  73. }
  74. echo "</div>";
  75. $js = <<<JS
  76. //setInterval(function(){console.log($('#{$this->id}').val())},2000)
  77. $('#{$this->id}_block').cascadingDropdown({
  78. selectBoxes: [
  79. {
  80. selector: '.{$this->id}_1',
  81. source: function(request, response) {
  82. var content_model_id = {$this->content_model_id};
  83. var data_url = '{$this->dataUrl}';
  84. $.getJSON(data_url, {content_model_id:content_model_id}, function(result) {
  85. var data = result.data;
  86. var selectOnlyOption = data.length >= 1;
  87. response($.map(data, function(item, index) {
  88. return {
  89. label: item.cat_name,
  90. value: item.cat_id,
  91. selected: item.cat_id=='{$this->selectedIds[0]}'
  92. };
  93. }));
  94. });
  95. },
  96. onChange: function(event, value, requiredValues, requirementsMet){
  97. var targetId="{$this->id}_{$this->level}";
  98. var realId = "{$this->id}";
  99. var eventTargetId = $(event.target).attr('id');
  100. if(value!='')
  101. {
  102. $('#'+realId).val(value)
  103. }
  104. else
  105. {
  106. $('#'+realId).val('')
  107. }
  108. }
  109. },
  110. {
  111. selector: '.{$this->id}_2',
  112. requires: ['.{$this->id}_1'],
  113. source: function(request, response) {
  114. var parent_id;
  115. $.each(request, function(key, val) {
  116. parent_id = val;
  117. });
  118. var data_url = '{$this->dataUrl}';
  119. $.getJSON(data_url, {parent_id:parent_id,content_model_id:{$this->content_model_id}}, function(result) {
  120. var data = result.data;
  121. var selectOnlyOption = data.length >= 1;
  122. response($.map(data, function(item, index) {
  123. return {
  124. label: item.cat_name,
  125. value: item.cat_id,
  126. selected: item.cat_id=='{$this->selectedIds[1]}'
  127. };
  128. }));
  129. if(data.length==0)
  130. {
  131. $('.{$this->id}_2').attr('disabled','disable');
  132. $('.{$this->id}_3').attr('disabled','disable');
  133. $('.{$this->id}_4').attr('disabled','disable');
  134. var realId = "{$this->id}";
  135. $('#'+realId).val($('.{$this->id}_1 option:selected').val());
  136. }
  137. });
  138. },
  139. onChange: function(event, value, requiredValues, requirementsMet){
  140. var targetId="{$this->id}_{$this->level}";
  141. var realId = "{$this->id}";
  142. var eventTargetId = $(event.target).attr('id');
  143. if(value!='')
  144. {
  145. $('#'+realId).val(value)
  146. }
  147. else
  148. {
  149. $('#'+realId).val('')
  150. }
  151. }
  152. },
  153. {
  154. selector: '.{$this->id}_3',
  155. requires: ['.{$this->id}_1', '.{$this->id}_2'],
  156. requireAll: true,
  157. source: function(request, response) {
  158. var parent_id;
  159. $.each(request, function(key, val) {
  160. parent_id = val;
  161. });
  162. var data_url = '{$this->dataUrl}';
  163. $.getJSON(data_url, {parent_id:parent_id,content_model_id:{$this->content_model_id}}, function(result) {
  164. var data = result.data;
  165. response($.map(data, function(item, index) {
  166. return {
  167. label: item.cat_name,
  168. value: item.cat_id,
  169. selected: item.cat_id=='{$this->selectedIds[2]}'
  170. };
  171. }));
  172. if(data.length==0)
  173. {
  174. $('.{$this->id}_3').attr('disabled','disable');
  175. $('.{$this->id}_4').attr('disabled','disable');
  176. var realId = "{$this->id}";
  177. $('#'+realId).val($('.{$this->id}_2 option:selected').val());
  178. }
  179. });
  180. },
  181. onChange: function(event, value, requiredValues, requirementsMet){
  182. var targetId="{$this->id}_{$this->level}";
  183. var realId = "{$this->id}";
  184. var eventTargetId = $(event.target).attr('id');
  185. if(value!='')
  186. {
  187. $('#'+realId).val(value)
  188. }
  189. else
  190. {
  191. $('#'+realId).val('')
  192. }
  193. }
  194. },
  195. {
  196. selector: '.{$this->id}_4',
  197. requires: ['.{$this->id}_1', '.{$this->id}_2', '.{$this->id}_3'],
  198. requireAll: true,
  199. source: function(request, response) {
  200. var parent_id;
  201. $.each(request, function(key, val) {
  202. parent_id = val;
  203. });
  204. var data_url = '{$this->dataUrl}';
  205. $.getJSON(data_url, {parent_id:parent_id,content_model_id:{$this->content_model_id}}, function(result) {
  206. var data = result.data;
  207. response($.map(data, function(item, index) {
  208. return {
  209. label: item.cat_name,
  210. value: item.cat_id,
  211. selected: item.cat_id=='{$this->selectedIds[3]}'
  212. };
  213. }));
  214. if(data.length==0)
  215. {
  216. $('.{$this->id}_4').attr('disabled','disable');
  217. var realId = "{$this->id}";
  218. $('#'+realId).val($('.{$this->id}_3 option:selected').val());
  219. }
  220. });
  221. },
  222. onChange: function(event, value, requiredValues, requirementsMet){
  223. var targetId="{$this->id}_{$this->level}";
  224. var realId = "{$this->id}";
  225. var eventTargetId = $(event.target).attr('id');
  226. if(value!='')
  227. {
  228. $('#'+realId).val(value)
  229. }
  230. else
  231. {
  232. $('#'+realId).val('')
  233. }
  234. }
  235. }
  236. ]
  237. });
  238. JS;
  239. $view = $this->getView();
  240. $view->registerJs($js);
  241. }
  242. protected function registerClientScript()
  243. {
  244. $view = $this->getView();
  245. $coreAsset = CoreAsset::register($view);
  246. $coreAssetsUrl = $coreAsset->baseUrl;
  247. $assetPlugin = PluginsAsset::register($view);
  248. $pluginUrl = $assetPlugin->baseUrl;
  249. return array('coreAssetsUrl'=>$coreAssetsUrl,'pluginUrl'=>$pluginUrl);
  250. }
  251. }