Kendo mvc grid template format - kendo-asp.net-mvc

My application is MVC5 C#; using Kendo MVC grid, need to format as currency an item.
columns.Bound(p => p.IsFree).Template(#<text> #if (item.IsFree)
{
<span>Free</span>
}
else
{
<span>#item.Price </span>
}</text>).Title("Cost");
Could not find any documentation how to format #item.Price as currency.

You can use ClientTemplate instead of Template with if else condition like below
columns.Bound(c => c.IsFree).ClientTemplate("# if(IsFree) {# <span>Free</span> #} else{# <span>#= kendo.toString(Price, 'C') #</span> #}#").Title("Cost")

I used >#item.Price.ToString("c0").

For server-side binding, perhaps you could try:
.Template(#<text>#if (Model.IsFree) { <span>Free</span> } else { <span>#Model.Price.ToString("C")</span>}</text>)

Related

Get data from db, using multiple checkboxes in laravel 5.5

I have a table called history in which i have a column called PRODUCTS in which the products are saved.
I have a search screen with products using checkboxes, I can get the data if i use only checkbox.
How can i get data if i tick more than one checkboxes.
Displaying the checkboxes using below code
#foreach($products as $product)
#if (in_array($product->name, explode(" , ",$user->products)) !== false)
<input type="checkbox" name="products[]" value="{{$product->name}}"
#if(in_array($product->name, explode(" , ",$user->products))) #endif>
{{$product->name2}}
#endif
#endforeach
Controller
public function usersSearch(Request $request, Repair_History $query)
{
$query = $query->newQuery();
if ($request->has('products')) {
$query->where('proc_doc', $request->input('products'));
}
return $query->get();
}
Fixed it by using below code in my controller, posting as it might help someone.
$friends_checked = $request->input('products');
if (count($friends_checked) > 1 && is_array($friends_checked)) {
$query->whereIn('proc_doc', $friends_checked)
}

Link interpolation i18n

I use ember-i18n to handle multiples langages in my project, and I need to insert a link inside a translation (with interpolation).
Any idea ?
Response from #jamesarosen on Github :
You can't use the {{link-to}} helper inside a translation because it
emits a DOM node, not a String.
But you can use ember-href-to addon to generate URLs.
In JavaScript:
// some-thing/component.js:
import { hrefTo } from 'ember-href-to/helpers/href-to';
text: Ember.computed('i18n.locale', function() {
const i18n = this.get('i18n');
const href = hrefTo(this, 'some.route');
const link = Ember.String.htmlSafe(`Link Text`);
return i18n.t('some.translation', { link });
})
Or in Handlebars (you'll need an htmlSafe helper):
{{t 'some.translation'
link=(htmlSafe (join 'Link Text'))}}
I face a similar problem recently and this is what I did:
Translation file (en.json) :
"some.message.key": "Successfully created <a href='{{userlink}}'> {{username}} </a>."
In the respective template/hbs file:
{{html-safe (t "some.message.key" userlink=link username=name)}}
html-safe is a helper provided by ember-cli-string-helpers add on.

Customize list rendering for pagination

i'm trying to implement infinite scroll in Orchard doing minimal changes.
The script I use need to perfectly identify with a jquery selector the Next page link of pager.
Currently a standard orchard pager renders this way:
<li>></li>
the desiderable rendering is:
<li class="next">></li>
I tried many ways to override the Pager_Next template but no joy.
The pager is a list and list is done by code. No easy way to override.
A great article the should explain how to do miss some basic part (as to override the whole list for example):
http://weblogs.asp.net/bleroy/overriding-the-pager-rendering-in-orchard
Right now my workaround was to change the Orchard source CoreShapes.cs for list rendering adding these two lines:
if (itemTag != null) {
if (index == 0)
itemTag.AddCssClass("first");
if (index == count - 1)
itemTag.AddCssClass("last");
//new lines
if (index == 1 && count > 2)
itemTag.AddCssClass("previous");
if (index == count - 2 && count > 2)
itemTag.AddCssClass("next");
So far it works BUT I do not like it
1) It changes orchard source, this is bad
2) It changes all the lists (and not just pager)
So "How may I override the list for JUST my theme and for JUST pager in a way that a class is added automatically at the Page_Next li tag?"
Thanks
Try something like this in your Pager.Next.cshtml alternate view:
#using System.Web.Routing;
#{
string text = Model.Value.ToString();
string action = Model.RouteValues["action"].ToString();
RouteValueDictionary routeValues = (RouteValueDictionary)Model.RouteValues;
}
<span><a class="next" href="#Url.Action(action, routeValues)">#text</a></span>
I'm not sure if the answer by joshb is close enough for your needs.
Otherwise I would say almost the same thing.
Change the Pager.Next.cshtml and Pager.Previous.cshtml in your theme, and simply add a way for you to identify the link.
The use jQuery to add the class you need to the parent.
This is by no means an elegant solution, but it will accomplish what you're asking for without changing the core and without affecting other lists.
My test looks like this, just for testing and only for next, but you get the idea.
(Don't use id obviously as it would give you duplicate id's, this is just a quick and dirty example)
Pager.Next.cshtml :
#{
var RouteValues = (object)Model.RouteValues;
RouteValueDictionary rvd;
if (RouteValues == null) {
rvd = new RouteValueDictionary();
}
else {
rvd = RouteValues is RouteValueDictionary ? (RouteValueDictionary)RouteValues : new RouteValueDictionary(RouteValues);
}
}
<a id="pagination-pager-next" href="#Url.Action((string)rvd["action"], rvd)"><i class="fa fa-angle-right"></i></a>
And added this at the end of Pager.cshtml, but you may have a better place for it:
<script>
$(document).ready(function () {
$("#pagination-pager-next").parent().addClass("next");
});
</script>

