I am using OpenLayers, Geoserver 2.1 CQL language. The problem is that I do not know how to address the layer. I am trying to call a function like this:
var createWmsLayer = function(name, url, params, options) {
map.addLayer(new OpenLayers.Layer.WMS(name, url, params, options));
};
I am creating the layer like this:
var ikke_listet = new createWmsLayer("<span class='lc'>Ikke rødlistet</span>",
"http://kart.naturkart.no/geoserver/wms",
{layers: 'gbif_ns:ikke_listet',format: 'image/png',
transparent: 'true'}, {singleTile: true, ratio: 1,
isBaseLayer:false, visibility: false});
Then I am calling a function on the layer to redraw it with CQL params to GeoServer.
function CQLfilter(param){
filter_list = []
filter_list.append("collector LIKE '%H%' ")
ikke_listet.mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})
But I get this error in Firebug:
ikke_listet is not defined
So my question is: What is the name of my layer?
I am using the "name" part of my function to write out some styles for the layerlist (I use ExtJS) and put a padding and different icons in the layerlist to show what icons represent which layer. Is this a wrong way to do this styling?
Any suggestions as to what I use in the function to add CQL call to the layer and redraw it?
Aragon, I was able to get the layername from your suggestion of the layer array:
function CQLfilter(param){
var mLayers = map.layers;
mLayers[9].mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})
// This gave me the names of the layers
// for(var a = 0; a < mLayers.length; a++ ){
// alert(mLayers[a].name + '#' + a + '#')
//};
}
So I wonder if there is another way to style my layer list because now I get the layernames like this:
"<span class='lc'>Ikke rødlistet</span>"
. But of course I can still get to the layer by referencing the layer array position.
I managed to run the function now and it filters correctly. I only had to ignore the first base 4 layers and I have to find out how to collect the values from the search form. But manually this code now works and I have much progress :-)
function CQLfilter(param){
var mLayers = map.layers;
// have to get the search by document.getElementById or jQuery I guess. Testing manually now
var search = "kingdom LIKE '%Plantae%' ";
for(var a = 4; a < (mLayers.length); a++ ){
alert(mLayers[a].name + '#' + a + '#')
mLayers[a].mergeNewParams({'CQL_FILTER': search})
};
}
Hi again, I am able to get the layer now. Works fine with this function on the CQL filter and the reset filter function at the end.
function CQLfilter(sok,cat){
var mLayers = map.layers;
switch (cat)
{
case 'SN':
param = "scientificname LIKE '%"+ sok + "%' ";
break;
case 'NNG':
param = "norsknavngruppe LIKE '%"+ sok + "%' ";
break;
case 'NNA':
param = "norsknavnart LIKE '%"+ sok + "%' ";
break;
case 'PROJ':
param2 = new Array()
param2.push("prosjektnummer LIKE '%" + sok + "%'")
param2.push("prosjektnavn LIKE '%" + sok + "%'")
param = param2.join(" OR ");
break;
default:
param2 = new Array()
param2.push("scientificname LIKE '%" + sok + "%'")
param2.push("norsknavngruppe LIKE '%" + sok + "%'")
param2.push("norsknavnart LIKE '%" + sok + "%'")
param2.push("collector LIKE '%" + sok + "%'")
param = param2.join(" OR ");
break;
}
for(var a = 4; a < (mLayers.length); a++ ){
mLayers[a].mergeNewParams({'CQL_FILTER': param});
mLayers[a].setVisibility(true);
}}
function resetCQLfilters() {
var mLayers = map.layers;
for(var a = 4; a < (mLayers.length); a++ ){
delete mLayers[a].params.CQL_FILTER;
mLayers[a].redraw();
}}