Magnific Popup with Select Box - magnific-popup

I am trying to use Magnific Popup with a select box. Initially I was attempting to form the options like this:
<option class="popup-modal" value="#<%= counties[i].Code %>">
The SelectBox has a onchange set to send "this.value" to a Javascript function:
var doPopUp = function(county){
console.log(county);
$('.popup-modal').magnificPopup({
type: 'inline',
preloader: false,
focus: '#username',
mainClass: 'mfp-fade',
removalDelay: 300,
modal: true
});
};
This is not opening the popup however.
If I give the select element the following:
<select class="popup-modal" onchange="href=this.value">
This opens the popup but it just says "true".
I am very new to Magnifi and am feeling pretty lost at the moment.
Has anyone had luck in using Magnific Popup with Select Boxes?

This solved it...
<select onchange="doPopUp(this.value)">
<option selected="true" disabled="disabled">
-Select County-
</option>
<% for (var i = 0; i < counties.length; i++) { %>
<option value="#<%= counties[i].Code %>">
<%= counties[i].Name %>
</option>
<% } %>
</select>
and then...
$.magnificPopup.open({
type: 'inline',
preloader: false,
focus: '#username',
mainClass: 'mfp-fade',
removalDelay: 300,
modal: true,
items:{
src:county
}
});

Related

Validate submitted form and display errors gracefully

I am trying to validate user submitted form and display meaningful errors, but I have a lot of problems. First of all, my form (event_form.ejs):
<% if (typeof errors !== 'undefined') { %>
<div class="message"><%= errors %></div>
<% } %>
<form method="POST" action="/submit-create-event" id="create-event">
<input type="text" class="title" name="title" placeholder="title">
<input type="text" class="date" autocomplete="off" name="date" placeholder="date">
<select class="type" name="type">
<option value="road">Road</option>
<option value="mtb">MTB</option>
</select>
<input type="text" class="city" name="city" placeholder="city">
<select class="level" name="level">
<option value="beginner">Beginner</option>
<option value="advanced">Advanced</option>
<option value="pro">Pro</option>
</select>
<input type="text" class="start-time timepicker" autocomplete="off" data-time-format="H:i" name="startTime" placeholder="start time">
<input type="text" class="start_location" name="startLocation" placeholder="start location">
<input type="text" class="distance" name="distance" placeholder="distance">
<input type="submit" class="create-event-submit">
</form>
On submit, in my eventController.js I have function to handle submit.
//Helper function to handle event form submit.
module.exports.submitEventForm = function(req, res) {
const event = new eventModel.event({
title: req.body.title,
date: req.body.date,
type: req.body.type,
city: req.body.city,
level: req.body.level,
start_time: req.body.startTime,
start_location: req.body.startLocation,
distance: req.body.distance
});
event.save(function(err, data) {
if (err) {
console.log(err);
//I tried this, kinda works but pretty sure a bad practice
res.render('forms/create_event_form', {errors: err})
} else {
res.render('status/success');
}
});
};
As you can see, on event.save() if I have errors, I console.log() them at the moment. What I tried doing was rendering once again my event creation form and displaying errors (as visible in my .ejs file). Pretty sure it should not be done like that.
This is my model file with one validator
var mongoose = require('mongoose')
var validate = require('mongoose-validator')
var Schema = mongoose.Schema;
var titleValidator = [
validate({
validator: 'isAlphanumeric',
message: 'Title should be alphanumeric'
})
]
var eventSchema = new Schema({
title: {type: String, required: true, validate: titleValidator},
date: {type: String },
type: {type: String },
city: {type: String },
level: {type: String },
start_time: {type: String },
start_location: {type: String },
distance: {type: Number },
});
var Event = mongoose.model('Event', eventSchema);
module.exports.eventSchema = eventSchema;
module.exports.event = Event;
I created titleValidator, and if my form fails, it prints out something like this:
My questions is: How can I properly send my validator message to the template and display it? How to handle multiple messages once more validators are created, as errors are not returned in array manner, meaning I cannot loop through them?
Kinda found a solution. To make error messages more readable and usable, I used Mongoose error helper. It prints out all error messages neatly. However, when getting package via npm there is a bug in code and a pull request is created for a solution, but was never merged. Just make that minor change manually.
I couldn't find another way of rendering the same form with errors, so I sticked to this:
event.save(function(err) {
if (err) {
res.render('forms/create_event_form', {errors: errorHelper(err, next)});
} else {
res.render('status/success');
}
});
and in my .ejs template I print out errors as follows:
<% if (typeof errors !== 'undefined') {
errors.forEach(function(err) { %>
<div class="message"><%= err %></div>
<% })} %>
Hope this helps someone, cheers.

Ignore certain links in the html magnific-popup

