selected pastrat dupa refresh
Scris: Mie Dec 22, 2010
Salut,
Am si eu o problema cu dropdown menu si postez aici in speranta ca cineva imi poate da o idee in legatura cu problema..
Am functia urmatoare care genereaza html categoriile in optgroup si subcategoriile in option
Pana aici totul este ok.Problema este ca am codul Javascript care preia datele din optgroup si imi creaza douo campuri select ceea ce functioneaza perfect numai ca am incercat in nenumarate moduri sa pastrez ce am in categori la refresh si nu am reusit..Am sa atasez si codul javascript aici
EX: Dupa apasarea butonului submit am introdus intr-o variabila valoarea trimisa si deoarece javascriptul imi genereaza acel select am incercat in fe;lul urmator sa rezolv problema numai ca in alert imi da mereu null
atasez si codul pe care l-am incercat eu
Va rog daca este cineva si are o idee care ma poate ajuta... dupa refres-ul paginii ce am la categorii revine la normal implicit si subcategoriile.
Multumesc
Am si eu o problema cu dropdown menu si postez aici in speranta ca cineva imi poate da o idee in legatura cu problema..
Am functia urmatoare care genereaza html categoriile in optgroup si subcategoriile in option
Cod: Selectaţi tot
function genereazahtmlforselect($rows, $valsubcategory = '', $valcategorie = '')
{
$html = '<select name="subcategori" id="categorii" class="cascading" title="Categorie "> ';
$html .= '<optgroup label="Selecteaza">';
$html .= '<option value="">Alege o Categorie</option></optgroup>';
foreach ($rows as $category) {
if($category['parinte'] == '0'){
$html .= '<optgroup id="' . $category['id'] . '" title=" Subcategorie " label="' . $category['name'] . '"> ';
foreach ($rows as $subcategory) {
if ($subcategory['parinte'] == $category['id']){
$html .= '<option value="'.$subcategory['id'].'"';
if ($subcategory['id'] === $valsubcategory) {
$html .= 'selected = "selected"';
}
$html .= '>'.$subcategory['name'].'</option>';
}
}
$html .= '</optgroup>';
}
}
$html .= '</select>';
return $html;
}
Cod: Selectaţi tot
var CascadingMenu =
{
init: function()
{
var menus = Core.getElementsByClass("cascading");
for (var i = 0; i < menus.length; i++)
{
CascadingMenu.convertLabelToFieldset(menus[i]);
var masterMenu = CascadingMenu.extractMasterMenu(menus[i]);
CascadingMenu.createLabelFromTitle(masterMenu);
CascadingMenu.createLabelFromTitle(menus[i]);
CascadingMenu.updateSlaveMenu(masterMenu);
Core.addEventListener(masterMenu, "change", CascadingMenu.changeListener);
}
},
convertLabelToFieldset: function(menu)
{
var menuId = menu.id;
var labels = document.getElementsByTagName("label");
for (var i = 0; i < labels.length; i++)
{
if (labels[i].htmlFor == menuId)
{
var label = labels[i];
label.parentNode.removeChild(label);
var legend = document.createElement("legend");
while (label.hasChildNodes())
{
legend.appendChild(label.firstChild);
}
var fieldset = document.createElement("fieldset");
fieldset.appendChild(legend);
menu.parentNode.replaceChild(fieldset, menu);
fieldset.appendChild(menu);
return;
}
}
},
extractMasterMenu: function(menu)
{
var masterMenu = document.createElement("select");
masterMenu.id = menu.id + "master";
masterMenu.setAttribute("name", masterMenu.id);
masterMenu.setAttribute("title", menu.getAttribute("title"));
masterMenu._slave = menu;
while (menu.hasChildNodes())
{
var optgroup = menu.firstChild;
if (optgroup.nodeType == 1)
{
var masterOption = document.createElement("option");
masterOption.setAttribute("id", optgroup.getAttribute("id"));
masterOption.setAttribute("value", optgroup.getAttribute("id"));
masterOption.appendChild(document.createTextNode(optgroup.getAttribute("label")));
masterMenu.appendChild(masterOption);
var slaveOptions = [];
while (optgroup.hasChildNodes())
{
var option = optgroup.firstChild;
slaveOptions[slaveOptions.length] = option;
optgroup.removeChild(option);
}
masterOption._slaveOptions = slaveOptions;
menu.setAttribute("title", optgroup.getAttribute("title"));
}
menu.removeChild(optgroup);
}
menu.parentNode.insertBefore(masterMenu, menu);
return masterMenu;
},
createLabelFromTitle: function(menu)
{
var title = menu.getAttribute("title");
menu.setAttribute("title", "");
var label = document.createElement("label");
label.htmlFor = menu.id;
label.appendChild(document.createTextNode(title));
menu.parentNode.insertBefore(label, menu);
},
updateSlaveMenu: function(masterMenu)
{
var selectedOption = masterMenu.options[masterMenu.selectedIndex];
while (masterMenu._slave.hasChildNodes())
{
masterMenu._slave.removeChild(masterMenu._slave.firstChild);
}
for (var i = 0; i < selectedOption._slaveOptions.length; i++)
{
masterMenu._slave.appendChild(selectedOption._slaveOptions[i]);
}
masterMenu._slave.selectedIndex = 0;
},
changeListener: function(event)
{
CascadingMenu.updateSlaveMenu(this);
}
};
Core.start(CascadingMenu);
atasez si codul pe care l-am incercat eu
Cod: Selectaţi tot
if (isset( $categori )) {
?>
<script type="text/javascript">
(function () {
var categorieSelectata = <?php echo "'$categori'"; ?>;
var categorii = document.getElementById("categoriimaster");
alert(categorii);
for ( var i = 1; i < categorii.length; i++ ) {
if ( categorii.options[i].value === categorieSelectata ) {
categorii.selectedIndex = i;
break;
}
}
})();
</script>
<?php
}
Multumesc