Jquery Template : Can we use string.Compare() or any other alternative with {{if }} statement - jquery-templates

{{each HAName }}
{{if HAName.Conatins('Bar')}}
Yes
{{/if}}
{{/each}}
</div>
I am using Jquery template.
HAName is of type var/string.
My requirement :
If HAName contains the word 'Bar' then Yes should be printed.
e.g. HAName=Bar ,HAName=Bar/Lounge , HAName=He goes to Bar
In all the 3 cases Yes should be printed.
As I am new to Jquery template , a quick help would be appreciated.

{{if HAName.toLowerCase().indexOf("bar") >= 0}}

Related

Laravel Bootstrap-Form update route error

I have a strange issue cause it appeared without changing anything as I remember.
I have done my form like this ( which was working great )
#extends('admin')
#section('content')
{{ BootForm::open(['model'=>$post,'update'=>'posts.update','files'=>'true']) }}
{{ BootForm::text('name','Nom') }}
<div class="form-group">
{{ Form::textarea('content',$post->content,['class'=>'editor','id'=>'editor']) }}
#if($errors->first('content'))
<div class="alert alert-danger">{{ $errors->first('content') }}</div>
#endif
</div>
<div class="form-group">
<input type="file" name="img"/>
#if($errors->first('img'))
<div class="alert alert-danger">{{ $errors->first('img') }}</div>
#endif
</div>
<p>{{ BootForm::submit('Modifier') }}</p>
{{ Bootform::close() }}
#stop
And my route file :
Route::resource('posts','PostsController');
But when I submit my form it goes on :
http://local.dev/posts/4 instead of http://local.dev/myfoldername/public/posts/4
All my others routes are working great ( I have others models )
Thank you for the help
I think the problem is that Laravel is built to be used with virtual host that links on public directory and BootForm may have been coded in the same way.
Try to make a virtual host directly on the public directory so you can access the page with something like
http://something.devinstead of http://local.dev/directory/public
{{ BootForm::open(['model'=>$post,'update'=>'posts.update','files'=>'true']) }}
where is $post->id you must gone "id" controller

Update Categories

