How to right align numeric data and left align others? - ruby-on-rails-3

I want that if the user enters numeric data it should be left aligned and if he enters other datatype then it should be left aligned. Text align I can give the css property that text-align:right. How can I find the datatype when the user enters data? I am using the jquery datatables.

This is not a rails concern. If you want to set the css property based on the nature of content in the cell, I would strongly advise a jQuery / Javascript solution.
You could do something like this:
$(document).ready(function(){
$("td").each(function() {
if (parseInt($(this).text()) > 1) {
$(this).css("text-align", "right");
}
});
});
For a more complicated solution you could implement your own Jquery Filter, something like:
$.extend($.expr[":"], {
expr: (function() {
var op = {
'=': function(n1, n2) { return n1 == n2; },
'!=': function(n1, n2) { return n1 != n2; },
'>': function(n1, n2) { return n1 > n2; },
'>=': function(n1, n2) { return n1 >= n2; },
'<': function(n1, n2) { return n1 < n2; },
'<=': function(n1, n2) { return n1 <= n2; }
};
var ops = [];
for (p in op) {
ops.push(p);
}
var r = new RegExp("^\\s*(" + ops.join("|") + ")\\s*(-?\\d+(?:\\.\\d+)?)\\s*$");
return function(elem, i, match) {
var m = r.exec(match[3]);
return m && op[m[1]](parseFloat($(elem).text()), parseFloat(m[2]));
};
})()
});
Which would allow you to filter values based on numeric constraints like:
$("td:expr(>5)").css("text-align", "right");

Related

Row Span AutoGrouping and Bug

