question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Separate custom markup for results and final value

See original GitHub issue

I have a Python autocomplete view returning custom markup. (I need to disambiguate possibly duplicate names using email addresses so I need the name and the email in the search results.)

class MyAutocompleteView(autocomplete.Select2QuerySetView):
    def get_result_label(self, item):
        return f"""
            <div class="modelchoice-search-result">
                <div class="modelchoice-search-name">{item.name}</div>
                <div class="modelchoice-search-email">{item.email}</div>
            </div>
        """

This ultimately generates the html below inside <span class="select2 select2-container> (I’ve omitted a few containers/levels for clarity).

<span class="select2-selection__rendered" id="select2-id_form-13-child-container" title="
    <div class=&quot;modelchoice-search-result&quot;>
        <div class=&quot;modelchoice-search-name&quot;>Firstname Lastname</div>
        <div class=&quot;modelchoice-search-email&quot;>email@example.com</div>
    </div>
"><span class="select2-selection__clear">×</span><span>
    <div class="modelchoice-search-result">
        <div class="modelchoice-search-name">Firstname Lastname</div>
        <div class="modelchoice-search-email">email@example.com</div>
    </div>
</span></span>

So, it looks like the markup template is used for both the display of the results and also as the value that is added to the widget upon selection. That’s fine, I guess that makes sense as much as not. But is there any way I can separate out the display of the results from the final value that is added to the field? In this case, for example, I want to omit the email and be left just with the name.

I’ve tried just deleting that DOM node and other hacks, I’ve tried those by attaching to both the change event and also select2’s select2:close event, etc. But there seems to be some DOM manipulation happening that I’m not able to override and so I’m always ultimately left with the full template being added to the box. Any suggestions would be much appreciated, I’d prefer not to have to fork and hack the template logic in autocomplete_light/select2.js.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gagarskicommented, May 10, 2017

@jhrr you can do it with Select2 but unfortunately you’ll have to hack autocomplete_light/select2.js. Select2 initalization has two parameter functions: templateResult and templateSelection , one for rendering choices in the dropdown and one to render the selected choice. Unfortunately DAL currently does not allow to use different templating functions for templateResult and templateSelection

0reactions
jhrrcommented, May 11, 2017

Ah great 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Markup & Markdown Formulas & Percentages with Examples
Markup prices can be defined as the increase (by percentage) in the price of a product based on its original cost. Markdown prices...
Read more >
Markup Calculator - FreshBooks
Should you markup your prices? Try the free Markup Calculator to determine how much you should charge. Plus, calculate your profit margins, total...
Read more >
Cost-Plus Pricing: What It Is & When to Use It - HubSpot Blog
Cost-plus pricing can be used as part of the company's value ... Then multiply the final result by 100 to get the markup...
Read more >
Schema Markup - The ONLY Guide You Need To Read in 2022
Rank Math's Schema Markup helped to get the Rich Result. ... Creating Reusable Custom Schema Templates; Validating Schema – Does It Actually Work?...
Read more >
Handbook Markdown Guide - GitLab
The last markup {: .custom-class #custom-id} can be used in case you want to attribute a custom class and/or a custom ID to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found