Dojox Grid pass two fields to formatter - ruby-on-rails

I created a dojox.Grid successfully, but in one case I need to pass two fields to a formatter function instead of just one.
For instance:
{
field: 'id',
name: 'Id',
formatter: formatterFunction,
},
I need to pass to formatterFunction() both 'id' and 'name' for instance. How can I do this?
Thank you.

I know this was already mentioned in the IRC channel, but I'm answering here so others are aware, and also to address your further question that I'm not sure anyone answered.
New in 1.4 If you set the value of the field to "_item", then your formatter will be called with the entire item from the store - instead of just one field value
This makes it possible to do what you want using a formatter as well.
http://www.dojotoolkit.org/reference-guide/dojox/grid/DataGrid.html#usage
In the simplest case, without setting the grid's formatterScope, the grid's store can be accessed from within formatters via this.grid.store, e.g.:
function fmtItem(value) {
var store = this.grid.store;
return store.getValue(value, 'id') + ': ' + store.getValue(value, 'name');
}
Here's a really simple example of the above formatter in action:
http://jsbin.com/upico4/edit
There's also an example of this in one of the test pages, which creates an object to hold and scope the formatters:
http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_grid_formatters.html

As from dojo 1.4 you can also get multiple fields from a store. Should look something like:
var layout = [{
rows: [
{name: 'Title', fields:['Title', 'url'], formatter:formatLink}
]}]
function formatLink(value){
return ''+value[0]+'';
}
That uses the value from the field "url" to point your link at and the displayed title is filled with the data from "Title" field in your store.

Are you sure that you want to format and maybe not use get instead?
When you use a formatter the only value that is passed to the function is the value that field represents.
However, if you were to use get instead, you could use the item to access the other values. (However then you will lose sorting).
So for your column have
{
field: 'id',
name: 'Id',
get: getFunction
},
Then have
getFunction: function(index,row) {
return row.id + row.name;
}

function formatterFunction(val, rowIdx, cell){
var name=this.name,
field=this.field;
}

Related

Referencing another field in a domain function (Odoo12)

I want to restrict the account lookup (domain) based on the value entered in GL Prefix (I'll actually use some wildcards and some other logic which I'm comfortable adding later), the problem is that I'm getting a logical True or False value returned by self.x_poLineGLprefix rather than the value in the field. How do I get the actual data value of x_poLineGLprefix?
class QuickPOLine(models.Model):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'
x_poLineGLprefix = fields.Char(string='GL Prefix')
x_poLineGLaccount = fields.Many2one(
'account.account', string="Line Item Expense Account",
domain=lambda self: [('code', '=', self.x_poLineGLprefix)])
Try this
#api.onchange('x_poLineGLprefix')
def onchange_x_poLineGLprefix(self):
if self.x_poLineGLprefix:
return {'domain': {
'x_poLineGLaccount': [('code', '=', self.x_poLineGLprefix)]
}}
You can add dynamic domain to achieve this based on any field. In #api.onchange() function you can return domain for many2one. To add dynamic domain you can refer this link. If you are using both many2one fields refer this link.

How to dynamically set binding type's "formatOptions" and "constraints" in XML with binding?

I have a list of elements (OData set) and use a binding to show this list.
One field is for a quantity value and this value could sometimes need some decimal places.
The requirement is: only show that amount of decimal numbers that is also available in the OData service.
Annotation techniques can't be used.
I 'hacked' something that is misusing a formatter to update the type of a binding. But this is 'a hack' and it is not possible to convert it to XML views. (The reason is a different handling of the scope the formatter will be called).
So I am searching for a working solution for XML views.
The following code would not work but shows the issue:
new sap.m.Input({ // looking for an XML solution with bindings
value: {
path: "Quantity",
type: new sap.ui.model.type.Float({
// formatOptions
maxFractionDigits: "{QuantityDecimals}",
// ...
}, {
// constraints
minimum: 0
}),
// ...
}
});
The maxFractionDigits : "{QuantityDecimals}" should be "dynamic" and not a constant value.
Setting formatOptions and constraints dynamically in XML (via bindings or a declared function) is unfortunately not (yet) supported. But IMHO this is a valid enhancement request that app developers would greatly benefit from, since it encourages declarative programming.
I already asked for the same feature some years ago but in a comment at https://github.com/SAP/openui5/issues/2449#issuecomment-474304965 (See my answer to Frank's question "If XMLViews would allow a way to specify the dynamic constraints as well (e.g. enhanced syntax), would that fix the problem?").
Please create a new issue via https://github.com/SAP/openui5/issues/new with a clear description of what kind of problems the feature would resolve and possibly other use cases (You can add a link to my comment). I'll add my 👍 to your GitHub issue, and hopefully others too.
I'll update this answer as soon as the feature is available.
Get your dynamic number from your model and store it in a JS variable.
var nQuantityDecimals = this.getModel().getProperty("/QuantityDecimals");
new sap.m.Input({
value : {
path : "Quantity",
type : new sap.ui.model.type.Float({
maxFractionDigits : nQuantityDecimals,
source : {
groupingSeparator: ",",
decimalSeparator: ".",
groupingEnabled: false
}
}, {
minimum:0
})
}
}),