I've created a plnkr to auto-group row-spans the way you would really expect it to work out of the box IMHO.
Anyhow... doing this surfaces an apparent bug... the rowSpan is not consistently applied to the grid.. if you scroll up and down, it sometimes applies, and sometimes does not.
In the screenshot below... you can see 'Aaron Peirsol' is spanning... but if I scroll up and down it might not span on him... not consistent.
Here 'Aaron Peirsol' is no longer spanning all 3 rows -- all I did was scroll up and back down
See this Sample
https://plnkr.co/edit/UxOcCL1SEY4tScn2?open=app%2Fapp.component.ts
Here I've added columndefs for the grouping
{
field: 'athlete',
rowSpan: params => params.data.groupCount,
cellClassRules: {
'cell-span': "data.isFirst && data.groupCount>1",
},
width: 200,
},
{field:'groupCount', width: 20}, /* included for debugging */
{field:'isFirst', width: 20}, /* included for debugging */
And here I'm doing the auto-grouping code:
onGridReady(params: GridReadyEvent) {
this.http
.get<any[]>('https://www.ag-grid.com/example-assets/olympic-winners.json')
.subscribe((data) => {
let groupKey = 'athlete';
let sorted = data.sort((a,b) => (a[groupKey] > b[groupKey]) ? 1 :
((b[groupKey] > a[groupKey]) ? -1 : 0));
let filtered = sorted.filter(x => {
return x[groupKey] < 'Albert' && x[groupKey];
});
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
let keyValue = x[key];
if (rv[keyValue] === undefined)
{
rv[keyValue] = 0;
}
if (keyValue) {
rv[keyValue] ++;
}
return rv;
}, {});
};
let grouped = groupBy(filtered, groupKey);
let prev = '';
for (let i=0; i<filtered.length; i++)
{
let keyValue = filtered[i][groupKey];
filtered[i]['groupCount'] = grouped[keyValue];
if (keyValue == prev)
{
filtered[i]['isFirst'] = false;
}
else
{
filtered[i]['isFirst'] = true;
}
prev = keyValue;
}
this.rowData = filtered});
}
OK, found the issue...
rowSpan function must only return a span count for the first row of the span...
every other row it must return 1
I've updated the plunker
public columnDefs: ColDef[] = [
{
field: 'athlete',
rowSpan: params => params.data.groupRowCount == 1 ? params.data.groupCount: 1, //THIS IS CRUCIAL.. only return count for first row
cellClassRules: {
'cell-span': "data.groupRowCount==1 && data.groupCount>1",
},
width: 200,
},

weex input number limit

use component, when i input content,it has a limit condation,only input number length is 8,and only 2 radix point,for example,can input 123.45, 123.456 is not.
<input :type="type"
v-model="inputValue"
:max-length="maxInputLength"
:maxlength="maxInputLength"
:placeholder="rightPlaceholder"
:class="['input', disabled ? 'input-style-disabled' : '']"
:disabled="disabled"
#input="onTextChange"/>
and data is :
data () {
return {
inputValue:'', //input value
}
},
mounted(){
if(this.value) {
this.inputValue = this.value;
}
},
methods:{
formatAmount(value){
let result;
if (value.indexOf(".") > 0 && value.length - value.indexOf(".") >= 4){
result = value.substr(0,value.indexOf('.') + 3);
// fValue = value.toFixed(2);
Log.d("formatAmount",result);
}else {
result = value;
Log.d("formatAmount else",result);
}
return result;
},
onTextChange() {
setTimeout(() => {
this.inputValue = this.formatAmount(this.inputValue);
this.$emit("onTextChange",this.inputValue);
}, 10);
}
}
totally,this just work when two point change to three point,then still can input three point.
how does it work,need help.thx.
You can use array split on (.) and then check arr[1].length<=2 and arr[0].length <=6.or use regex /^\d+(.\d{1,2})?$/ for 2 radix point;

It is corret usage to create a tab for mithril?

I want to create a simple Tab but I think it is strange for my using:
var root = document.body
var index = {
view: () => index.html,
html: m('div', { id: 'div1' }, [
[
(function () {
let value = ["A", "B", "C", "D"]
let output = []
for (let i = 0; i < 4; i++) {
output.push(m('input', {
class: (function () {
if (i == 0) return "onit"
})(),
type: 'button',
value: value[i],
onclick: function () {
let div1 = document.getElementById("div1")
let btn = div1.getElementsByTagName("input")
let div1_div = div1.getElementsByTagName("div")
let _this = this
let num = (function () {
for (let i = 0; i < btn.length; i++) {
if (btn[i] == _this) {
return i
}
}
})()
for (let i = 0; i < btn.length; i++) {
btn[i].className = ""
}
this.className = "onit"
for (let i = 0; i < div1_div.length; i++) {
div1_div[i].setAttribute("style", "dispaly:none")
}
div1_div[num].setAttribute("style", "display:block")
}
}))
}
return output
})()
],
[
(function () {
let arr = ["aaa", "bbb", "ccc", "ddd"]
let output = []
for (let i = 0; i < 4; i++) {
output.push(m("div", { style: (the => i == 0 ? "display:block" : undefined)() }, arr[i]))
}
return output
})()
]
])
}
m.route(root, "/index", {
"/index": index
})
Is there any other simple way to achieve this?
If I click the button, the style of button will change and the display of all "div" will be changed. Screenshot
In mithril.js views you can only use expressions, for-loops, ifs and so on are no expressions and only possible the way you did it. Nevertheless there are other ways to achieve this
Loops can be achieved using the functional counterparts
let values = ["A", "B", "C", "D"]
let output = []
function(){
for (let i = 0; i < 4; i++) {
output.push(m('span', value)
}
return output
}()
can be written as
values.map(value => m('span', value)
if-Statements can be written using the ternary expression
function() {
if (condition) {
return 'this'
} else {
return 'that'
}
}()
just use
condition ? 'this' : 'that'
You can also use view functions if your view code gets to deeply nested and you need custom logic:
function someOtherView(someData) {
if (someData.shouldBeShown) {
return someData.text
}
}
function someView() {
...
someOtherView(someData)
...
}
This also makes your views more readable.

Sorting the Dates in the Grid corresponding to the Locale in the Browser

Sorting Image Issue
Greeting,
I am writing a code in dojo that compares the date column in grid for the sorting. below is the code :
function(a,b){
var a1=new Date(a);
var a2=new Date(b);
var x = dojo.date.locale.format(a1, {datePattern: "yyyy-MM-dd", selector: "date"});
var y = dojo.date.locale.format(a2, {datePattern: "yyyy-MM-dd", selector: "date"});
if((a!=null)&&(b!=null)){
if (x.toLowerCase() < y.toLowerCase())
{
debugger;
return -1;
}
else if (x.toLowerCase() > y.toLowerCase())
{
debugger;
return 1;
}
else
{
debugger;
return 0;
}
}
Code works fine for me when the Language in the browser is English but when I changes to Dutch or any other then it doesnt sorts the values properly.
Please guide.
Thanks
I'm not sure why you're having this problem since those format calls ought to be returning the same result regardless, but that code seems severely overcomplicated.
If you are simply trying to sort dates in chronological order, you should merely need to compare them as numbers.
var a = [ '2015-10-18', '2015-10-12', '2015-10-16' ];
a.sort(function (a, b) {
a = new Date(a);
b = new Date(b);
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
});
console.log(a); // ["2015-10-12", "2015-10-16", "2015-10-18"]

Meteor.js Handlebars template logic operators

From this page, I inserted to my /client/helpers/handlebars.js file this handlebars helper:
Handlebars.registerHelper('compare', function (lvalue, operator, rvalue, options) {
var operators, result;
if (arguments.length < 3) {
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
}
if (options === undefined) {
options = rvalue;
rvalue = operator;
operator = "===";
}
operators = {
'==': function (l, r) { return l == r; },
'===': function (l, r) { return l === r; },
'!=': function (l, r) { return l != r; },
'!==': function (l, r) { return l !== r; },
'<': function (l, r) { return l < r; },
'>': function (l, r) { return l > r; },
'<=': function (l, r) { return l <= r; },
'>=': function (l, r) { return l >= r; },
'typeof': function (l, r) { return typeof l == r; }
};
if (!operators[operator]) {
throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
}
result = operators[operator](lvalue, rvalue);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
And to the template:
{{#compare "Test" "Test"}}
Default comparison of "==="
{{/compare}}
And in console I always see: Exception from Deps recompute: Error: Handlerbars Helper 'compare' needs 2 parameters
I tried this as well:
{{#compare "Test" "==" "Test"}}
But this did not help.
Try
{{#compare "Test" "Test" operator="=="}}