Openlayers Wfs-删除功能的事务问题,看起来我从功能 属性 中遗漏了一些东西,因为响应说 o 已删除
Openlayers Wfs- transaction problem deleting features, it looks i'm missing something from the feature property because the response says o deleted
这是我的代码
var selectFeat = new ol.interaction.Select({
condition: ol.events.condition.click,
hitTolerance: 3,
});
map.addInteraction(selectFeat);
// var selectedFeat = selectFeat.getFeatures();
function deleteFeatures() {
var selectedFeat = selectFeat.getFeatures();
if (selectedFeat.getLength() > 0) {
var toDeleteFeat = selectFeat.getFeatures().getArray()[0];
// wfsSource.removeFeature(toDeleteFeat);
// selectFeat.getFeatures().remove(toDeleteFeat);
// selectFeat.getFeatures().on("change:length", function (e) {
selectFeat.on("select", function (e) {
transactWFS("delete", e.target.item(0));
});
wfsSource.removeFeature(toDeleteFeat);
selectFeat.getFeatures().remove(toDeleteFeat);
} else
window.alert("Please select a layer first :" + selectedFeat.getLength());
}
Document.getElementById("delete").addEventListener("click", function () {
deleteFeatures();
})
交易功能-
////////transact function //////////////
let transactWFS = function (p, f) {
let WFSTSerializer = new ol.format.WFS();
let formatGML = {
featureNS: "Flori",
featureType: ["floriii"],
srsName: "urn:x-ogc:def:crs:EPSG:4326", //kshtu eshte formati i xml
//outputFormat: "application/json",
//format_options='callback:loadFeatures'
};
switch (p) {
case "insert":
node = WFSTSerializer.writeTransaction([f], null, null, formatGML);
break;
case "update":
node = WFSTSerializer.writeTransaction(null, [f], null, formatGML);
break;
case "delete":
node = WFSTSerializer.writeTransaction(null, null, [f], formatGML);
break;
}
let serializer = new XMLSerializer();
let featString = serializer.serializeToString(node);
console.log(node);
console.log(featString);
let request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080/geoserver/ows");
request.setRequestHeader("Content-Type", "text/xml");
//console.log(request);
request.send(featString);
};
Openlayers Wfs- 事务删除功能问题,看起来我从功能中遗漏了一些东西 属性 因为响应说 o 已删除。
**I figure out that the problem was at the request and actually didnt need an select event :**
function deleteFeatures() {
var selectedFeat = selectFeat.getFeatures();
if (selectedFeat.getLength() > 0) {
var toDeleteFeat = selectFeat.getFeatures().getArray()[0];
//wfsSource.clear();
console.log(toDeleteFeat.geometryName);
transactWFS("delete", toDeleteFeat);
} else
window.alert("Please select a layer first :" + selectedFeat.getLength());
}
**And the request must be like this;**
$.ajax("http://localhost:8080/geoserver/Flori/ows?", {
service: "WFS",
type: "POST",
dataType: "xml",
processData: false,
contentType: "text/xml",
data: featString,
}).done(function () {
// wfsSource.refresh();
wfsSource.clear();
});
这是我的代码
var selectFeat = new ol.interaction.Select({
condition: ol.events.condition.click,
hitTolerance: 3,
});
map.addInteraction(selectFeat);
// var selectedFeat = selectFeat.getFeatures();
function deleteFeatures() {
var selectedFeat = selectFeat.getFeatures();
if (selectedFeat.getLength() > 0) {
var toDeleteFeat = selectFeat.getFeatures().getArray()[0];
// wfsSource.removeFeature(toDeleteFeat);
// selectFeat.getFeatures().remove(toDeleteFeat);
// selectFeat.getFeatures().on("change:length", function (e) {
selectFeat.on("select", function (e) {
transactWFS("delete", e.target.item(0));
});
wfsSource.removeFeature(toDeleteFeat);
selectFeat.getFeatures().remove(toDeleteFeat);
} else
window.alert("Please select a layer first :" + selectedFeat.getLength());
}
Document.getElementById("delete").addEventListener("click", function () {
deleteFeatures();
})
交易功能-
////////transact function //////////////
let transactWFS = function (p, f) {
let WFSTSerializer = new ol.format.WFS();
let formatGML = {
featureNS: "Flori",
featureType: ["floriii"],
srsName: "urn:x-ogc:def:crs:EPSG:4326", //kshtu eshte formati i xml
//outputFormat: "application/json",
//format_options='callback:loadFeatures'
};
switch (p) {
case "insert":
node = WFSTSerializer.writeTransaction([f], null, null, formatGML);
break;
case "update":
node = WFSTSerializer.writeTransaction(null, [f], null, formatGML);
break;
case "delete":
node = WFSTSerializer.writeTransaction(null, null, [f], formatGML);
break;
}
let serializer = new XMLSerializer();
let featString = serializer.serializeToString(node);
console.log(node);
console.log(featString);
let request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080/geoserver/ows");
request.setRequestHeader("Content-Type", "text/xml");
//console.log(request);
request.send(featString);
};
Openlayers Wfs- 事务删除功能问题,看起来我从功能中遗漏了一些东西 属性 因为响应说 o 已删除。
**I figure out that the problem was at the request and actually didnt need an select event :**
function deleteFeatures() {
var selectedFeat = selectFeat.getFeatures();
if (selectedFeat.getLength() > 0) {
var toDeleteFeat = selectFeat.getFeatures().getArray()[0];
//wfsSource.clear();
console.log(toDeleteFeat.geometryName);
transactWFS("delete", toDeleteFeat);
} else
window.alert("Please select a layer first :" + selectedFeat.getLength());
}
**And the request must be like this;**
$.ajax("http://localhost:8080/geoserver/Flori/ows?", {
service: "WFS",
type: "POST",
dataType: "xml",
processData: false,
contentType: "text/xml",
data: featString,
}).done(function () {
// wfsSource.refresh();
wfsSource.clear();
});