Jump to content
suntrop

Sort by checkbox status

Recommended Posts

Hi all! I want to sort child-pages by two fields but I don't know if it is possible within one selector or if I have to make two separate queries

<?php foreach ($page->children('sort=checkbox, sort=-number') as $child): ?>

I want to keep 'checked' pages on top and sort them and all following by the 'number' field. The 'checkbox' field is a 

Share this post


Link to post
Share on other sites

Hi, Can you give us an example? I cannot seem to follow... Also, your post seems to be cut off at:

4 hours ago, suntrop said:

The 'checkbox' field is a 

 

Share this post


Link to post
Share on other sites

Nobody loves my riddles :-) 

It should read … "The 'checkbox' field is a default checkbox field, no special module"

<?php foreach ($page->children('sort=-chiffre') as $child): ?>
	<div class="job <?= $child->highlight ? 'hightlight' : 'default' ?>">Chiffre <?= $child->chiffre ?> | <?= $child-title ?><?= $child->body ?></div>
<?php endofreach ?>

… will render …

<div class="jobs">
	<div class="job">Chiffre 734 | Job Title – Job Description …</div>
	<div class="job">Chiffre 732 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 722 | Job Title – Job Description …</div>
	<div class="job">Chiffre 720 | Job Title – Job Description …</div>
	<div class="job">Chiffre 718 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 605 | Job Title – Job Description …</div>
</div>

But what I need …

<div class="jobs">
	<div class="job highlight">Chiffre 605 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 722 | Job Title – Job Description …</div>
	<div class="job">Chiffre 734 | Job Title – Job Description …</div>
	<div class="job">Chiffre 732 | Job Title – Job Description …</div>
	<div class="job">Chiffre 720 | Job Title – Job Description …</div>
	<div class="job">Chiffre 718 | Job Title – Job Description …</div>
</div>

… the pages with a checked "highlight" (the checkbox field) should be on top and ordered by the chiffre. The other pages should follow and ordered by chiffre too.

  • Like 1

Share this post


Link to post
Share on other sites

From the docs:

 

Quote

You may specify multiple fields to sort by. For example, lets say that you wanted to sort first by date descending (newest to oldest) and then by title ascending, you would do this:


sort=-date, sort=title
 

 

Share this post


Link to post
Share on other sites

I know. The problem is it doesn't seem to work with checkboxes.

Share this post


Link to post
Share on other sites

Hm, okay. Maybe then make two arrays and merge them / prepend the array with checkboxes?

<?php 
$withcheckbox = $page->children("checkboxname=1"); 
$others = $page->children("checkboxname=0, sort=-chiffre");
$all = $withcheckbox->import($others);

foreach($all as $a) {
..
?>

 

  • Like 1

Share this post


Link to post
Share on other sites

I thought it is possible within one selector … and I only have to rewrite my selector. 

My other solution looks better :-)

$page->children('highlight=1, sort=-chiffre')->add( $page->children('highlight=0, sort=-chiffre') )

 

Share this post


Link to post
Share on other sites

thats fine i think! ;) didn't had in mind ->add works for arrays

Quote

Add the given $item. Supports import of page arrays (since 2.2)

 

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, suntrop said:

The problem is it doesn't seem to work with checkboxes.

It's working for me. I assume the code in your first post is just for demonstration, but double-check that you are using the name of your checkbox field in the selector, i.e. "sort=highlight" and not "sort=checkbox". And you will want to reverse the sort for the checkbox to have checked items appear first: "sort=-highlight".

  • Like 1

Share this post


Link to post
Share on other sites

HI Robin. Thanks for checking. But I can't get this to work, double checked my wording. I am using PW 3.0.78. Perhaps we are using other "checkbox modules"? How can I tell what module this checkbox field is exactly? I am pretty sure I have not installed any module like this … but possibly I am wrong :) 

Share this post


Link to post
Share on other sites
14 hours ago, suntrop said:

Perhaps we are using other "checkbox modules"?