I need Magnific-popup to bind to only certain links in the HTML. This is so when one linked is clicked it will link out to another website. If the other link is clicked it will enlarge the image.
Right now the link the being clicked that is not wrapped in an image the popup is initialized.
This is the HTML
<div class="field__item">
<div class="mfp-field mfp-separate-item">
<a href="path-to-origin-image">
<!-- This link is being added to the popup and works correctly-->
<img class="mfp-thumbnail" src="img-url" />
</a>
</div>
<div class="field--name-field-url">
<!-- This link is being added to the popup and should be ignored -->
Google
</div>
</div>
This is the jquery
$(context).find('.field--name-field-gallery-items').once('mfp-processed').each( function() {
$(this).magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled: true
},
image: {
titleSrc: function (item) {
return item.img.attr('alt') || '';
}
}
});
});
$(context).find('.field--name-field-gallery-items .field--name-field-url a').each().unbind('click');
How do I stop magnificPopup from binding to the second link?
The solution is the following:
$(context).find('.field--name-field-gallery-items').once('mfp-processed').each( function() {
$(this).magnificPopup({
delegate: '.mfp-field a',
type: 'image',
gallery: {
enabled: true
},
image: {
titleSrc: function (item) {
return item.img.attr('alt') || '';
}
}
});
});
I should have read the api properly

vuejs inline editing with select box option call issues

Good Day, I'm working on a simple inline editing functionality with Vuejs. Please have a look at this jsbin.
Users info. listed with Edit buttons, when clicked i turn those into input/select fields and populate corresponding options with helper methods.
My issue here is once i populate select options, my helper methods being called even if i change select value. How can i change this to load them only once and use them. Also, how can i validate current row fields as required when clicked on save button?
Try this.
new Vue({
el: '#app',
data: {
users: [
{name: 'Jhon', city:'Newyork', country: 'US', country_id:'23', city_id:'4'},
{name: 'Ali', city:'London', country: 'UK', country_id:'13', city_id:'33'},
{name: 'Raj', city:'Delhi', country: 'IN', country_id:'3', city_id:'11'},
],
cities: [
{id:'4', val:'Newyork', country_id:'23'},
{id:'33', val:'London', country_id:'13'},
{id:'11', val:'Delhi', country_id:'3'},
],
countries: [
{id:'23', val:'US'},
{id:'13', val:'UK'},
{id:'3', val:'IN'},
]
},
computed:{
citiesByCountry(){
return this.countries.reduce((acc, country) => {
acc[country.id] = this.cities.filter(c => c.country_id == country.id)
return acc
}, {})
}
},
methods: {
edit :function(obj){
this.$set(obj, 'editmode', true);
},
save : function(obj){
this.$set(obj, 'editmode', false);
},
cloneLast:function(){
var lastObj = this.users[this.users.length-1];
lastObj = JSON.parse(JSON.stringify(lastObj));
lastObj.editmode = true;
this.users.push(lastObj);
},
}
})
And change your template to this.
<td>
<span v-if="user.editmode">
<select v-model="user.city_id" required>
<option v-for="option in citiesByCountry[user.country_id]" :value="option.id">{{option.val}}</option>
</select>
</span>
<span v-else>{{user.city}}</span>
</td>
<td>
<span v-if="user.editmode">
<select v-model="user.country_id" required>
<option v-for="option in countries" :value="option.id">{{option.val}}</option>
</select>
</span>
<span v-else>{{user.country}}</span>
</td>
Working example.

adding button on column template of kendo grid

