list:reference painfull to fill

127 views
Skip to first unread message

BlueShadow

unread,
Dec 9, 2013, 9:57:36 AM12/9/13
to web...@googlegroups.com
Hi,
If I had two tables one called ingredience and the other soup. It is quite painfull to put 20 ingredience in one soup. Is there any more convenient way? It is sopposed to work with a form for users to create more entries. So punching in the ids is not an option.

Richard Vézina

unread,
Dec 9, 2013, 10:02:37 AM12/9/13
to web2py-users
You need to have control over the ingredient order no? You need something like BSM selet : https://github.com/vicb/bsmSelect

You will need also a piece of python to restore the order of the list elements you get from the DB...

But I am not sure of your needs... For you issue that you have to pick 20 ingredience, may you can have a kind of recipe default that get you the 10 first commons ingredient of a soup for example...

Richard




On Mon, Dec 9, 2013 at 9:57 AM, BlueShadow <kevin....@gmail.com> wrote:
Hi,
If I had two tables one called ingredience and the other soup. It is quite painfull to put 20 ingredience in one soup. Is there any more convenient way? It is sopposed to work with a form for users to create more entries. So punching in the ids is not an option.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kevin Bethke

unread,
Dec 9, 2013, 10:30:51 AM12/9/13
to web...@googlegroups.com
that bsm select looks already way better than the standard w2p implementation.
My requirements are pick 0-max ingredience. All ingredience have to be in the ingredience table. no ingredient can be picket twice.
I don't need the default or most common 10 stuff. Just a simple way to pick quite a few out off a table with close to 500 entries (and rising).


You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/3HTTdoGnCAA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Richard Vézina

unread,
Dec 9, 2013, 10:35:42 AM12/9/13
to web2py-users
So you need a auto complete widget... Don't remember if bsmselect has one... I use to build my own bsm like widget with bootstrap multiselect and typeahead... To me what was important was to make sure order of picked items remains from input and update, so you can see the order of importance of the thing selected, like ingredient label on can...

:)

Richard

Kevin Bethke

unread,
Dec 9, 2013, 10:43:36 AM12/9/13
to web...@googlegroups.com
autocomplete would be nice. but anything is better than scrolling though a list showing only 5 entries holding shift for 25 ingredience and forget holding shift at the last one. I read "really quickly through the examples from bsmselect. that works with <ul> and <ol> lists? Does that work with web2py when this is part of a form with some more entries like name of the soup, instructions and preperation time

Richard Vézina

unread,
Dec 9, 2013, 10:47:15 AM12/9/13
to web2py-users
It a custom thing, you can do anything you need... Web2py as really little to do with that. But you will have to work around web2py default... BSM select is not going to work out of the box you will have to give it what it need to work : web2py custom widget that wrap BSM for instance.

Richard

Kevin Bethke

unread,
Dec 9, 2013, 10:49:10 AM12/9/13
to web...@googlegroups.com
has anyone done that before and give me a few hints? I'm not the master programmer^^

Richard Vézina

unread,
Dec 9, 2013, 11:09:03 AM12/9/13
to web2py-users
I can help you, but I will not write it for you :)

Richard

BlueShadow

unread,
Dec 12, 2013, 4:07:01 AM12/12/13
to web...@googlegroups.com
thanks for your offer I'm working my way through the example code but my biggest problem is that php site. I don't know php. But from what I get from the php code is that all the sample cities need to be in an unordered list environment <ul><li></li>...</ul>
but this form part of the bsmselect example really confuses me.
<h1>Example 1: Typical Usage</h1>
<form action="./example_results.php" method="post">


   
<label for="cities1">What are your favorite cities?</label>
   
<select id="cities1" multiple="multiple" name="cities[]" title="Click to Select a City" class="sminit">
     
<option>Amsterdam</option>
   
</select>
   
<p><input type="submit" name="submit" value="submit" /></p>
 
</form>

especially the php file in form action=""
<?php

if(!empty($_POST['submit'])) {

  echo
"<html>\n<body style='width: 400px; margin: 2em auto; font-family: Arial;'>";

 
if(!empty($_POST['cities'])) {

    echo
"\n<p><strong>You selected the following cities:</strong></p>\n<ul>";

   
foreach($_POST['cities'] as $city) {

     
// exclude any items with chars we don't want, just in case someone is playing
     
if(!preg_match('/^[-A-Z0-9\., ]+$/iD', $city)) continue;

     
// print the city
      echo
"\n\t<li>" . htmlspecialchars($city) . "</li>";
   
}

    echo
"\n</ul>";

 
} else {
    echo
"\n<p>No items selected</p>";
 
}

  echo
"\n<p><a href='index.html'>Try Again?</a></p>";

  echo
"\n</body>\n</html>";

} else {
 
// if someone arrived here not having started at example.html
 
// then show example.html instead
 
require("index.html");

}


It seems to me that there is an entire html document in this php file.

