Dashcode - how do you combine two values from one datasource

邮差的信 提交于 2020-01-17 03:05:21

问题


In Dashcode, if I have a dataSource that has, for example, 2 fields called 'FirstName' and 'Last Name', how do I concatenate the 2 fields into one text field in a list view?

I'm fairly sure it must be to use a value transformer, so say that I assign the 'FirstName' field to the textfield, and add a value transformer... how do I then add the 'LastName' value to the 'value' variable in the transformer.

I'm sure it's to do with dashcode.getDataSource and valueForKeyPath and I think I'm close to the solution but it all seems a bit ungainly so any help would be much appreciated.


回答1:


Correct - you need to use a Value Transformer.

In the Transformer, you would code as follows:

itemDescription = Class.create(DC.ValueTransformer,{
    transformedValue: function(value){

    var itemDataSource = dashcode.getDataSource('itemsList'); // The Data Source Name here
    var lastName = itemDataSource.selection().valueForKey('lastName'); // Presumes you have a field called lastName
    return value + " " + lastName;
    }
});

Hope this helps - I battled with this for a day!!!




回答2:


For future googlers, since there is absolutely no documentation anywhere about this :

When in detailed view to concatenate two fields from same the datasource :

XML

<?xml version="1.0" encoding="utf-8"?>
<immobilier>    
  <bien>
    <ID>1453</ID> 
    <Titre>Maison / Villa F4</Titre>
    <Ville>Sainte Clotilde</Ville>
    <Quartier>BRETAGNE</Quartier>
  </bien>
</immobilier>

To combine the fields Ville and Quartier create a value transformer like so :

mapAdresse = Class.create(DC.ValueTransformer,{
    transformedValue: function(value){

        if (value.trim() != "") {
            //Replace immoListe with your source name
            var itemDataSource = dashcode.getDataSource('immoListe');

            //THIS IS THE MOST IMPORTANT : HOW TO FIND THE CURRENTLY SELECTED ITEM INDEX 
            var selectedIndex = document.getElementById('list').selectedIndex; 

            //Use the selectedIndex to find the record in the datasource
            var quartier = itemDataSource.selection().valueForKey("bien")[selectedIndex].valueForKey("Quartier");

            //Concatenate to your liking
            if (quartier.trim() != "") value = value + ", "+ quartier;
        }

        return value;
    }
});

Why is this not documented anywhere ?? Beats me !!



来源:https://stackoverflow.com/questions/5518400/dashcode-how-do-you-combine-two-values-from-one-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!