I am building an app using MVC using Jquery(KendoGrid) for displaying data, everything was working fine as per requirement, later we planned to add extra column with button present on each row of the grid, sounds simple, but tried number of ways to add into the application, getting error message "undefined 'node' ..... ", so i had no other options rather than posting here, if any one could able to help me in this will be appreciative, and i used column template on jquery kendo grid thanks
scenario
onclick of that button in specified row it should carry "ID(as shown below)" and redirect to "ActionResult" Controller, where I can further code as per my requirement
code(part of the code)
columns: [
{ field: "ID", Title: "ID", filterable: false, sortable: false, hidden: true },
{ field: "RowID", Title: "RowID", filterable: false, sortable: false, hidden: true },
{ field: "BillNumber", Title: "BillNumber", filterable: false, sortable: false,hidden:true },
{ field: "ServiceName", Title: "ServiceName",width:600 },
{ field: "ServiceStatus", Title: "ServiceStatus", width: 150 }
// Creating template column
, {
field: "Action", title: "Is Action", template: "<input type=\"checkbox\" #= Action ? checked='checked' : '' # class=\"check_row\"/> ", editable: false,
// field: "Action",title: "Preview ", template: '<input type="button" class="info" name="info" value="Info" style="height: 26px; margin: 0px 2px; min-width: 64px;" />',
headerTemplate: '<label> <input type="checkbox" id="checkAll"/>Print All</label>', filterable: false, sortable: false, width: 100,
}
currently I am able to generate checkbox in column, what i need is one more column with button(same as checkbox in each row)
Manjuboyz
Defining a button in a cell is pretty simple... basically you were doing it right.
Example: Define a column as:
columns: [
...
{
title: "Preview ",
template: '<input type="button" class="k-button info" name="info" value="Info" />',
headerTemplate: '<label> <input type="checkbox" id="checkAll"/>Print All</label>',
filterable: false,
sortable: false,
width: 100
}
]
Define the template as an input of type button. If you want it to look like a Kendo UI button add k-button class to it.
Then, you can bind a click handler doing:
$(".info").on("click", function() {
var row = $(this).closest("tr");
var item = grid.dataItem(row);
...
});
Where item contains all the data corresponding to the row that you clicked the button.
Running example here : http://jsfiddle.net/m8fsv/1/
EDIT: If what you need is to control / decide showing one or the other, you should change the template to something like:
<script id="template" type="text/kendo-template">
# if (showButton) { #
<input type="checkbox" #= data.Action ? checked="checked" : "" # class=\"check_row\"/>
# } else { #
<input type="button" class="k-button info" name="info" value="Info" />
# } #
</script>
You can check it here: http://jsfiddle.net/OnaBai/m8fsv/12/

How do I get a rails page to update when a form is submitted?

I've struggled with this, searched, etc., now I'm ready to ask for help.
I have a controller 'DashboardController', and I have a single view 'dashboard.html.erb'.
The dashboard.html.erb page has three components; some javascript that creates a chart using model data, a div to hold the chart, and a form to select chart options.
This all works. I can get the form to place the parameters into the query string properly when I submit the form, but the div containing the target for the chart will not update on submit.
Basically looking to have a form driven chart, can't make it happen. Frustrating.
I'm new at Rails and my knowledge of Controllers, Routing and Actions is still evolving.
Having difficulty with the form_tag aspect of Rails in my code here I've switched to a simple form here. Still nothing.
Any help would be appreciated.
<script type="text/javascript">
$(function() {
new Highcharts.Chart({
chart: {
renderTo: "orders_chart",
type: "column"
},
title: {
text: "Sales Billings - <%= params[:sale_year] %>"
},
xAxis: {
categories: [1,2,3,4]
},
yAxis: {
text: "Billings"
},
tooltip: {
formatter: function() {
return 'Q-' + this.x + ': ' +
"$" + Highcharts.numberFormat(this.y,2);
}
},
series: [{
name: "Business Unit A",
data: <%= Sale.where(:year => params[:id], :segment => 'IT Infrastructure').each.map { |row| row.billings.to_f }.inspect %>
},{
name: "Business Unit B",
data: <%= Sale.where(:year => params[:id], :segment => 'Collaboration').each.map { |row| row.billings.to_f }.inspect %>
},{
name: "Business Unit C",
data: <%= Sale.where(:year => params[:id], :segment => 'MSP').each.map { |row| row.billings.to_f }.inspect %>
}]
});
});
<div id="orders_chart" style="width:560px; height:380px;"></div>
<form accept-charset="UTF-8" action="/dashboard" method="">
<select id="sale[:year]" name="sale_year" data-mini="true" data-corners="false">
<option>Year</option>
<% #salesyear = Sale.select(:year).uniq
#salesyear.each do |years| %>
<option value="<%=years.year %>"><%=years.year %></option>
<% end %>
</select>
<select id="sale[:quarter]" name="sale_quarter" data-mini="true" data-corners="false">
<option>Quarter</option>
<% #salesqtr = Sale.select(:quarter).uniq
#salesqtr.each do |qtr| %>
<option value="<%=qtr.quarter %>">Q<%=qtr.quarter %></option>
<% end %>
</select>
<select id="sale[:month]" name="sale_month" data-mini="true" data-corners="false">
<option>Month</option>
<% #salesm = Sale.select(:month).uniq
#salesm.each do |month| %>
<option value="<%=month.month %>">Month - <%=month.month %></option>
<% end %>
</select>
<input name="commit" type="submit" value="Update" class="ui-btn-hidden" aria-disabled="false" data-theme="c" data-mini="true" data-icon="search" data-iconpos="right" data-corners="false">
</form>
Without seeing your routes.rb file or your DashboardsController, it's difficult to infer what's happening.
I can say that your form tag needs to submit to the correct action with an expected method (likely "post" or "put". You have specified the "/dashboard" action and no method:
<form accept-charset="UTF-8" action="/dashboard" method="">
I'm guessing "/dashboard" maps to a "get" request in your routes.rb file, like:
match "/dashboard", to: "dashboards#dashboard"
An atypical, but possibly quick) change, would be to allow both "get" and "post" requests:
match "/dashboard", to: "dashboards#dashboard", via: [:get, :post]
You would also need to make sure the controller action is set up to handle the params sent by the post request.