GRAILS: Can I call service logic from _FORM.GSP template?

I'm refactoring scaffolding templates and I hit a wall with this issue:
I was trying to call service (some security logic) from template _FORM.GSP - but in the code part, not in the output part
I've read and tried what was suggested in here: How do I call a Grails service from a gsp?
I've tried to use taglib, but my knowledge of grails may not be extensive enough for that
I've tried add import and def to the beginning of _FORM.GSP file (both grailsApplication and application instantiation of service were crashing on missing property application resp. missing property grailsApplication)
I've even tried to call the taglib from the code both directly as method isAllowedToEdit and also as g.isAllowedToEdit both crashing on unknown method resp. "no such property g"
it seems that template _form.gsp has different rules than standard gsp view
I want to do something like this:
private renderFieldForProperty(p, owningClass, prefix = "") {
boolean hasHibernate = pluginManager?.hasGrailsPlugin('hibernate')
boolean display = true
boolean required = false
if (hasHibernate) {
cp = owningClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
required = (cp ? !(cp.propertyType in [boolean, Boolean]) && !cp.nullable && (cp.propertyType != String || !cp.blank) : false)
}
/* trying to do this part */
// I want to assign value to cp.editable - so later I can render read-only fields in renderEdit
if (!mySecurityService.canEdit(springSecurityService.currentUser, owningClass.getClass(), actionName, p.name)) {
cp.editable = false
}
/* trying to do this part */
if (display) { %>
<div class="fieldcontain \${hasErrors(bean: ${propertyName}, field: '${prefix}${p.name}', 'error')} ${required ? 'required' : ''}">
<label for="${prefix}${p.name}">
<g:message code="${domainClass.propertyName}.${prefix}${p.name}.label" default="${p.naturalName}" />
<% if (required) { %><span class="required-indicator">*</span><% } %>
</label>
${renderEditor(p)}
</div>
<% } } %>
if there is any way to assign cp.editable - I'll try your suggestions
it seems that template _form.gsp has different rules than standard gsp view
The generated _form.gsp works same as other gsps but the template inside the scr/templates/scaffolding/ is different. Customizing the templates like you are doing is a bit more tricky. Keep in mind that the logic you are writing is for Grails on how to generate views(gsp). Meaning you are telling Grails to check some logic before generating the views in memory or in the file. You might be able to accomplish that to some extend for dynamic (in memory) scaffolding at run-time but for sure not for static scaffolding. That's because Grails
is not aware of currentUser when generating the templates.
Your problem will be much simpler if you generate your views and then customize them instead of modifying their templates. Then you can inject your services and do other checks. However, as you also mentioned those logics are better off in a tag library here.
Also since you mentioned security, rendering a field non-editable does not guaranty inability to edit your fields. I would suggest to put the check logic inside your controller for example in SAVE or UPDATE action to prevent any unauthorized user editing fields.
Did you try this?
<%# page import="com.myproject.MyService" %>
<%
def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>
this will work for sure.
go through this link : click here

Adding a search box to filter a list of results in Symfony?

I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.
I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).
I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.
I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?
EDIT
I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.
Thanks!
I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:
//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
//set the form filter
$this->searchForm = new EmployeeFormFilter();
//bind it empty to fetch all data
$this->searchForm->bind(array());
//fetch all
$this->employees = $this->searchForm->getQuery()->execute();
...
}
I made a search action which does the search
public function executeSearch(sfWebRequest $request)
{
//create filter
$this->searchForm = new EmployeeFormFilter();
//bind parameter
$fields = $request->getParameter($this->searchForm->getName());
//bind
$this->searchForm->bind($fields);
//set paginator
$this->employees = $this->searchForm->getQuery()->execute();
...
//template
$this->setTemplate("index");
}
It's important that the search form goes to mymodule/search action.
Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.
If you want to add more fields to the search form check this :)
I finally made a custom form using the default MyModuleForm generated by Symfony
public function executeIndex {
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
but displaying only a custom field:
<div id="search_box">
<input type="text" name="criteria" id="search_box_criteria" value="Search..." />
<?php echo link_to('Search', '#my_module_search?criteria=') ?>
</div>
Then I created a route named #my_module_search linked to the index action:
my_module_search:
url: my_module/search/:criteria
param: { module: my_module, action: index }
requirements: { criteria: .* } # Terms are optional, show all by default
With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:
$('#search_box a').click(function(){
$(this).attr('href', $(this).attr('href') + $(this).prev().val());
});
And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:
public function executeIndex {
...
// Deal with search criteria
if ( $text = $request->getParameter('criteria') ) {
$query = $this->pager->getQuery()
->where("MyTable.name LIKE ?", "%$text%")
->orWhere("MyTable.remarks LIKE ?", "%$text%")
...;
}
$this->pager->setQuery($query);
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.