Filter not registering on first character - angularjs

I a user list that pops up when a user types # into a text field. When this happens I want the user list to be filtered for every letter typed after the # symbol. So if they typed "#s" all usernames starting with an "s" would show. I'm recording the text typed after the # and assigning it as the text to filter by, but the filter won't act until two characters have been typed. Can someone tell me why this would be happening? Thank you for your help.
HTML/SLIM:
/ USER LIST
.user-list.list-group [ng-show="usersShow"]
a.user.list-group-item [ng-repeat="user in users | filter:formattedUser"]
strong
| {{user.username}}
| {{' : ' + (user | fullName)}}
/ COMMENT FORM
= form_for Comment.new, html: {action: nil, name: 'newCommentForm', 'ng-submit' => 'createComment($event, comment)'} do |f|
= f.text_field :text, placeholder: 'Add comment...',
'ng-model' => 'comment.text', 'ng-change' => 'showUserList(comment.text)'
span.input-group-btn
button.btn type="submit" Add Comment
JS:
$scope.showUserList = function(comment) {
var USERNAME_PATTERN = /(?:^|\s)#\S*$/;
var comment = comment || '';
var userMatch = comment.match(USERNAME_PATTERN) || '';
if(userMatch) {
$scope.userFormatted = userMatch[0].replace('#', '').trim();
}
$scope.usersShow = USERNAME_PATTERN.test(comment);
};

Related

Override registration module & Hide or show input text drupal 7

I created a module and a hook to override the registration module form :
function custommodule_form_alter(&$form, $form_state, $form_id) {
// retrieve name & surname
global $user;
$user_fields = user_load($user->uid);
$name = $user_fields->field_name['und']['0']['value'];
$surname = $user_fields->field_surname['und']['0']['value'];
// var_dump($name); die();
// check the form_id
if($form_id=='registration_form'){
if( $form['who_is_registering']['#options']['registration_registrant_type_me'] =='Myself') {
$form['field_name']['und'][0]['value']['#default_value'] = $name;
$form['field_surname']['und'][0]['value']['#default_value'] = $surname;
} else {
$form['field_name']['und'][0]['value']['#default_value'] = '';
$form['field_surname']['und'][0]['value']['#default_value'] = '';
}
}
}
In the original module we can hide or display a field depending on the select value. For example if the select is positionned on "Myself", the user mail field is not visible.
I'd like to set the fields to empty if the select is positionned on "Myself" and to show empty fields otherwise.
Actually the name and surname are defined in the fields.

Drupal Custom Module HTML

