Sencha Touch - How to change the style of a list item based on its previous value - sencha-touch

I have a list (xtype: 'list') and I use an XTemplate to customize it. It looks like this:
itemTpl: new Ext.XTemplate('<div>',
'<span style="display: inline-block; width: 100px">{itemName}</span>',
'<span style="display: inline-block; width: 100px">{price}</span>','</div>')
I would like to change the color of {price} (using css I guess) based on the previous and current value:
If [current value] > [previous value] color it green.
If [previous value] > [current value] color it red.
The price value is updated like this:
var itemList = Ext.getCmp('itemList');
var store = itemList.getStore();
store.getAt(i).set('price', currentPrice);
While i is the number of item (or row in the list) that is updated and currentPrice is the new value. This code is executed by a function that is called once every second.
I know there is an option to have conditions inside an XTemplate, but all the examples I saw compare the new value with a constant. How can I change the color of {price} by comparing the new value to the old one?

You may add 'color' property to store when the store has loaded/created or while it is filling/creating.
store.each(function(item, i) {
var previousItem = store.getAt(i-1);
if (previousItem) {
var previousPrice = previousItem.get('price');
var price = item.get('price');
item.set('color', (price > previousPrice) ? 'green' : 'red');
}
})
Then you can use property in template
itemTpl: new Ext.XTemplate('<div>', '<span style="display: inline-block; width: 100px; color: {color}">{itemName}</span>', '<span style="display: inline-block; width: 100px">{price}</span>','</div>')

Related

Check if array has a value, add class if doesn't

