Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2014-03-12 14:29:40

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Filtering categories based on the chosen section (back end)

Try .val() instead of .text(). Unless your section titles exactly match your category names, they won’t match. That’s what the plugin I posted above does and it works.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#14 2014-03-12 14:40:58

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Filtering categories based on the chosen section (back end)

Bloke wrote #279677:

Done

Thanks! Once released, this will allow for

if (@txpinterface == 'admin') {
	register_callback('abc_cat_filter','article');
}

function abc_cat_filter() {
echo n.script_js('$(function() {
	var cats = $("#category-1"), catsClone = cats.clone().removeAttr("id");

	$("#section").change(function() {
		var selected = $(":selected", this).val();
		cats.empty().append(catsClone.children("option[value=\'\']").clone());
		if(selected) catsClone.children("option[value=\'"+selected+"\']").each(function(){
			$(this).nextUntil("[data-level=\'"+$(this).attr("data-level")+"\']").clone().appendTo(cats);
		});
	}).change();
});');
}

Edit: and replacing "#category-1" with "#category-2" and "#section" with "#category-1", will restrict Category 2 choices to the children of Category 1.

Edit: replaced hide() with clone(), to satisfy all browsers.

Last edited by etc (2014-03-13 14:31:14)

Offline

#15 2014-03-12 14:52:22

a7aylor
New Member
From: Manchester, UK
Registered: 2014-03-11
Posts: 9

Re: Filtering categories based on the chosen section (back end)

Bloke wrote #279688:

in case you only want the filtering to take place on certain sections.

That would be ace, thanks.

Amateur plugin question (current experience is paste and click upload :-) – How do I get your PHP code as a BASE64-encoded piece of text to paste in on the plugin page?

Offline

#16 2014-03-12 15:02:44

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Filtering categories based on the chosen section (back end)

a7aylor wrote #279690:

Amateur plugin question (current experience is paste and click upload :-) – How do I get your PHP code as a BASE64-encoded piece of text to paste in on the plugin page?

Plugin Composer is the easiest option.

Offline

#17 2014-03-12 15:08:04

a7aylor
New Member
From: Manchester, UK
Registered: 2014-03-11
Posts: 9

Re: Filtering categories based on the chosen section (back end)

Bloke wrote #279688:

Try .val() instead of .text(). Unless your section titles exactly match your category names, they won’t match. That’s what the plugin I posted above does and it works.

No joy. The match is with the names, not the titles.

Offline

#18 2014-03-12 15:13:46

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Filtering categories based on the chosen section (back end)

a7aylor wrote #279692:

No joy. The match is with the names, not the titles.

Hmm. Take a look at the page’s HTML source code and check the <option> value attributes. The aim is to match the category with the section somehow, bearing in mind Txp will sanitize both section and catgeory titles because they form part of the URL. So, for example, “Human Sciences” would become “human-sciences” in both cases.

The .val() gets the option value names whereas .text() gets the (screen-visible) title of the option so you may need to mix and match them to get the desired outcome depending on how you’ve set them up.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#19 2014-03-12 15:35:40

a7aylor
New Member
From: Manchester, UK
Registered: 2014-03-11
Posts: 9

Re: Filtering categories based on the chosen section (back end)

Ok so, heres the sections…

<option value="biology">biology</option>
<option value="chemistry">chemistry</option>
<option value="french">french</option>
<option value="ict">ict</option>
<option value="inclusion">inclusion</option>
<option value="physics">physics</option>
<option value="spanish">spanish</option>
<option value="test">test</option>

Heres are some of the categories…

<option value="biology">Biology</option>
<option value="b1">&#160;&#160;B1</option>
<option value="b2">&#160;&#160;B2</option>
<option value="b3">&#160;&#160;B3</option>
<option value="biology-keywords">&#160;&#160;Keywords</option>
<option value="isa">&#160;&#160;ISA</option>
<option value="chemistry">Chemistry</option>
<option value="c1">&#160;&#160;C1</option>
<option value="c2">&#160;&#160;C2</option>
<option value="c3">&#160;&#160;C3</option>
<option value="chemistry-keywords">&#160;&#160;Keywords</option>

Option value does match e.g. biology in sections is also biology(parent) in categories

{Edited to add Textile’s bc. for readability. – Uli}

Last edited by uli (2014-03-12 16:07:16)

Offline

#20 2014-03-12 15:46:25

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Filtering categories based on the chosen section (back end)

Waou!
How to a member problem become a plugin. Great community !

I’m just a question to bloke and etc plugin code :
Wath the difference since

echo script_js(<<<EOJS
   Your JS code
EOJS);

and

echo n.script_js('Js code');

to inject JS code into a page ?

Offline

#21 2014-03-12 16:30:20

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: Filtering categories based on the chosen section (back end)

From the browsers I tried, the JS only worked on FF (27 Mac). Failed on Chromium, SAF & OP. Unfortunately I can’t help with that sort of troubleshooting.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#22 2014-03-12 17:07:43

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Filtering categories based on the chosen section (back end)

Hohooo,
display:none don’t work for Chrome and IE for option.
This work only with Firefox…

stackoverflow.com/questions/9234830/how-to-hide-a-option-in-a-select-menu-with-css

Last edited by sacripant (2014-03-12 17:14:46)

Offline

#23 2014-03-12 17:21:01

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Filtering categories based on the chosen section (back end)

sacripant wrote #279696:

Waou!
How to a member problem become a plugin. Great community !

You said it, I remembered it but I don’t know if it still works: bot_write_tab_customize


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#24 2014-03-12 19:37:31

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Filtering categories based on the chosen section (back end)

sacripant wrote #279696:

Wath the difference since echo script_js(<<<EOJS... and echo n.script_js('Js code');... to inject JS code into a page ?

Stef is using heredoc syntax, me poor-man quotes.

Offline

Board footer

Powered by FluxBB