I tested with the core Checkbox fieldtype. You can check the fieldtype by opening the field (Setup > Fields > Your field) and looking in the Type dropdown.

2017-10-27_165014.png.1db54ac2ae2f14bacf32f7be27795b9b.png

Not sure why it wouldn't work for you, but there is no real harm in just sticking with what you are doing (combining two PageArrays together) unless you have a huge number of children. 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By opalepatrick
      Hi, trying to order pages by modified date, date. I can do 'sort=-date_modified, sort=-date' but that will always put the modified date first regardless of actual date order. I just want to coalesce the two fields and sort. Any ideas would be appreciated.
    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By louisstephens
      Is it possible to use count() to return a number of repeater items don't have a checkbox checked? In my current set up, I have a repeater on the page "dev_repeater" with a checkbox called "dev_repeater_exclude". I need to get a count of the current items that do not have it checked so I can pass it to my css grid to alter the column width.
    • By Chris Bennett
      Not sure where I originally saw it while lurking around the forums, but someone, somewhere at some time was asking about styling Uikit checkboxes as toggle-style switches, much like the ones at the bottom of this post asking me if I want to be notified of replies.
      So here is my humble offering, rough and ready,  which can be thrown in at the bottom of your Uikit css as a starting point.
      Everything is based on ems and rems, so you can increase size as you desire by altering the single instance of font-size.
      It only targets single instances of labels within a specific field to a) try to limit unintended consequences and b) because in those cases it often seems more user-friendly to have an on/off binary switch rather than a checkbox. It's still totally a checkbox, just styled differently.
       
      .uk-form-controls-text label:only-of-type input.uk-checkbox { font-size:.8rem; margin-top:0; position:relative; -webkit-appearance:none; outline:none; width:4em; height:2.4em; border:2px solid #D6D6D6; border-radius: 3em; box-shadow:inset 5em 0 0 0 #DDD; flex-shrink: 0; } .uk-form-controls-text label:only-of-type input.uk-checkbox:after { content:""; position:absolute; top:.25em; left:.25em; background:#FFF; width:1.6em; height:1.6em; border-radius:50%; transition:all 250ms ease 20ms; box-shadow:.05em .25em .5em rgba(0,0,0,0.2); } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked { background-color: transparent; box-shadow:inset 5em 0 0 0 #4ed164; border-color:#67bba5; } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked:after { left:1.85em; box-shadow:0 0 1em rgba(0,0,0,0.2); } label:only-of-type input.uk-checkbox:checked + span { color:#008a00; transition:all 250ms ease 20ms; } .InputfieldCheckbox .InputfieldContent label:only-of-type {display:flex;} label:only-of-type input.uk-checkbox + span { color:#c3c3c3; display:flex; align-items:center; line-height:1.1; } /* Below is only necessary if you want the optional "tick" after items when selected */ label:only-of-type input.uk-checkbox + span:after { flex-shrink:0; margin-left:.5em;width:2em; opacity:0; content:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 250 250'%3E%3Ccircle cx='125' cy='125' r='125' fill='%23231F20' opacity='.1'/%3E%3Cpath fill='%230B8B44' d='M95.823 139.432l-32.492-32.56-31.872 31.883-.008-.008 63.72 63.732L218.549 79.116 187.494 47.52z'/%3E%3C/svg%3E"); } label:only-of-type input.uk-checkbox:checked + span:after { opacity:1; transition: opacity 250ms ease 150ms; }  

    • By louisstephens
      I have done a bit of searching, but I can not seem to find an actual answer. I have a list of services as child pages under "Services". I can output the services just fine, but I cant wrap my head around how to group them "alphabetically" like:
      Services A - Service "A" 1 - Service "A" 2 - Service "A" 3 B - Service "B" 1 - Service "B" 2 - Service "B" 3 C - Service "C" 1 - Service "C" 2 - Service "C" 3 Has anyone achieved this type of functionality before?
×
×
  • Create New...