I am pretty close to a solution but am stuck. I have a category page where the category is created and deleted on the same page. I am adding a edit function, edit link, and making it to where the user can edit the category on the same page instead of creating another static page and repeating the same code.
Here is my controller function for update
public function postEdit() {
$category = Category::find(Input::get('id'));
if ($category) {
$category->update();
return Redirect::to('admin/categories/index')
->with('message', 'Category Updated');
}
return Redirect::to('admin/categories/index')
->with('message', 'Something went wrong, please try again');
}
Here is the category page where a user can view, create, edit, and delete the categories listed I am trying to have a conditional that states to show create form as default and only show edit form when edit button is clicked.
#extends('layouts.main')
#section('content')
<div id="admin">
<h1>Categories Admin Panel</h1><hr>
<p>Here you can view, delete, create, and edit new categories.</p>
<h2>Categories</h2><hr>
<ul>
#foreach($categories as $category)
<li>
{{ $category->name }} -
{{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }}
{{ Form::open(array('url'=>'admin/categories/edit', 'class'=>'form-inline'))}}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('edit') }}
{{ Form::close() }}
</li>
#endforeach
</ul>
<h2>Create New Category</h2><hr>
#if($errors->has())
<div id="form-errors">
<p>The following errors have occurred:</p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><!-- end form-errors -->
#endif
<php
if(isset(url=>'admin/categories/destroy')) {
{{ Form::open(array('url'=>'admin/categories/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
}else{
{{ Form::open(array('url'=>'admin/categories/edit')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
}
?>
</div><!-- end admin -->
#stop
Please give me an example of how I can accomplish this on the same view page. I know I somehow have to pass in a variable as the value in the form to fill the category selected.
Thank you for the help.
By the way, if you are a moderator and close my question, I will block my IP address, throw a million IP addresses in the network, and just sign up again and again and again with a temp email.
I tried doing a javascript onclick event and here is my results. I cannot get it to work.
Button for link
{{ HTML::Link('#', 'edit', array('class'=>'form-inline', 'id'=>'button', 'onclick'=>'showEditForm(this)'))}}
Javascript
<script>
function showEditForm(id) {
id.innerHTML="
{{ Form::open(array('url'=>'admin/categories/edit')) }}
<p>
{{ Form::hidden('id', $category->id) }}
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Edit Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
";
}
</script>
actually your question is perfectly fine, i gave it a -1 due to the rant. none the less, this question really deserves an answer.
let's sum up your problem. you have 3 main tasks.
create
edit
delete
For Create:
pretty easy. use a 2 column layout. on the side bar, show the form to create category. then use ajax to submit. e.g.
$(function(){
$(document).on('submit','#writeCategory', function(e){
var f = $(this);
e.preventDefault();
var d = f.serialize();
$.ajax({
type : POST,
url : //your url,
data : d
}).done(function(data){
alert('submitted');
//you can also prepend/append the submitted data after successful submission
}).fail(function(x,h,r){
alert('error occured')
});
});
});
For Edit:
for editing, you can use Xeditable. the page has all demo, docs in the site.
For delete:
i'l mention a very simplified approach. using data- attribute. if you want to delete a category, probably you will need only the id.
suppose you are looping to show all the categories. while doing so, store the id in a say, data-id attribute. and write a delete link for each category.
e.g. a delete link for single category entity may look like following:
<a href="#" class="delete" data-id={{$i->id}}>delete</a>
rest is easy. just use ajax and delete the link. e.g.
$(function(){
$('.delete').click(function(e){
e.preventDefault();
var a = $(this);
var answer = confirm('Are you sure you want to delete this?');
if (answer)
{
 
$.ajax({
type : 'post',
url : //your url,
data : {id:a.attr('data-id')}
}).done(function(data){
alert('deleted');
}).fail(function(x,h,r){
alert('error occured');
});
}
});
});
you can also modify it. e.g. after deletion, if you are showing the categories in a tabular manner, you can slide up the row (which contains the specified catgory information) hinting that, that category has been deleted.
Addition
For View:
there are loads of javascript model window plugins. just select one and through ajax, show the details.
All 4 tasks, done in one place.
N.B. here i have used jQuery just for convenience. if you want plain javascript, you can write it that way too....

On click event update template

I´m working on a meteor example. I get the value of one tag on click event on the link. That value is the same that is present on one collection inside doc "pet" or "zoo". I want to use this value to filter the content present on the template.
A minimal example:
{{#each Animal}}
<div>
<span> {{pet}} </span>
</div>
<div>
<span> {{zoo}} </span>
</div>
{{/each}}
After click:
{{#each Animal}}
<div>
<span> {{zoo}} </span>
</div>
{{/each}}
On this case when I get the value present in "zoo" I just want to update the template with all the the spans that contains elements on doc zoo, and that all related to pet dissappear.
The query to mongodb is working perfectly, my problem is that I´m a little bit confused.
Should I use helpers?
Thank you so much.
Let's see if I understood correctly your problem.
You should use a Session variable where you store the action you are doing. Then add a template if and print inside of this tag whatever you want to show at that time.
Let's do a minimal example:
<template name="showAnimalsTemplate">
{{if showAnimals}}
{{#each Animal}}
<div>
<span> {{pet}} </span>
</div>
<div>
<span> {{zoo}} </span>
</div>
{{/each}}
{{/if}}
{{if showZoo}}
{{#each Animal}}
<div>
<span> {{zoo}} </span>
</div>
{{/each}}
{{/if}}
Following this example, you add in the client javascript something like this:
Template.showAnimalsTemplate.showAnimals = function(){
if( Session.get('action') == 'showingTheZoo')
return true;
return false;
}
Template.showAnimalsTemplate.showZoo = function(){
if( Session.get('action') == 'showingTheZoo')
return true;
return false;
}
Don't forget to set the session value inside the click event.
Session.set('action', 'showingTheZoo');

Meteorjs - select DOM of a template from another template

I have the following
<div id="header">
{{> header}}
</div>
<div class="hidden content_box">
{{> content}}
</div>
"content_box" is hidden at the start.
template "header" has a single button.
<template name="header">
<button id="display_content">Click to display content</button>
</template>
template "content" is just a simple div
<template name="content">
It's me, content
</template>
When I click on the button in the header, I want to "show" the content_box.
How do I achieve this? - or better yet, what is the best way to achieve this type of effect where you need to access the DOM of a template from an event of another template?
Template.header.events "click button#display_content": (e) ->
Template.content.show() ?????
I don't know if it is the best way to do it, but in similar situations what I've done before is to use a session parameter to store the show/hide status of the div. In your click event, you then only need to change the value of the session flag. In the template of the div you want to show/hide, you just return the class name.
Example in JS:
Template.header.events({
"click button#display_content": function () {
Session.set('contentShow', true);
}
});
Template.content.className = function (input) {
return Session.equals('contentShow', true) ? '' : 'hidden';
};
Html
<template name="content">
<div class="{{className}} content_box">
It's me, content
</div>
</template>
You'd need to initialise the session flag to false in Meteor.startup() for example Session.set('contentShow', false);. As session is reactive, the div class name will be re-evaluated automatically when you change the session flag.

First item in each - jQuery templates

I'm trying do write a jQuery template where I loop over an array and the first element of the array produces a different output to the remaining elements
<script type='text/x-jquery-tmpl'>
{{each things}}
{{if ${index} == 0}}
<div class='active'>${value}</div>
{{else}}
<div class='passive'>${value}</div>
{{/if}}
{{/each}}
</script>
and data:
{things: ["Val1", "Val2", ... , "Valn"]}
But the {{if ${index} == 0}} condition does not work. How do I make the template produce a different output for the first item of the array from the rest?
Try to remove curly braces at ${index}
{{if $index == 0}}