如何检查 jsgrid 中的 inserted/edited 项是否重复?
How can I check the inserted/edited item in jsgrid isn't duplicate?
我尝试为 jsgrid 构建自定义验证器,在插入或编辑之前检查 item.Name 是否真的存在于数据库中以避免任何重复的行。
这个问题我把jsgrid的文档看了三遍还是不知道怎么解决。
这项工作https://github.com/tabalinas/jsgrid-php对我帮助太大了,我转变为对我的工作和mysqli有用。
想法:
如果值存在或不存在,我使函数检查数据库,如果存在它 return true else false in a php class
public function checkName($name){
try{
if(!empty($name)){
$co_conn = $this->CON_conn->open_connect();
$getsame = mysqli_query($co_conn,"select Name from category where Name = '$name'");
if($getsame){
$numit = mysqli_num_rows($getsame);
if($numit > 0) return true;
else return false;
}
else return true;
$this->CON_conn->close_connect($co_conn);
}
else return true;
}
catch(Exception $er){
return true;
}
}
我在外部文件中调用了这个函数"CheckValue.php"
<?php require_once '////the director of the class';
$chnow = new NameOfMyClass();
$result = true;
switch($_SERVER["REQUEST_METHOD"]) {
case "CHGO":
parse_str(file_get_contents("php://input"), $_CHGO);
//to ensure if it work i tested it.
$result = $chnow->checkName($_CHGO["Name"]);
break;
default:
$result = true;
break;
}
header('Content-type: application/json');
echo json_encode($result);
?>
在basic.html
$(function() {
$.ajax({
type: "GET",
url: "../bin/update.php"
}).done(function(response) {
response.unshift({ ID: "0", Name: "" });
$("#jsGrid").jsGrid({
height: "50%",
width: "70%",
selecting: false,
filtering: false,
editing: false,
sorting: false,
paging: true,
autoload: true,
pageSize: 15,
pageButtonCount: 5,
controller: {
loadData: function(filter) {
return $.ajax({type:"GET", url: "../bin/update.php",data:filter});
},
updateItem: function(item) {
return $.ajax({type:"PUT",url: "../bin/update.php",data: item});
},
insertItem: function(item) {
/*$.ajax({type: "CHGO",url: "..bin/Checkvalue.php",data:item.Name, dataType: "json",
success: function(data){
JSON.parse(data);
consol.log(data);
//alert(data);
//I used many ways to find why its not work, then I commented it
}
});
//return $.ajax({type: "POST",url: "../bin/update.php",data: item});
//return data;
//else sweetAlert("error", "this value is exist!", "error");
//here I thought merge sweetalert js will give a good look of work not the normal message.
*/
}
},
fields: [
{ name: "ID", type: "number",width: 50, editing:false, inserting:false },
{ name: "Name", type: "text", width: 50, validate: ["required",{
message: "this value is exist",
validator: function(value,item){
if(value != "")
{
$.ajax({
type: "CHGO",
url: "..bin/Checkvalue.php",
data: item.Name,
success: function(data){
if(data == true)
//I assume here return the true/false to activate the error message but nothing work..sorry
}
});
}
}
} ]
},
{ type: "control", modeSwitchButton: false, deleteButton: false }
]
});
});
$(".config-panel input[type=checkbox]").on("click", function() {
var $cb = $(this);
$("#jsGrid").jsGrid("option", $cb.attr("id"), $cb.is(":checked"));
});
});
请帮忙,我试了2天来解决这个问题。
感谢开发人员编写验证 js 文件
我用的是jsgrid-1.4.1
原因是validate
只支持同步验证,而我们例子中的验证需要ajax调用,所以是异步的。
您有两个选择:
- 预先将数据加载到客户端并在客户端验证插入项目(因此验证将是同步的)
- 验证
insertItem
方法中的插入。查看以下问题以了解更多详细信息 https://github.com/tabalinas/jsgrid/issues/190
我尝试为 jsgrid 构建自定义验证器,在插入或编辑之前检查 item.Name 是否真的存在于数据库中以避免任何重复的行。
这个问题我把jsgrid的文档看了三遍还是不知道怎么解决。
这项工作https://github.com/tabalinas/jsgrid-php对我帮助太大了,我转变为对我的工作和mysqli有用。
想法: 如果值存在或不存在,我使函数检查数据库,如果存在它 return true else false in a php class
public function checkName($name){
try{
if(!empty($name)){
$co_conn = $this->CON_conn->open_connect();
$getsame = mysqli_query($co_conn,"select Name from category where Name = '$name'");
if($getsame){
$numit = mysqli_num_rows($getsame);
if($numit > 0) return true;
else return false;
}
else return true;
$this->CON_conn->close_connect($co_conn);
}
else return true;
}
catch(Exception $er){
return true;
}
}
我在外部文件中调用了这个函数"CheckValue.php"
<?php require_once '////the director of the class';
$chnow = new NameOfMyClass();
$result = true;
switch($_SERVER["REQUEST_METHOD"]) {
case "CHGO":
parse_str(file_get_contents("php://input"), $_CHGO);
//to ensure if it work i tested it.
$result = $chnow->checkName($_CHGO["Name"]);
break;
default:
$result = true;
break;
}
header('Content-type: application/json');
echo json_encode($result);
?>
在basic.html
$(function() {
$.ajax({
type: "GET",
url: "../bin/update.php"
}).done(function(response) {
response.unshift({ ID: "0", Name: "" });
$("#jsGrid").jsGrid({
height: "50%",
width: "70%",
selecting: false,
filtering: false,
editing: false,
sorting: false,
paging: true,
autoload: true,
pageSize: 15,
pageButtonCount: 5,
controller: {
loadData: function(filter) {
return $.ajax({type:"GET", url: "../bin/update.php",data:filter});
},
updateItem: function(item) {
return $.ajax({type:"PUT",url: "../bin/update.php",data: item});
},
insertItem: function(item) {
/*$.ajax({type: "CHGO",url: "..bin/Checkvalue.php",data:item.Name, dataType: "json",
success: function(data){
JSON.parse(data);
consol.log(data);
//alert(data);
//I used many ways to find why its not work, then I commented it
}
});
//return $.ajax({type: "POST",url: "../bin/update.php",data: item});
//return data;
//else sweetAlert("error", "this value is exist!", "error");
//here I thought merge sweetalert js will give a good look of work not the normal message.
*/
}
},
fields: [
{ name: "ID", type: "number",width: 50, editing:false, inserting:false },
{ name: "Name", type: "text", width: 50, validate: ["required",{
message: "this value is exist",
validator: function(value,item){
if(value != "")
{
$.ajax({
type: "CHGO",
url: "..bin/Checkvalue.php",
data: item.Name,
success: function(data){
if(data == true)
//I assume here return the true/false to activate the error message but nothing work..sorry
}
});
}
}
} ]
},
{ type: "control", modeSwitchButton: false, deleteButton: false }
]
});
});
$(".config-panel input[type=checkbox]").on("click", function() {
var $cb = $(this);
$("#jsGrid").jsGrid("option", $cb.attr("id"), $cb.is(":checked"));
});
});
请帮忙,我试了2天来解决这个问题。 感谢开发人员编写验证 js 文件 我用的是jsgrid-1.4.1
原因是validate
只支持同步验证,而我们例子中的验证需要ajax调用,所以是异步的。
您有两个选择:
- 预先将数据加载到客户端并在客户端验证插入项目(因此验证将是同步的)
- 验证
insertItem
方法中的插入。查看以下问题以了解更多详细信息 https://github.com/tabalinas/jsgrid/issues/190