Creating a dropdown list of fields to sort by

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
davidfells81@gmail.com's picture

I am currently rebuilding a site in Drupal, working on this page.

Original Page

New Page

It's complete, except for the dropdown box for the sort option. I have NO idea how to do this. I've been researching it for a while and have found miscellaneous tidbits that seem like they would be useful, but nothing that really gets me going in the right direction. For example, i found this post that looks like roughly what I need, though I have no idea where the code should go :)

I don't actually need to handle ascending/descending sort in this case, as it's always ascending, I just need to take an argument for the sort by field and apply that to the view.

Comments

Hi, I'm looking at

hthstnrd's picture

Hi,

I'm looking at implementing the same functionality on a site - sort-by dropdown box. How did you end up going about this - looks as if you've created separate views for each type of sort? If this is the case, how did you go about adding the dropdown select to each view?

Cheers,

Heath

Found a solution

socratesone's picture

I came across this post while researching the same issue.

What I did was use views and the Dropdown tabs module (http://drupal.org/project/dropdown_tabs)

In views, create several displays, each of which is sorted differently. Make them page displays. For each page display, create a tab.

Do this by Selecting "Menu Tab" under "Menu" in the "Page settings" of the view.

Set the path to the path of your main page, followed by the sort option, for instance:
page_name for the main, unsorted page display
page_name/sort_type_1 for the first sort option
page_name/sort_type_2 for the second
Set one of the sort options to "default menu tab"

Then, when you view your page, you will see tabs. Clicking on a tab will sort by the selected criteria.

Then, install the dropdown tabs module.

Add the dropdown tabs block on the block page to whereever you'd like to set the drop-down.

You may need to use some css to position the block correctly and hide the tabs.

That did it for me. I hope that helps anyone else.

A big thank you to Collective Colors for creating this module. If you hadn't, I would have had to create it myself.

Thanks for the solution!! I

aac's picture

Thanks for the solution!! I may use it in my project.

---~~~***~~~---
aac

Or you can try Views 3 -

dagmar's picture

Or you can try Views 3 - alpha 2, which includes exposed sorts.

Mariano D'Agostino
http://cuencodigital.com

Auto add taxonomy terms?!

roscojohn's picture

Hi,

I'm fairly new to Drupal and VERY addicted! My wife is already worrying!

Anyway, I am also trying to add a sort dropdown menu but would like to automatically populate the options with terms from a taxonomy vocabulary (as this will change over time). I have a show results page in views whose URL takes the taxonomy term as an argument and shows the appropriate results (which worls nicely with a manually added value). Can anyone offer any advice as how to do this.

Cheers
John

I just created a block

hthstnrd's picture

Thanks Mariano, I ended up trying Views 3 and it does the job nicely.

John - I needed the same sort of functionality, a select list consisting of only top-level taxonomy terms from a certain vocabulary. I basically used taxonomy_get_children to get those terms and javascript to both show and submit the form.

The resulting page is a view using the taxonomy term ID as an argument in order to filter the list.

<?php

global $base_url;

$top_level = taxonomy_get_children(0, 2);

if (!empty(
$top_level)): ?>

  <form id="category-choice">
    <span>CATEGORY:</span>
    <select id="category-chooser" name="category-chooser">
      <option value="">-- Select --</option>
      <?php foreach ($top_level as $term) { ?>
        <option value="<?php print $base_url; ?>/resources/knowledge-bank/category/<?php print $term->tid; ?>"<?php if (is_numeric(arg(3)) && arg(3) == $term->tid) { print ' selected="selected"'; } ?>><?php print $term->name; ?></option>
      <?php } ?>
    </select>
  </form>

<?php endif; ?>

Can you help with the javascript.

roscojohn's picture

Hi,

Thanks so much for the help and code.

Do you know of a way to send this as an autoload function in javascript?

I have not been able to get any added javascript to work as yet on my site. I copied some javascript code to a script.js file in my themes folder as i was advised to do for a previous attempt, and added scripts[] = my_scripts.js to my theme .info file to pick up the script but this would not work. I was OK when just working in php before delving into Drupal!
Can you offer me a dummies guide as to what code needs to go where to send the above code correctly?

Thanks again
John

Sounds correct

hthstnrd's picture

What you're doing sounds correct - this comment outlines the process: http://drupal.org/node/304255#comment-1165624

Have you checked your source to see if the javascript is being called and that it's not just the javascript itself not working?

A couple of issues..again!

roscojohn's picture

Hi,
Thanks again.

Is there any possibility you could post 2 pieces of code:

1 The correct format for the submit button for javascript to read it.
2. The javascript to put in my script file to process it.
(or the code for an autoreload function)

I have just started with javascript so you need to bear with me. A real idiots guide is needed!

John

i'm not a javascript expert by any means but..

hthstnrd's picture

Hi John,

I actually don't use a submit button on the form. The form isn't shown to users without javascript enabled and is never actually 'submitted'. It simply directs the user to the URL value of the item they choose in the select list with the help of jquery:

$(document).ready(function(){
  $("#form-id").show(); //Show the form (by default it is hidden with CSS)
  $("#id-of-select-field").change(function(){ //When a user changes the select field value
    var url
    url = $("#id-of-select-field").val(); //Get value of the option they've selected
    if (url != ''){ //If not the first option (-- Select --)
      window.location = url; //Send user to URL
    }
  });
});

If you wanted to actually submit the form instead you could try something like:

$(document).ready(function(){
  $("#form-id").show();
  $("#id-of-select-field").change(function(){
    $("#form-id").submit()
  });
});

If you're using a submit button you may like to hide it with javascript too..

More than one item from the Drop Down List

ss54's picture

I created a content type and I also introduced a Taxonomy with 4 items. After I began using this content type I discovered that users can select more than one item from the taxonomy list which is not good for me. Is there any way that we can prevent users from selecting more than one term from the list. I would be so grateful for any help.

Hi, Just edit your taxonomy

hthstnrd's picture

Hi,

Just edit your taxonomy vocabulary and make sure that 'tags' and 'multiple select' aren't selected and your users shouldn't be able to select multiple terms.

Thanks a lot hthstnrd

ss54's picture

Thanks a lot, I wonder why I was so stupid that I had not known. Blessings

May I ask a question about node/content formatting

ss54's picture

I note that when someone creates a content type and add fields like, for example, (Site Visit Date). It seems that this content will be only in black. How about if one wants to give the date a different color say red for example or highlight. Is there any way to manipulate the format of the content of the field? much thanks for any help

Hi sarmad, You should just be

hthstnrd's picture

Hi sarmad,

You should just be able to use CSS for this..

.field-name {
  color: #FF0000;
}

Have I understood your question?

I think you did understand- bunch of thanks

ss54's picture

But suppose the user who is filling the form does not know CSS, how do you think he/she gonna do it from the drupal user interface?
secondly, if I wanted to use your above code, and I have many fields likes
Solicitation No,
Project Title
Site Visit Date etc .
Should I replace the above class name (.field_name) with the actual field name - can you please clarify further. I have already used a good deal of css inside bodies of blocks but so far I have not used it for such situations.

What file inside Drupal does contain these CSS for the purpose of changing the color of the fields. I would be so thankful if you could answer these question for me. Blessings

I don't personally find

freeform.steph's picture

I don't personally find adding css inside block bodies to be good practice - add class or id names and add the styles to your theme's css file(s) in the appropriate place so that you can re-use style for multiple blocks.

If you use Firebug for firefox, you can navigate your html to find the field class names, then use those class names in your theme's css files, usually located in sites/all/themes/your_theme/. If using the latest zen theme, for instance, the css file would be sites/all/themes/your_theme/css/fields.css. (http://getfirebug.com/). Alternatively, the class name is usually field-field-FIELDNAME, where FIELDNAME is what you created when you added the cck field in your content type.

If you want to change anything for your theme's style, click on that item in firebug and it will tell you which file(s) that class can be found in. If you want to avoid changing anything from your theme, create a new css file in your theme's directory, call it something like theme_name.css (if it doesn't already exist), add a reference to it in theme_name.info, and clear your theme cache - this will be a fresh file for just your custom theming so that you can't accidentally delete/change anything from your theme's styling.

"But suppose the user who is filling the form does not know CSS, how do you think he/she gonna do it from the drupal user interface?"

Why would your user ever need to know css? If you're referring to a text area where you want them to be able to enter rich text, add tables, etc. than you should be installing a wysiwyg module (http://drupal.org/project/wysiwyg).

About Wysiwyg Daragonflyway

ss54's picture

Once I installed the module Wysiwyg and found that unless you also install an editor like FCKeditor you cannot utilize Wysiwyg- Is this correct?

Well, Views doesn't contain

merlinofchaos's picture

Well, Views doesn't contain any options to write CSS for you. You might have some luck with skinr or sweaver, but if this is one of your needs and use-cases, ultimately Views (and Drupal) may not be what you want.

Are you sure that

ss54's picture

Are you sure that Views do not give opportunity for including a css code? How about (rewrite the output of this field) doesn't accept any css coding? Thanks

While it's true that Views is

merlinofchaos's picture

While it's true that Views is a big module and I do forget its nooks and crannies from time to time, this would be a huge feature for me to forget about adding and then forgetting I added.

In addition, if you find

freeform.steph's picture

In addition, if you find there are not enough class options to style the way you want, in views, choose your field and select "Rewrite the output of this field" and add a containing div around it with a class of your choosing:

<div class="my_custom_class">[viewfieldname]</div>

Very Enlightening Remarks dragonflyway

ss54's picture

Dragonflyway: I cannot find proper words to thank you with. God's blessing.
BTW, I never thought that the field (rewrite the output of this field) could accept css code. So

this is great and I will give it a go.

"But suppose the user who is filling the form does not know CSS, how do you think he/she

gonna do it from the drupal user interface?"

For this I did not mean a free text area, I meant a text field. I think your idea for using

(rewrite the out of this field) is worth trying and smart, this is in addition to your preceding remarks.

Is it possible to create a test site from the Original

ss54's picture

I always wondered is it possible to create a duplicate of a Drupal website on another remote host server. I have searched the internet and tried myself to do it, but failed to find anything helpful. Can any one help in this regard, I would be most grateful.

edit your taxonomy vocabulary

nguoivietminh's picture

edit your taxonomy vocabulary and make sure that 'tags' and 'multiple select'

Thanks HI

ss54's picture

Thanks, but this question was already resolved

Creating a copy of a Drupal Site

ss54's picture

I have learned how to create a test copy Drupal site at another host, it is only a matter of an easy creation and tweaking of a new database, and also some changes on the settings.php file.

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week