Implement a simple form linked dropdown method that allows adding extra attribut…
…es to the input field. refs #1555
Showing
1 changed file
with
102 additions
and
2 deletions
| ... | @@ -136,7 +136,7 @@ function validate_creditcard($cc_num, $type) { | ... | @@ -136,7 +136,7 @@ function validate_creditcard($cc_num, $type) { |
| 136 | 136 | ||
| 137 | if ( ! function_exists('form_dropdown')) | 137 | if ( ! function_exists('form_dropdown')) |
| 138 | { | 138 | { |
| 139 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '') | 139 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '', $blankFirstOption = true) |
| 140 | { | 140 | { |
| 141 | if ( ! is_array($selected)) | 141 | if ( ! is_array($selected)) |
| 142 | { | 142 | { |
| ... | @@ -157,7 +157,10 @@ if ( ! function_exists('form_dropdown')) | ... | @@ -157,7 +157,10 @@ if ( ! function_exists('form_dropdown')) |
| 157 | 157 | ||
| 158 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; | 158 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; |
| 159 | 159 | ||
| 160 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n<option value=''>Select...</option>"; | 160 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n"; |
| 161 | if ($blankFirstOption) { | ||
| 162 | $form .= '<option value="">Select...</option>'; | ||
| 163 | } | ||
| 161 | 164 | ||
| 162 | foreach ($options as $key => $val) | 165 | foreach ($options as $key => $val) |
| 163 | { | 166 | { |
| ... | @@ -279,6 +282,103 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par | ... | @@ -279,6 +282,103 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par |
| 279 | return $form; | 282 | return $form; |
| 280 | } | 283 | } |
| 281 | 284 | ||
| 285 | if (!function_exists('formLinkedDropdownWithExtra')) { | ||
| 286 | function formLinkedDropdownWithExtra($name = '', $parent = '', $options = array(), $parent_selected = '', $selected = '', $extra = '', $provide_js = true, $blankFirstOption = true, $inputExtra = '') | ||
| 287 | { | ||
| 288 | if ($extra != '') { | ||
| 289 | $extra = ' '.$extra; | ||
| 290 | } | ||
| 291 | if ($inputExtra != '') { | ||
| 292 | $inputExtra = ' '.str_ireplace('"', '\"', $inputExtra); | ||
| 293 | } | ||
| 294 | |||
| 295 | $form = '<select name="'.$name.'" id="'.$name.'"'.$extra.">\n"; | ||
| 296 | |||
| 297 | if (isset($options[$parent_selected])) { | ||
| 298 | foreach ($options[$parent_selected] as $key => $val) { | ||
| 299 | $sel = ($selected != $key) ? '' : ' selected="selected"'; | ||
| 300 | |||
| 301 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n"; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | if ($provide_js) { | ||
| 306 | $form .= '</select> | ||
| 307 | <script> | ||
| 308 | function update_'.$name.'(value) { | ||
| 309 | if (value == "") { | ||
| 310 | return; | ||
| 311 | } | ||
| 312 | |||
| 313 | jQuery("#'.$name.'_custom").attr("disabled", "disabled").hide(); | ||
| 314 | jQuery("#'.$name.'").show(); | ||
| 315 | |||
| 316 | var options = new Array(); | ||
| 317 | var select = document.getElementById("'.$name.'"); | ||
| 318 | select.disabled = false; | ||
| 319 | while (select.firstChild) | ||
| 320 | select.removeChild(select.firstChild); | ||
| 321 | switch (value.toString()) {'; | ||
| 322 | foreach ($options as $k => $v) { | ||
| 323 | $form .= 'case "'.$k.'": | ||
| 324 | options = new Array('; | ||
| 325 | foreach ($v as $value => $content) { | ||
| 326 | $form .= '"'.$value.'","'.$content.'",'; | ||
| 327 | } | ||
| 328 | $form = substr($form, 0, -1); | ||
| 329 | $form .= '); | ||
| 330 | break;'; | ||
| 331 | } | ||
| 332 | $form .= 'default: | ||
| 333 | select.disabled = true; | ||
| 334 | |||
| 335 | jQuery("#'.$name.'_custom").removeAttr("disabled").show(); | ||
| 336 | jQuery("#'.$name.'").hide(); | ||
| 337 | |||
| 338 | return; | ||
| 339 | }'; | ||
| 340 | |||
| 341 | if ($blankFirstOption) { | ||
| 342 | $form .= ' | ||
| 343 | var option = document.createElement("option"); | ||
| 344 | var val = ""; | ||
| 345 | option.val = val; | ||
| 346 | option.innerHTML = "Select..."; | ||
| 347 | select.appendChild(option);'; | ||
| 348 | } | ||
| 349 | |||
| 350 | $form .= 'for(var i = 0; i < options.length;) { | ||
| 351 | var option = document.createElement("option"); | ||
| 352 | var val = options[i++]; | ||
| 353 | option.value = val; | ||
| 354 | option.innerHTML = options[i++]; | ||
| 355 | if (val == "'.$selected.'") { | ||
| 356 | option.selected = true; | ||
| 357 | } | ||
| 358 | select.appendChild(option); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | var elem = $("<input type=\'text\' name=\''.$name.'\' id=\''.$name.'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' '.$inputExtra.'/>").hide(); | ||
| 363 | elem.insertAfter(jQuery("#'.$name.'")); | ||
| 364 | update_'.$name.'(document.getElementById("'.$parent.'").value); | ||
| 365 | document.getElementById("'.$parent.'").onchange = function() { update_'.$name.'(this.value); } | ||
| 366 | </script> | ||
| 367 | '; | ||
| 368 | } else { | ||
| 369 | $form = ''; | ||
| 370 | if (array_key_exists($parent_selected, $options)) { | ||
| 371 | foreach ($options[$parent_selected] as $name => $option) { | ||
| 372 | $sel = ($selected == $name) ? ' selected="selected"' : ''; | ||
| 373 | $form .= '<option value="'.$name.'"'.$sel.'>'.$option."</option>\n"; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | return $form; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 282 | if ( ! function_exists('url_title')) | 382 | if ( ! function_exists('url_title')) |
| 283 | { | 383 | { |
| 284 | function url_title($str, $separator = 'dash', $lowercase = FALSE) | 384 | function url_title($str, $separator = 'dash', $lowercase = FALSE) | ... | ... |
-
Please register or sign in to post a comment