I'm try'n to create a custom form field of type text (or list) where a user can a) type free text and/or b) select from drop-down. Now I found many posts about autocomplete or auto-fill, but that's not what I'm after.
I followed the example on how to create a 'City' Custom field here http://docs.joomla.org/J2.5:How_to_add_custom_filters_to_components and this is all working.
However, it creates a drop-down only, no option to enter text.
I'm new to the Joomla (3.x) component development, so maybe I am missing something very simple here. With all those field types available, it's hard to belief there is no drop-down with free input.
So
1. can I make the default select/list to accept free text?
2. if not, can I get a pointer on how to get started making one my self?
3. For now, it would be fine to have ~10 city names listed, and free input, no need to filter the city list while typing. But ultimately, I would like to know on how to create a filter while typing Ajax version of this. (Like a suggest input-box)
This is what I use at the moment, the example as linked above
I also tried extending Jformfield, with no luck
class JFormFieldCftCity extends JFormFieldList {
protected $type = 'CftCity';
public function getOptions() {
$options = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('value As value, name As text');
$query->from('#__bitLuCity AS a');
$query->order('a.sortOrder');
$query->where('isEnabled = 1');
$db->setQuery($query);
$options = $db->loadObjectList();
return $options;
}
}
Thanks
Regards
Andreas
For anyone in the same position and future reference I'm going to post my own solution here as I ended up not using #isherwood's suggestion. I have not yet figured out completely how to integrate 'select2' into my component, nor is it needed for simple 'combobox' behavior.
This is HTML5 only, no additional script's, extends a plain JFormField.
It will allow free input, as well as select from the static list and filters while typing.
class JFormFieldCftCity extends JFormField {
protected $type = 'CftCity';
public function getInput() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('value As value, name As text');
....
$db->setQuery($query);
$rows = $db->loadObjectList();
$control= '<input id="' . $this->id . '" name="' . $this->name
. '" list="dataSrc-'. $this->id .'" value="' . $this->value
. '" type="text" class="inputbox" />'
. '<datalist id="dataSrc-'. $this->id .'">';
for ($i = 0; $i < count($rows); $i++) {
$control .= "<option value='{$rows[$i]->text}'>{$rows[$i]->text}/option>";
}
$control .= '</datalist>';
return $control;
}
It did not work in Safari, but I finally found a solution that works in all browsers, until datalist HTML5 markup is supported by all browsers:
<input type="text" class="span6 " style="margin: 0 auto;" data-provide="typeahead" data-items="4" data-source="["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]" />
You can of course fill "data-source" with data from your query.
I'm doin a simple query linq to retrieve a label from an optionSet. Looks like the formatted value for the option set is missing. Someone knows why is not getting generated?
Best Regards
Sorry for the unclear post. I discovered the problem, and the reason of the missing key as formattedvalue.
The issue is with the way you retrieve the property. With this query:
var invoiceDetails = from d in xrmService.InvoiceSet
where d.InvoiceId.Value.Equals(invId)
select new
{
name = d.Name,
paymenttermscode = d.PaymentTermsCode
}
I was retrieving the correct int value for the option set, but what i needed was only the text. I changed the query this way:
var invoiceDetails = from d in xrmService.InvoiceSet
where d.InvoiceId.Value.Equals(invId)
select new
{
name = d.Name,
paymenttermscode = d.FormattedValues["paymenttermscode"]
}
In this case I had an error stating that the key was not present. After many attempts, i tried to pass both the key value and the option set text, and that attempt worked just fine.
var invoiceDetails = from d in xrmService.InvoiceSet
where d.InvoiceId.Value.Equals(invId)
select new
{
name = d.Name,
paymenttermscode = d.PaymentTermsCode,
paymenttermscodeValue = d.FormattedValues["paymenttermscode"]
}
My guess is that to retrieve the correct text associated to that option set, in that specific entity, you need to retrieve the int value too.
I hope this will be helpful.
Best Regards
You're question is rather confusing for a couple reasons. I'm going to assume that what you mean when you say you're trying to "retrieve a label from an OptionSet" is that you're attempting to get the Text Value of a particular OptionSetValue and you're not querying the OptionSetMetadata directly to retrieve the actual LocalizedLabels text value. I'm also assuming "formatted value for the option set is missing" is referring to the FormattedValues collection. If these assumptions are correct, I refer you to this: CRM 2011 - Retrieving FormattedValues from joined entity
The option set metadata has to be queried.
Here is an extension method that I wrote:
public static class OrganizationServiceHelper
{
public static string GetOptionSetLabel(this IOrganizationService service, string optionSetName, int optionSetValue)
{
RetrieveOptionSetRequest retrieve = new RetrieveOptionSetRequest
{
Name = optionSetName
};
try
{
RetrieveOptionSetResponse response = (RetrieveOptionSetResponse)service.Execute(retrieve);
OptionSetMetadata metaData = (OptionSetMetadata)response.OptionSetMetadata;
return metaData.Options
.Where(o => o.Value == optionSetValue)
.Select(o => o.Label.UserLocalizedLabel.Label)
.FirstOrDefault();
}
catch { }
return null;
}
}
RetrieveOptionSetRequest and RetrieveOptionSetResponse are on Microsoft.Xrm.Sdk.Messages.
Call it like this:
string label = service.GetOptionSetLabel("wim_continent", 102730000);
If you are going to be querying the same option set multiple times, I recommend that you write a method that returns the OptionSetMetadata instead of the label; then query the OptionSetMetadata locally. Calling the above extension method multiple times will result in the same query being executed over and over.
Started using code igniter recently and been trying to make a page that filters events by certain parameters .I have the view page that contains an input type search (searches keywords in database)and a drop down filter list that filters by name of event,location and pricing.however I need help in querying the search results from database and displaying it on my view page .Anyone with an idea? thanks in advance
when you are submitting a form submit it to the controller method and get the post value in controller method
function search()
{
$keyword = $this->input->post('keyword');
$this->load->model('mymodel');
$result = $this->mymodel->getSearchResults($keyword);
$data['results'] = $result;
$this->load->view('search_results',$data);
}
And in your model
function getSearchResults($keyword){
$this->db->like('column_name',$keyword,'after');
return $this->db->get('tablename')->result();
// or you can write query simple way instead of Active Record
// $query = "SELECT BLah blah FROM mytable WHERE column_name like '$keyword%'";
// return $this->db->query($query)->result();
}
Could someone point me to an example of a Couchapp application that queries a view with a key based on user input from an HTML form?
I can't seem to either figure out how to do this or find an example on my own via google.
I figured it out:
If you're using evently, then in /evently/[whatever]/_changes, instead of a fixed query.json, you can replace it with a query.js where you can dynamically return a query, e.g.:
function() {
var key = /* ... */
// get your key from the appropriate source (jquery, current page's URL, etc)
return {
"view" : "recent-items",
"key" : key,
"limit" : 50
}
}
I managed to pull out data using autocomplete at my local (http://mysite.dev/swan/autocomplete). The json data is displayed.
But when I applied the same module at live (now a subdomain: http://test.mysite.com/swan/autocomplete with different drupal installs), this autocomplete fails to pull out data. No json data is displayed.
Do you have any idea if this is related to cross domain issue, or any possible cause I might not be aware of?
This is the callback:
/**
* Callback to allow autocomplete of organisation profile text fields.
*/
function swan_autocomplete($string) {
$matches = array();
$result = db_query("SELECT nid, title FROM {node} WHERE status = 1 AND type='organisation' AND title LIKE LOWER ('%s%%')", $string, 0, 40);
while ($obj = db_fetch_object($result)) {
$title = check_plain($obj->title);
//$matches[$obj->nid] = $title;
$matches[$title] = $title;
}
//drupal_json($matches); // fails at safari for first timers
print drupal_to_js($matches);
exit();
}
Any hint would be very much appreciated.
Thanks
It's the conflict with password_policy.module. Other similar modules just do the same blocking. These modules stop any autocomplete query.