<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="script.js"></script>
<!-- * class=select_hierarhy_container * id * , ( ) * old new , , --> <div class=select_hierarhy_container id=field_1> <input type=hidden name=field_1[old] value=""/> </div>
// $(document).ready(function() { /** * */ $('.select_hierarhy_container').each(function() { selecHierarhyInit(this); }); }); /** * , . : * id (containerId), (initValue) * , * * @param v_container * - */ function selecHierarhyInit(v_container) { var container = $(v_container); var containerId = container.attr('id'); var initValue = container.children('input').val(); container.load('ajax.php?container_id=' + containerId + '&init_id=' + initValue); } /** * v_obj, * : id * (containerId) (level) * (parent_id) * , ( , * ) * * @param v_obj * select, */ function selecHierarhygetChilds(v_obj) { obj = $(v_obj); var containerId = obj.parents('.select_hierarhy_container').attr('id'); var parent_id = obj.val(); obj.nextAll().remove(); $.ajax({ url : 'ajax.php?container_id=' + containerId + '&parent_id=' + parent_id, success : function(data) { $('#' + containerId).append(data); } }); }
/** * , . * * * $_POST[$selectName] . - . * (, ) * * ( ) * , , * * @author dibrovsd */ class HerarhySelect { // select- private $selectName; /** * @param string $selectName select-, */ public function __construct($selectName) { $this->selectName = $selectName; } /** * * @param int $parentId */ public function getContent($parentId) { return $this->generateSelect($parentId); } /** * * , * ( ) * , * ( ) , , * @param int $nodeId */ public function getInit($nodeId) { if($nodeId == '') { return $this->getContent(-1); } else { $s = null; $parents = $this->getDataParentsList($nodeId); foreach($parents as $parentId => $childId) { $s .= $this->generateSelect($parentId, $childId); } $s .= $this->generateSelect($nodeId); return $s; } } /** * $parentId, $childId * @param int $parentId * @param int $childId */ private function generateSelect($parentId, $childId = null) { // - $childs = $this->getDataChilds($parentId); if($childs == null) { return; } $s = '<select name='.$this->selectName.'[new][] onChange="selecHierarhygetChilds(this);"> <option></option>'; foreach($childs as $id => $value) { $s .= '<option value="'.$id.'" '. ($id == $childId ? 'selected' : '').'>'. $value.'</option>'; } $s .='</select>'; return $s; } /** * . - * * * * . * , web- * - ( ) * * @param int $parentId */ private function getDataChilds($parentId) { $dataSource = $this->getDataArray(); if(!isset($dataSource[$parentId])) { return null; } else { return $dataSource[$parentId]; } } /** * , * $childId * - * , - * * ( ) * * : * Oracle , * PostgreSQL * , * ( ) * * @param int $child * @return int:int id : id , * , * */ private function getDataParentsList($childId) { $dataSource = $this->getDataArray(); $result = array(); $currentChild = $childId; // . // , do { $isParentFind = false; foreach($dataSource as $idParent => $childs) { if(isset($childs[$currentChild])) { $result[$idParent] = $currentChild; $isParentFind = true; $currentChild = $idParent; break; } } } while($isParentFind); return array_reverse($result, true); } /** * - . * , , , * , * */ private function getDataArray() { return array( -1 => array(1 => 'test1', 2 => 'test2', 3 => 'test3'), 1 => array(4 => 'test1_1', 5 => 'test1_2', 6 => 'test1_3'), 4 => array(7 => 'test2_1', 8 => 'test2_2', 9 => 'test2_3'), 9 => array(10 => 'test2_1_1', 11 => 'test2_1_2', 12 => 'test2_1_3') ); } }
if(isset($_POST['sendForm'])) { $data1 = array_reverse($_POST['field_1']['new']); $data2 = array_reverse($_POST['field_2']['new']); $val1 = $data1[0] == '' ? $data1[1] : $data1[0]; $val2 = $data2[0] == '' ? $data2[1] : $data2[0]; } else { $val1 = null; $val2 = 12; }
Source: https://habr.com/ru/post/132594/
All Articles