简体   繁体   中英

YII2: error adding javascript function to a leaflet choropleth map using YII2

I'm newbie with YII2 and I'm having a lot of problems adding Javascript functions inside object properties because I really don´t understand how the framework work in the backstage.

More to the point, I'm using Leaflet javascript library through awesome 2amigos extension but I've failed all syntactic tries to add javascript functions to my GeoJson class through clientOptions.

Lots of hours spent in vain so far...

Any help?

This is the code I'm trying

$capaProvincias = new \dosamigos\leaflet\layers\GeoJson([       
            'data' => $geojson,
            'clientOptions' => [
                    'style' => 'js:function(feature) {
                            switch (feature.properties.Cod_Prov) {
                                    case "06": return {color => "#ff0000"};
                                    case "18": return {color => "#ffff00"};
                                    case "41": return {color => "#ff00ff"};
                            }
                    }',
            ],
    ]);  

The map is rendered perfectly but without any interaction :(

Thanks in advance!!

I had a similar problem once and a Fellow here suggest me a solution that in your case would be something like this:

Note: Remember to import yii\\web\\JsExpression

 'style' => new JsExpression('function(feature) {
                            switch (feature.properties.Cod_Prov) {
                                    case "06": return {color => "#ff0000"};
                                    case "18": return {color => "#ffff00"};
                                    case "41": return {color => "#ff00ff"};
                            }
                    }',

The link to the original answer is this :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM