Show optgroup in selected values in selctize.js - selectize.js

We have a selectize field where multiple values can be selected. These values are grouped in optgroups. To ease understanding, some values can be in multiple optgroups, we would like to see the name of the optgroup in the labels for the selected fields, too. How can we accomplish this?
See following fiddle, if you select "First Item" you do not know which one was selected, Store or Warehouse.
Thanks!
https://jsfiddle.net/4t8fjj7g/1/
HTML:
<select name="test" id="test-select" multiple="multiple">
<optgroup label="Store">
<option data-type="stores" value="1">First item</option>
<option data-type="stores" value="2">Second item</option>
</optgroup>
<optgroup label="Warehouse">
<option data-type="warehouses" value="3">First item</option>
<option data-type="warehouses" value="4">Second item</option>
</optgroup>
</select>
And this is the javascript code:
$('select#test-select').selectize({
searchField: ['text', 'type']
});

Basically, what Daffy has suggested, BUT...
You can use custom rendering functions for dropdown options and for selected items:
$('select#test-select').selectize({
searchField: ['text', 'type'],
render: {
item: itemRenderFunction,
option: optionRenderFunction
}
});
This will allow you to avoid label multiplication in selected items list.

What I did is have the values have the optgroup then the value like optgroup_value then when the form gets submitted on the server or doing something in JS I can get it by exploding the string like:
<select multiple class="my-select">
<optgroup label="taxes">
<?php foreach ( $taxes as $tax) : ?>
<option value="tax_<?php echo $tax->slug; ?>"><?php echo $tax->name; ?></option>
<?php endforeach; ?>
</optgroup>
<optgroup label="Version">
<?php foreach ( $my_versions as $version ) : ?>
<option value="version_<?php echo $version->slug; ?>"><?php echo $version->name; ?></option>
<?php endforeach; ?>
</optgroup>
<optgroup label="Language">
<?php foreach ( $my_languages as $language ) : ?>
<option value="language_<?php echo $language->slug; ?>"><?php echo $language->name; ?></option>
<?php endforeach; ?>
</optgroup>
</select>
then
<?php
foreach ( $_POST['taxes'] as $val ) {
if ( ! empty( $val ) ) {
// $val_parts is an array with
// $val_parts[0] = optgroup only
// $val_parts[1] = the value of the option minus optgroup
$val_parts = explode( "_", $val );
// My use case to add a meta_query to a WP_Query so add filter based on both values
$meta_arg = array(
'taxonomy' => "my_" . $val_parts[0], // custom prefix then optgroup
'field' => 'slug',
'terms' => $val_parts[1] // value
);
// Add to running array and possibly do again
array_push( $args['tax_query'], $meta_arg );
}
}

Related

Populate Select using ViewBag without using html Helper

I Have this in my controller
ViewBag.list = new SelectList(db.ListOfDB, "Value", "Text");
I need to create a Select, but without using the #Html.DropDownList I try to use this:
<select id="mylist"> ??? (selectitem) #ViewBag.list ??? </select>
How create select based from Viewbag, but not use Html.Helper
You can pass your list to the view on your model. You could then do
<select id="myList">
#foreach(var item in Model.myList)
{
<option value="#item.Value">item.Name</option>
}
</select>
#{
ViewBag.Title = "";
Layout = "~/Views/Shared/_LayoutLanding.cshtml";
List<SelectListItem> list = ViewBag.list;
}
#for(int i = 0; i < list.Count; i++)
{
<option value="#list[i].Value">#list[i].Text</option>
}
After many attempts, this one worked for me
This one in the Controller -
ViewBag.ilist = new SelectList(db.tblPersons.Where(d => d.PersonTyp == "SUPPLIER").ToList(), "slno", "PersonName");
'slno' being the Id column and 'PersonName' being the 'Data' column
Then in Razor page (Without HTML helpers)
<select id="dpdnperson" name="dpdnperson" class="dropdown btn bg-gradient-primary" style="color:white; width:100%" Font-Size="Medium">
<option>-- SELECT --</option>
#foreach (var item in ViewBag.ilist)
{
<option value="#item.Value" >#item.Text</option>
}
</select>

Pagination not working( using wp-paginate) i m using wordpress

here is my code, I am using wp-paginate plugin in wordpress. I have tried many thing like replacing twentyfourteen_paging_nav(); with
if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentyfourteen_paging_nav();
}
in archieve.php but still its not working.
<ul class="blog-list">
<?php
$blog = array(
'category' => '8', // post_id of category
'order' => 'Asc',
'posts_per_page' => 4,
'paged' => $paged
);
$blog_post = get_posts( $blog );
foreach ($blog_post as $post) : setup_postdata( $post );
$blog_img = $post->ID;
$blog_title = $post->post_title;
$blog_para = $post->post_content;
$blog_trimed=wp_trim_words($blog_para, 45);
$blog_name = get_post_meta($post->ID, 'author name', true);
?>
<li>
<div class="blog-content-left">
<?php echo get_the_post_thumbnail($blog_img, array(420,250)); ?>
</div>
<div class="blog-content-right">
<?php echo $blog_title; ?>
<p class="blog-date-area"><span class="blog-date"><?php echo get_the_date('M d, Y'); ?></span><span class="blog-date blog-crcl"><?php echo $blog_name; ?></span></p>
<div class="blog-para">
<p><?php echo $blog_trimed; ?></p>
</div>
<span class="read-more-btn-blue">
Read More
</span>
</div>
</li>
<?php endforeach;?>
</ul>
<?php echo wp_paginate(); ?>
Thanks for help in advance.
I didn't find any solutions, so I used pagednavi.

Displaying All Data from Custom Post Type Fields through Advanced Custom Fields

I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.
<?php get_header(); ?>
<?php
$args = array(
'posts_per_page' => 30,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'attorneys'
);
query_posts($args);
if ( have_posts() ) :
?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="attorney-preview">
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />
<p><strong><?php echo get_post_meta($post->ID,'attorney_name', true); ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php get_footer(); ?>
When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />
Try this
<?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));
if (have_posts()) : while (have_posts()) : the_post();
$attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);
$attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>
<div class="attorney-preview">
<img src="<?php echo $attorney_photo; ?>" />
<p><strong><?php echo $attorney_name; ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Osclass search from homepage to search for location also

I am trying to configure osclass for a real estate website. when searching from the home page, i need the search keywords to be searched in city and region too... ie, if the search key is "apartment newyork", all apartments where city or region is newyork should be displayed.
for the city filter try this one
<?php $aCities = City::newInstance()->listAll(); ?>
<?php if(count($aCities) > 0 ) { ?>
<select name="sCity" id="sCity">
<option value=""><?php _e('Select a city...')?></option>
<?php foreach($aCities as $city) { ?>
<option value="<?php echo $city['s_name'] ; ?>">
<?php echo $city['s_name'] ; ?></option>
<?php } ?>

Kohana "has many through" relation

I was wondering what the best method is to edit a 'has many through' relation with a form.
Let's say I have a bunch of users that can belong to multiple categories.
The form would have some checkboxes like this:
<input type="checkbox" name="category_ids" value="1" />
<input type="checkbox" name="category_ids" value="2" />
Then in my controller I could do something like:
// dump all relations
DB::delete('users_categories')->where('user_id','=',$user->id)->execute();
// add new relations
foreach (explode(',', $_POST['category_ids']) as $category)
$user->add('category', ORM::factory('category', $category))
But this looks too complicated to me (also because I have more than one 'has many through' relations). Is there an easier / better way to accomplish this using kohana orm? :)
thats how i do it
// C
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role)
{
$action = isset($form['user']['roles'][$role->id]) ? 'add' : 'remove';
// you dont need this if-statement if you'r using ko2
if ($action === 'add' && $user->has('roles', $role))
{
continue;
}
$user->$action('roles', $role);
}
// V
<?
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role):
?>
<?= form::checkbox('user[roles]['.$role->id.']', $role->id, $user->has('roles', $role)) ?>
<?= form::label('user_roles_'.$role->id, $role->name) ?>
<br />
<? endforeach ?>
To find what was added (reverse the args to find what was removed) consider using array_diff().
With this you should be able to code something more efficient than pure orm.

Resources