extjs 3.4 ComboBox send wrong data type

this is the problem.
I have a store in which I have a fiels without typization named al_key
The al_key actually comes as an int from the server
{ al_key: 5512, description: "test"}
I load those data into a gridpanel, then I edit the Record with a Form that takes the Record from the row. In the form I have a combobox named "AL VALUE" preselected with the al_key key.
{
xtype: 'combo',
triggerAction: 'all',
store: 'AlStore',
forceSelection: true,
allowBlank: true,
editable: false,
fieldLabel: 'AL VALUE',
name: 'al_key',
hiddenName: 'al_key',
displayField: 'text',
valueField: 'id',
disabled: true
}
Now, the problem is: when I load the Record (getForm().loadRecord(rec)) the field al_key is a number and when I submit the form it sends a number.
When I change the value of the combo the fiel al_key becomes a STRING and it sends a STRING!
How can I force to use integer?
Thank you Al.
I'm sorry...
It seems a problem of the FormPanel.
When I call : this.page.dataGrid.store.insert(0, new this.page.dataGrid.store.recordType(this.getForm().getValues()));
the this.getForm().getValues() returns this object:
al_key: "4088"
cod_aerom: "1458"
WHY??!
SOLVED!
THe problem is that the FORM doesn't know about the store configuration and it passes all data as a normal form.
So, if I fill the store in this way:
this.page.dataGrid.store.recordType(this.getForm().getValues()));
it will insert all strings.
Here is my workaround..
MyRecordType = Ext.data.Record.create(this.page.dataGrid.store.fields.keys);
var myRec = new MyRecordType();
this.getForm().updateRecord(myRec);
this.page.dataGrid.store.add(myRec);
this.page.dataGrid.store.save();
tnx!!
A.

How to dynamically set Datatables search and column filters state via url params?

Let's say I have two models: customers and orders.
Every order belongs to a customer.
I also set a orders (index) view with a Datatable showing all the orders.
If I want to see all the orders for a given customer all I have to do is to filter the results for that customer writing the concerning customer name in the "search" field or selecting that customer from its column filter field.
Now I'd like to implement a customer-orders link which automatically shows the orders Datatable with those settings.
In other words, I'd like that a link to /orders?isearch='customer_name+customer_surname' will show the Datatable with the customer full name already written in the search field (or the customer column filter set accordingly).
Of course I can fetch the #customer instance variable from the controller and pass it to the view, but then?
Using the oSearch variable in the initialization code I can define an initialization value for the search field, the isearch variable:
$(document).ready( function() {
var isearch = $('#isearch').val()
$('#example').dataTable( {
"oSearch": {"sSearch": isearch}
} );
} )
The isearch value is stored in a proper hidden field in the view:
<%= hidden_field_tag "isearch", #isearch.to_s, { :id => "isearch" } %>
(This is the best way I found so far to pass an instance variable to a .js file in Rails).
Last but not least, the #isearch instance variable is set from the order_controller:
#isearch = params[:isearch]
Of course its value should be passed to the url like:
.../orders?isearch='customer_name+customer_surname'
----ADDENDUM----
I figured out I don't need an instance variable, in fact I can pass as many url params as I want using this javascript function:
function getUrlParam( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1].replace("+", " ");
}
Then I can fetch them from my .js file like this:
// fetches url params
var isearch = getUrlParam('isearch');
var customer_name = getUrlParam('customer_name');
var order_status = getUrlParam('order_status');
Accordin to these values I not only can set the oSearch variable but also a column-specific using Datatables aoSearchCols initialization parameter ( http://www.datatables.net/ref#aoSearchCols )
The last problem to solve, given that I am using individual column filtering with select elements (http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html), so set every select element according to the URL-passed params.
I did it with this jQuery instruction:
// sets all select filters according to url passed params
$('select').val([customer_name, preparation_kind, preparation_status]);
From the performance point of view this solution is improvable, but its easy and it works.

AutoIncrement fields other than Id in grails

I'm new to Grails. I have a field in my domain object which I define as an String.
String ponum
When i click first time on create page ,the "ponum" field has to display 0001 and later it has to save in database.And When i click on second time the "ponum" field has to display 0002 and later it has to save in database.Everytime i click on create page "ponum" field has to autoincrement and save it database.I google ,but i did not get any document.
thanks
As things get an id anyway (assuming your using a regular rdbms with standard id genaerator), why not just pretend there's a variable ponum which is based on the id?
Just add a getter to your domain class:
class Page {
String getPonum() {
String.format( "%04d", id )
}
}
According to this answer, it is not possible to use hibernate generators (those used for ids) to do that. If you really need to use Hibernate generators, a workaround is described in the answer as well.
In your case, you could use an interceptor to generator your ponum property when inserting a new object:
class Yours {
int ponum
def beforeInsert() {
def lastPonum = Book.list([sort: 'ponum', order:'desc', max: 1])
if(lastPonum)
ponum = (lastPonum.pop().ponum as int) + 1 as String
else
ponum = '0'
}
}

Resources