So I've only just begun to learn Drupal so If you believe I'm going about this the wrong way, please let me know.
I have a content type called Events. I'm basically just trying to output a snippet of the latest event on the home page. To do this, I've created a custom module following the Drupal tutorial Drupal doc's custom module tutorial
Here's my module's code
<?php
/**
* Implements hook_block_info().
*/
function latest_event_block_info() {
$blocks['latest_event'] = array(
// The name that will appear in the block list.
'info' => t('Latest Event'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Custom content function.
*
* Set beginning and end dates, retrieve posts from database
* saved in that time period.
*
* #return
* A result set of the targeted posts.
*/
function latest_event_contents(){
//Get today's date.
$today = getdate();
//Calculate the date a week ago.
$start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
//Get all posts from one week ago to the present.
$end_time = time();
//Use Database API to retrieve current posts.
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('status', 1) // published == true
->propertyCondition('created', array($start_time, $end_time), 'BETWEEN')
->propertyOrderBy('created', 'DESC') //Most recent first.
->range(0, 1); //ony grab one item
return $query->execute();
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function latest_event_block_view($delta = '') {
switch ($delta) {
case 'latest_event':
$block['subject'] = t('Latest Event');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = latest_event_contents();
$nodes = array();
if (!empty($result['node'])) {
$nodes = node_load_multiple(array_keys($result['node']));
}
// Iterate over the resultset and generate html.
foreach ($nodes as $node) {
//var_dump($node->field_date);
$items[] = array(
'data' => '<p>
<span class="text-color">Next Event</span> ' .
$node->field_date['und'][0]['value'] . ' ' .
'</p>' .
'<p>' .
$node->title . ' ' .
$node->field_location['und'][0]['value'] . ' ' .
'</p>'
);
}
// No content in the last week.
if (empty($nodes)) {
$block['content'] = t('No events available.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
return $block;
}
}
I've added the block to a region, and it renders fine in my template page. However, the events are outputted in a list which is not what I want.
Here's how the block is being rendered in HTML
<div class="item-list">
<ul>
<li class="first last">
<p>
<span class="text-color">Next Event</span> June 23 2016 18:30 - 21:00 </p><p>Cancer Research UK Angel Building, 407 St John Street, London EC1V 4AD
</p>
</li>
</ul>
</div>
So assuming I'm going about this whole thing correctly, how can I modify this blocks html? Thanks!
I think first you need to understand the theme('item_list', ....). This always output a HTML list either UL or OL as given.
If you want to show your content without the HTML list wrapper, you could try this:
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function latest_event_block_view($delta = '') {
switch ($delta) {
case 'latest_event':
$block['subject'] = t('Latest Event');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = latest_event_contents();
$nodes = array();
if (!empty($result['node'])) {
$nodes = node_load_multiple(array_keys($result['node']));
}
// Iterate over the resultset and generate html.
$output = '';
foreach ($nodes as $node) {
//var_dump($node->field_date);
$output .= '<p>
<span class="text-color">Next Event</span> ' .
$node->field_date['und'][0]['value'] . ' ' .
'</p>' .
'<p>' .
$node->title . ' ' .
$node->field_location['und'][0]['value'] . ' ' .
'</p>';
}
// No content in the last week.
if (empty($output)) {
$block['content'] = t('No events available.');
}
else {
// Pass data through theme function.
$block['content'] = $output;
}
}
return $block;
}
}
That is one way. Another way is to use your own custom them template and use the array to output through that. For eg.
// Pass data to template through theme function.
$block['content'] = theme('latest_event_block_template', $items);
Then define a hook_theme function to get this to a template, like:
function latest_event_theme() {
return array(
'latest_event_block_template' => array(
'arguments' => array('items' => NULL),
'template' => 'latest-event-block-template',
),
);
}
Now, you should have a template at the module's root directory with the name latest-event-block-template.tpl.php. On this template you will be able to get the $items array and adjust the HTML yourself. Don't forget to clear theme registry after creating the template.
Hope it helps!
It's outputting as a list because you are passing $block['content'] the item_list theme function.
What you could do instead is create your own custom theme template using hook_theme. This will let you use custom markup in a custom template file.
After, replace this:
// Pass data through theme function.
$block['content'] = theme('item_list', array('items' => $items));
With this:
// Pass data through theme function.
$block['content'] = theme('my_custom_theme', array('items' => $items));

Search data in mongodb according to field values

I am using mongodb and node.js. In my database i have 6 fields that is
bid
color (Array)
size (Array)
cat_id
sub_cat_id
All is working fine. Now i want to add filter in my code. In filter area i have add this all fields. user select multiple colors and sizes so it will come in Array format but most of the time user will not select color option or size option at that time field values comes blank so my filter will not take any result from database. so i want to remove color or size field if value is empty during search. I have tried below code but its not working.how i do this.
var catId = new Array();
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
catId.push("category_id:"+req.body.catId);
}
if(req.body.subcatid){
catId.push("sub_category_id:"+req.body.subcatid);
}
if(req.body.minprice){
catId.push("price:{$gt:"+req.body.minprice+"}");
}
if(req.body.maxprice){
catId.push("price:{$lt:"+req.body.maxprice+"}");
}
if(req.body.color){
catId.push("color:{$in:"+req.body.color+"}");
}
if(req.body.size){
catId.push("attribute:{$in:"+req.body.size+"}");
}
var finalCat = catId.join(',');
console.log(finalCat);
console.log(catId);
if((filter) && (sort)){
Product.find(
{
brand_id:bid, finalCat
},
function(error,fetchallFeatProds)
{
console.log('#######################');
console.log(fetchallFeatProds);
console.log('#######################');
callback(error,fetchallFeatProds);
}).sort( {_id:-1,price:-1} );
This code is not working. Please help me.
Mongoose find prototype handle json and not string
var query = {brand_id:bid};
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
query.category_id = req.body.catId;
}
if(req.body.subcatid){
query.sub_category_id = req.body.subcatid;
}
if(req.body.minprice){
query.price = {$gt:req.body.minprice};
}
if(req.body.maxprice){
query.price = {$lt:req.body.maxprice};
}
if(req.body.color){
query.color = {$in:req.body.color};
}
if(req.body.size){
query.attribute = {$in:req.body.size};
}
if((filter) && (sort)){
Product.find(query, ...

Wordpress: Create pages through database?

Currently I am working on a new traveling website, but am having problems with 1 thing:
I have a list with all the country's, regions and city's i want to publish. How do I quickly create a page for all of them like this:
Every page should be a subpage like: country/region/city
Every page should have a certain page template
Please let me know, thanks in advance for your time and information!
You can do something like this.
<?php
// $country_list = get_country_list(); // returns list, of the format eg. array('India' => 'Content for the country India', 'Australia' => 'Content for the country Australia')
// $region_list = get_region_list($country); // Get the list of regions for given country, Assuming similar format as country.
// $city_list = get_city_list($region); // Get the list of cities for given region, Assuming similar format as country
/* Code starts here...*/
$country_list = get_country_list();
foreach($country_list as $country_title => $country_content) {
$country_template = 'template_country.php';
$country_page_id = add_new_page($country_title, $country_content, $country_template);
// validate if id is not 0 and break loop or take needed action.
$region_list = get_region_list($country_title);
foreach($region_list as $region_title => $region_content) {
$region_template = 'template_region.php';
$region_page_id = add_new_page($region_title, $region_content, $region_template, $country_page_id);
// validate if id is not 0 and break loop or take needed action.
$city_list = get_city_list($region_title);
foreach($city_list as $city_title => $city_content) {
$city_template = 'template_city.php';
add_new_page($city_title, $city_content, $city_template, $region_page_id);
}
}
}
function add_new_page($title, $content, $template_file, $post_parent = 0) {
$post = array();
$post['post_title'] = $title;
$post['post_content'] = $content;
$post['post_parent'] = $post_parent;
$post['post_status'] = 'publish'; // Can be 'draft' / 'private' / 'pending' / 'future'
$post['post_author'] = 1; // This should be the id of the author.
$post['post_type'] = 'page';
$post_id = wp_insert_post($post);
// check if wp_insert_post is successful
if(0 != $post_id) {
// Set the page template
update_post_meta($post_id, '_wp_page_template', $template_file); // Change the default template to custom template
}
return $post_id;
}
Warning: Make sure that the is executed only once or add any validation to avoid duplicate pages.

Multiple records in a details view

I have a database with a table called Patient. In my view I have:
<h2>Search by Patient_Name</h2>
#using (#Html.BeginForm("DetailsbyName", "Patient"))
{
#Html.Label("First Name")
#Html.TextBoxFor(model => model.First_Name)
<br />
#Html.Label("Last Name")
#Html.TextBoxFor(model => model.Last_Name)
<input type="submit", value="Submit"/>}
In my controller is the following method:
public ActionResult DetailsbyName(Patient _patient)
{
string Fname = _patient.First_Name;
string Lname = _patient.Last_Name;
try
{
Patient patient = db.Patients.Single(p => p.First_Name == Fname);
patient = db.Patients.Single(p => p.Last_Name == Lname);
return View(patient);
}
catch
{
return RedirectToAction("About", "Home");
}
}
When a user enters a first or last name that occurs more than once in the Database table, the db.Patients.Singlethrows an exception. What might I use other than .Single to handle this?
For instance a user enters First Name: John
Last Name: Smith
If the DB has more than once "John" I currently get an exception. Or if the DB has more than one "Smith" as a last name I get an exception.
Thanks.
Got it working with this:
List<Patient> patientList = db.Patients.Where(p => p.Last_Name == Lname || p.First_Name == Fname).ToList();
return View(patientList);
Thanks for the help!
I think the decision of what to do when multiple records are returned is up to you. You could have your View's model be a List and display all of them in your view.
You would then have:
List<Patient> patientList = db.Patients.Where(p => p.Last_Name = LName || p.First_Name = FName).ToList();
try db.patients.where(p=>p.LastName = LName), same for FName. And you will need to reutrn this as a list since there could be more than one entry.

Resources