Richard Vézina

unread,
Dec 12, 2013, 9:48:36 AM12/12/13
to web2py-users
Don't bother with the php thing...

Just look the data structure of the select... 

Here what I do to restore proper order on update form :

controller_view_js += """
var ordered_values = ''%s''; 
$(document).ready(function() { 
    if(ordered_values != "None") { 
        $.each(ordered_values, function(i, val) { 
            $("select[name=field_name]").append("<option value=\'"+ordered_values[i]+"\'>"+
                $("select[name=field_name] option[value="+ordered_values[i]+"]").text()+"</option>"); 
        }); 
        $("select[name=field_name] option:selected").remove(); 
        $.each(ordered_values, function(i, val) { 
            $("select[name=field_name] option[value="+ordered_values[i]+"]").attr("selected", "selected"); 
        }); 
    }; 
});""" % [int(ID) for ID in ordered_values_query]

controller_view_js is a variable containing a piece of jquery that I pass to the view from the controller... What it does put the options into the proper order or how they were ordered at the input... All this is because web2py don't use "ol" list out of the box... So I manage thing to not require ol.

The ordered_values_query contain the value of the field field_name for the given record...

I had to set a representation like this too :

represent=lambda values, row: ', '.join(map(lambda id: db.ref_referenced_table(id).represent_field, values)) if values != [] or None else T('N/A')

Hope it helps.

Richard


Richard Vézina

unread,
Dec 12, 2013, 9:49:23 AM12/12/13
to web2py-users
It's a bit hacky but it works...

:)

Richard

Kevin Bethke

unread,
Dec 13, 2013, 7:27:18 AM12/13/13
to web...@googlegroups.com
thanks but to be honest now I'm even more confused.
why do you need a piece of jquery code in a javascript variable. I sopose you included the entire jquery library.
Why do you do the ordering with javascript and not with python is there a special reason for this? I think I just explained the last question  by looking at the code again. The document ready function is executed every time you add or remove an element from the list?
the javascript variable ordered_values="%s" is filled from the represent python variable?
And my last question do you implement the multiple select into a form?
Sry for all those questions but I'm still learning and the need for this to work really pushes my skills and knowledge beyond its limits.


You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/3HTTdoGnCAA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Richard Vézina

unread,
Dec 13, 2013, 9:53:19 AM12/13/13
to web2py-users
I am sorry about that...

Web2py default behavior for list:reference or reference is a HTML Select and option... This is ordered by the navigator base on the spelling of the element... So when you edit your form if you want to maintain the preexisting order (the one that were there when you select items and order them with bsm) you need to set them in order in the select yourself... Bsmselect use OL that is Ordered List instead of UL... But it consume a standard select... So, as long as he get a select with the proper ordered options he can recreate the prexisting order... That is what the js I show you does...

ordered_values content the list:reference or the reference value that are a simple python list... List are items of list are ordered not like python dict that not preserve order... So I just get the value of the field that is a list object and pass it to the javascript as a list object... then I create option from that list in javascript like you could do in web2py with helpers for instance or by generating html yourself...

Hope it helps.

Richard

Kevin Bethke

unread,
Dec 13, 2013, 10:18:46 AM12/13/13
to web...@googlegroups.com
Thanks it explains a lot. I will try to work on it as soon as I got the time. Unfortunatly we got a deadline at work which is Christmas. So I will probably not work on it till than.

Kevin Bethke

unread,
Dec 13, 2013, 10:20:55 AM12/13/13
to web...@googlegroups.com
Well actually I got one more question: How do you change the standard form from web2py, to use the bsmselect form?

Richard Vézina

unread,
Dec 13, 2013, 10:54:48 AM12/13/13
to web2py-users
You init the js on the field!!

Richard

Jesse Ferguson

unread,
Dec 13, 2013, 6:38:23 PM12/13/13
to web...@googlegroups.com
sorry to hop in and ask a silly question but, when you say init the js on the field how is that supposed to be done in web2py? Is it done in the view? would it be ideal to put the js in the template and then pass params? 
any named optional attribute is passed to the <form> tag
for example _class, _id, _style, _action, _method, etc.

sorry if this doesn't make much sense I'm sure I am just missing the java knowledge I'm pretty new to programming and am learning through trial and error...

 

Richard Vézina

unread,
Dec 14, 2013, 3:17:03 PM12/14/13
to web2py-users
In the view something like that :

$( document ).ready(function() {
    $('input#id').bsmselect(...);
});

It can be pass to the view from the controller, as you want... So you will need to do something like that :

{{=XML(your_pass_js_script)}}

:)

Richard

Richard Vézina

unread,
Dec 14, 2013, 3:20:08 PM12/14/13
to web2py-users
I forget... Don' forget to surround with <script></script> tag...

<script>
{{=XML(your_pass_js_script)}}
</script>
Reply all
Reply to author
Forward
0 new messages