I have a list of category names that are written in Jade.
ul
li Discussion
li Movie
li Music
li Performance
li Dance
li Theatre
And I have some json, that shows what kind of categories are added to a specific event:
…
values: [
{
ordinal: 50469,
db_value: 626,
id: 50469,
value: "Discussion"
},
{
ordinal: 50470,
db_value: 623,
id: 50470,
value: "Dance"
}
],
…
I have a route, that gets the category values:
res.render("event", {
categories: data.result.properties.category.values
})
How can I achieve an outcome like this, that an if/else checks if the value equals the same thing that's in the li tag and by this adds a class .unactive, if it doesn't exhist in the json array:
<ul>
<li>Discussion</li>
<li class="unactive">Movie</li>
<li class="unactive">Music</li>
<li class="unactive">Performance</li>
<li>Dance</li>
<li class="unactive">Theatre</li>
</ul>
First, as Andrew did, i would simplify the data format. You could either do this in Node before sending it to the template, or using some JS in the template itself. I'll only edit the template here:
- categoryNames = categories.map(function(c){return c.value});
This will create an array of just the names. (And, it doesn't even need underscore.js. ;))
Now, you can simply check if a given name is in the array using indexOf():
ul
li(class=(categoryNames.indexOf("Discussion") > -1 ? "" : "inactive")) Discussion
li(class=(categoryNames.indexOf("Movie") > -1 ? "" : "inactive")) Movie
li(class=(categoryNames.indexOf("Music") > -1 ? "" : "inactive")) Music
li(class=(categoryNames.indexOf("Performance") > -1 ? "" : "inactive")) Performance
li(class=(categoryNames.indexOf("Dance") > -1 ? "" : "inactive")) Dance
li(class=(categoryNames.indexOf("Theatre") > -1 ? "" : "inactive")) Theatre
I would first massage the data a bit to a format that's easier to render, like this:
var _ = require('underscore');
var categoryNames = _.map(data.result.properties.category.values, function(value) {
return value.value;
});
// categoryNames is an array like this: ['Discussion', 'Dance']
res.render("event", {
categoryNames: categoryNames
})
Now you can get the behavior you're looking for like this:
ul
each category in categoryNames
li category
But if you really want to keep the "disabled" cateogry tags around, you can do it with an inline conditional like this:
ul
each category in ['Discussion', 'Movie', 'Music', 'Performance', 'Theatre']
li(class=(categoryNames.indexOf(category) === -1) ? "" : "inactive") #{category}

Twitter Bootstrap Modal trigger on table click

Folks, trying to follow example here http://jsfiddle.net/yF45N/. I have JADE file below, which should open up a modal once a table item is clicked. What am I missing?
Thanks!!!!
block Content
if (pendingArray)
legend Pending Requests
table.table-hover.table-condensed
thead
tr
th id
th Email
th foo
th bar
tbody
- each val in pendingArray
tr
data-toggle="modal"
data-id="#{val.id}"
data-target="#orderModal"
href='someModal'
td #{val.id}
td #{val.Email}
td #{val.foo}
td #{val.bar}
orderModal.modal.fade
.modal-dialog
.modal-content
.modal-header
button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
h4.modal-title Title
.modal-body
p body
.modal-footer
button.btn.btn-default(type='button', data-dismiss='modal') Close
button.btn.btn-primary(type='button') Save changes
body
script.
$(function(){
$('#orderModal').modal({
keyboard: true,
backdrop: "static",
show:false,
}).on('show', function(){
var getIdFromRow = $(event.target).closest('tr').data('id');
//make your ajax call populate items or what even you need
$(this).find('#orderDetails').html($('<b> Order Id selected: ' + getIdFromRow + '</b>'))
});
});
Very Simple, even for a front-end idiot like myself:
tbody
- each val in pendingArray
tr(data-toggle='modal', href='#mvrDisplayModal', data-target="#foo")
td #{val.email}
td #{val.blah}
#mvrDisplayModal.modal.fade
.modal-dialog
.modal-content
.modal-header
button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
h4.modal-title Title
.modal-body
p body
p #{foo}
.modal-footer
button.btn.btn-default(type='button', data-dismiss='modal') Close
button.btn.btn-primary(type='button') Save changes

Extjs 4: how to make a text field input to an editable div?

I have a text field (or time/date field) with mask. __:__:__ I have to change the color of "_" from black to white. Has Extjs 4 any solution to change input to (editable) div?
Have you tried overriding the base fieldSubTpl property for your field? I.e, adding the property with a as a div element instead of input:
fieldSubTpl: [ // note: {id} here is really {inputId}, but {cmpId} is available
'<div id="{id}" type="{type}" {inputAttrTpl}',
' size="1"', // allows inputs to fully respect CSS widths across all browsers
'<tpl if="name"> name="{name}"</tpl>',
'<tpl if="value"> value="{[Ext.util.Format.htmlEncode(values.value)]}"</tpl>',
'<tpl if="placeholder"> placeholder="{placeholder}"</tpl>',
'{%if (values.maxLength !== undefined){%} maxlength="{maxLength}"{%}%}',
'<tpl if="readOnly"> readonly="readonly"</tpl>',
'<tpl if="disabled"> disabled="disabled"</tpl>',
'<tpl if="tabIdx"> tabIndex="{tabIdx}"</tpl>',
'<tpl if="fieldStyle"> style="{fieldStyle}"</tpl>',
' class="{fieldCls} {typeCls} {editableCls}" autocomplete="off"/>',
{
disableFormats: true
}
],
Now I am here, using this tpl:
fieldSubTpl: [
'<div stlye="position: inherit; top: 0px; left: 0px" id="{id}-div" contentEditable='+!this.disableSel+' type="{type}" {inputAttrTpl}',
' size="1"',
'<tpl if="value"> value="{[Ext.util.Format.htmlEncode(values.value)]}"</tpl>',
'<tpl if="placeholder"> placeholder="{placeholder}"</tpl>',
'<tpl if="tabIdx"> tabIndex="{tabIdx}"</tpl>',
'<tpl if="fieldStyle"> style="{fieldStyle}"</tpl>',
' class="{fieldCls} {typeCls} {editableCls}" autocomplete="off">{[Ext.util.Format.htmlEncode(values.value)]}</div>' +
'<input type="text" id="{id}" value="{[Ext.util.Format.htmlEncode(values.value)]}" ' +
'style="visibility: hidden; height: 0px; width: 0px; " />',
{
disableFormats: true
}
]
And I made a new plugin extended the original, change all this.inputTextElement.value to get or set HTML
this.getPicker().on('select', function (p, r, o){
Ext.get(this.id + '-inputEl-div').setHTML(r.data.disp);
}, this);
And on some other event the hidden input and div change their values, but it's not the ultimate version of my sollution. Now I have lots of work yet... Huh... :)
And if you know other idea, please post it!
An other solution I'm trying:
buildDivsByFormat: function () {
// 12: 'h:i A', '03:15 PM'. 'h:i:s.u A' 24: 'H:i' 'H:i:s.u' instead.
var w = 8;
var divs = {};
if(this.format.indexOf("i") > -1)
divs.i = '<div style="padding:0px 0px 1px 0px;width:'+w*2+'px;min-width:'+w*2+'px;max-width:'+w*2+'px;float:left;display:inline-block;"' +
contentEditable="'+!this.disableSel+'" id="'+this.id+'-div-i'+'"> </div>';
... // making this with other format elements
var d = '';
var formatArray = this.format.split('');
for(var i = 0; i < formatArray.length; i = i + 1){
var currentChar = formatArray[i];
if(formatArray[i] === ":") currentChar = 'dp';
if(formatArray[i] === ".") currentChar = 'p';
if(formatArray[i] === " ") currentChar = 'sp';
d = d + divs[currentChar];
}
this.divs = d;
return d;
}
Making divs by format and inserting them to this tpl's div:
fieldSubTpl: [
'<div stlye="position: inherit; top: 0px; left: 0px" id="{id}-div" type="{type}" {inputAttrTpl}',
' size="1"',
'<tpl if="value"> value="{[Ext.util.Format.htmlEncode(values.value)]}"</tpl>',
'<tpl if="placeholder"> placeholder="{placeholder}"</tpl>',
'<tpl if="tabIdx"> tabIndex="{tabIdx}"</tpl>',
'<tpl if="fieldStyle"> style="{fieldStyle}"</tpl>',
' class="{fieldCls} {typeCls} {editableCls}" autocomplete="off">' +
'</div>' +
'<input type="text" id="{id}" value="{[Ext.util.Format.htmlEncode(values.value)]}" ' +
'style="visibility: hidden; height: 10px; width: 0px; " />',
{
disableFormats: true
}
]
Then I do some changing function... I have some problems: I can't coloring this version, but... bigger problem is: if input is visibility: hidden;, it makes space after my tpl's div... on display: none makes time picker to absolute 0 0 position of window, not to the field (div)... Has anybody an idea about this?

Blueprint CSS last column not working correctly

I have a 10 column blueprint setup and having trouble with havingtwo divs each be 5 columns wide. The second one always jumps down under the first one, even though I have "last" in the second div.
Here's the code:
<div class="container">
<div class="span-5">
<ul><li><p><strong>Discounts on professional development programs that protect your interests.</strong> We monitor state and national policy issues that affect our industry and take action on our members' behalf. These efforts ensure that our members have a powerful voice and strong representation on the issues that matter.</p></li></ul>
</div>
<div class="span-5 last">
<ul><li><p><strong>Marketing and promotional opportunities and management and operational resources.</strong> You get access to useful tools as well as valuable insights and expert perspectives on industry issues and trends that will help your business succeed.</p></li></ul>
</div>
</div>
Here's part of the CSS:
== STRUCTURE: ========================
* Page width: 640 px
* Number of columns: 10
* Column width: 55 px
* Margin width: 10 px
======================================
By default, the grid is 640px wide, with 10 columns
spanning 55px, and a 10px margin between columns.
If you need fewer or more columns, use this formula to calculate
the new total width:
Total width = (number_of_columns * column_width) - margin_width
-------------------------------------------------------------- */
/* A container should group all your columns. */
.container {
width: 640px;
margin: 0 auto;
}
/* Use this class on any div.span / container to see the grid. */
.showgrid {
background: url(src/grid.png);
}
/* Body margin for a sensile default look. */
body {
margin:1.5em 0;
}
/* Columns
-------------------------------------------------------------- */
/* Sets up basic grid floating and margin. */
div.span-1, div.span-2, div.span-3, div.span-4, div.span-5, div.span-6, div.span-7, div.span-8, div.span-9, div.span-10 {float:left;margin-right: 10px;}
/* The last column in a row needs this class. */
div.last { margin-right: 0; }
/* Use these classes to set the width of a column. */
.span-1 { width: 55px;}
.span-2 { width: 120px;}
.span-3 { width: 185px;}
.span-4 { width: 250px;}
.span-5 { width: 315px;}
.span-6 { width: 380px;}
.span-7 { width: 445px;}
.span-8 { width: 510px;}
.span-9 { width: 575px;}
.span-10, div.span-10 { width: 640px; margin: 0; }

When use conditional in jade template, render result get wrong

First I want render code is:
ul
li
a
the render result should be
<ul>
<li><a></li>
</ul>
then I add conditional:
ul
- if (temp == "blog") {
li.active
- } else {
li
- }
a
but the render result is
<ul>
<li.active></li>
<a>
</ul>
what's wrong with my code? How can I get the same render result as the first one?
Try this:
ul
- if temp === "blog"
li.active
a
- else
li
a
If you prefer not to duplicate the nested a, you can use:
ul
li(class = (temp === 'blog') ? 'active' : '')
a
Also useful for menu lists and tabs, you can inline nesting like this:
ul
li: a
li: a.active
li: a
// ...etc