远程过滤;没有 return 值
Remote filtering; no return value
Extjs 5.0
在网格中,我有一个带文本字段的收费栏,用于过滤网格(远程)。
工作正常。
但是,如果我使用数据库中不存在的词进行过滤(例如'mywxtyz'),则会出现错误:Ext.JSON.decode (): You're trying to decode an无效 JSON 字符串,输出为空。
我想要的是当搜索没有 return 任何记录(数据库中不存在)、简单消息或网格空文本中的文本或 window 时,是显示。
我该怎么做?
提前致谢。
我的代码:
onButtonFilter : function(button, newValue, oldValue, eOpts){
var me = this;
var textfield = Ext.ComponentQuery.query('#filter')[0];
var grid = Ext.ComponentQuery.query('#griditemId')[0];
var store = grid.getStore();
if (form.isValid()) {
store.proxy.extraParams = {
action : 'filterGrid',
name : textfield.getValue()
},
store.load();
}
}
我的例子PHP:
<?php
include("conexion.php");
$action = $_REQUEST['action'];
switch($action){
case "create":{
}
case "read":{
$start = $_REQUEST['start'];
$limit = $_REQUEST['limit'];
$statement = $conexao->stmt_init();
$sqlQuery = "SELECT name, email FROM contact LIMIT ?, ?";
$sqlTotal = "SELECT COUNT(name) as num FROM contact";
if(isset($_REQUEST['action']) AND $_REQUEST['action'] == 'filterGrid') {
$name = $_REQUEST['name'];
$sqlQuery = "SELECT name, email
FROM contact
WHERE name like '%$name%'
LIMIT ?, ?";
$sqlTotal = "SELECT COUNT(name) as num
FROM contact
WHERE name LIKE '%$name%'";
}
if ($statement = $conexao->prepare($sqlQuery)){
$statement->bind_param("ii", $start, $limit);
$statement->execute();
$statement->bind_result($col1, $col2);
while($statement->fetch()){
$output[] = array($col1, $col2);
};
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
$sucess = array("success" => mysqli_errno($conexao) == 0);
echo json_encode(array(
"success" => $sucess,
"total" => $total,
"data" => $output
));
$conexao->close();
break;
}
case "update":{
}
case "destroy":{
}
}
?>
店铺:
Ext.define('APP.store.StoreList', {
extend: 'Ext.data.Store',
itemId:'mystore',
model: 'SPP.model.ModelList',
pageSize: 25,
autoLoad:true,
autoLoad: {start: 0, limit: 25},
autoSync: false,
proxy: {
type: 'ajax',
actionMethods:{
create:'POST',
read:'POST',
update:'POST',
destroy:'POST'
},
api: {
create: 'php/crudActionList.php?action=create',
read: 'php/crudActionList.php?action=read',
update: 'php/crudActionList.php?action=update',
destroy: 'php/crudActionList.php?action=destroy'
},
reader: {
type: 'json',
rootProperty: 'data',
totalProperty:'total',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
rootProperty: 'data'
}
},
});
我认为这里的问题是当没有找到匹配项时 $output 和 $total 变量为空。也许如果你初始化它并在 while 语句之前将它设置为一个空数组,那就可以了。
所以
while($statement->fetch()){
$output[] = array($col1, $col2);
};
应该是
$output = [];
while($statement->fetch()){
$output[] = array($col1, $col2);
};
整篇也一样。提前将 $total 设置为 0。
所以下面
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
应该是
$total = 0;
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
更新:要向用户提供未返回任何记录的消息,您需要使用加载函数的回调选项。
http://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.data.Store-method-load
所以,像下面这样的东西应该可以工作:
store.load({
scope: this,
callback: function(records, operation, success) {
if(records.length === 0){
Ext.Msg.alert("Nothing Found", "No records found");
}
}
});
Extjs 5.0
在网格中,我有一个带文本字段的收费栏,用于过滤网格(远程)。
工作正常。
但是,如果我使用数据库中不存在的词进行过滤(例如'mywxtyz'),则会出现错误:Ext.JSON.decode (): You're trying to decode an无效 JSON 字符串,输出为空。
我想要的是当搜索没有 return 任何记录(数据库中不存在)、简单消息或网格空文本中的文本或 window 时,是显示。
我该怎么做?
提前致谢。
我的代码:
onButtonFilter : function(button, newValue, oldValue, eOpts){
var me = this;
var textfield = Ext.ComponentQuery.query('#filter')[0];
var grid = Ext.ComponentQuery.query('#griditemId')[0];
var store = grid.getStore();
if (form.isValid()) {
store.proxy.extraParams = {
action : 'filterGrid',
name : textfield.getValue()
},
store.load();
}
}
我的例子PHP:
<?php
include("conexion.php");
$action = $_REQUEST['action'];
switch($action){
case "create":{
}
case "read":{
$start = $_REQUEST['start'];
$limit = $_REQUEST['limit'];
$statement = $conexao->stmt_init();
$sqlQuery = "SELECT name, email FROM contact LIMIT ?, ?";
$sqlTotal = "SELECT COUNT(name) as num FROM contact";
if(isset($_REQUEST['action']) AND $_REQUEST['action'] == 'filterGrid') {
$name = $_REQUEST['name'];
$sqlQuery = "SELECT name, email
FROM contact
WHERE name like '%$name%'
LIMIT ?, ?";
$sqlTotal = "SELECT COUNT(name) as num
FROM contact
WHERE name LIKE '%$name%'";
}
if ($statement = $conexao->prepare($sqlQuery)){
$statement->bind_param("ii", $start, $limit);
$statement->execute();
$statement->bind_result($col1, $col2);
while($statement->fetch()){
$output[] = array($col1, $col2);
};
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
$sucess = array("success" => mysqli_errno($conexao) == 0);
echo json_encode(array(
"success" => $sucess,
"total" => $total,
"data" => $output
));
$conexao->close();
break;
}
case "update":{
}
case "destroy":{
}
}
?>
店铺:
Ext.define('APP.store.StoreList', {
extend: 'Ext.data.Store',
itemId:'mystore',
model: 'SPP.model.ModelList',
pageSize: 25,
autoLoad:true,
autoLoad: {start: 0, limit: 25},
autoSync: false,
proxy: {
type: 'ajax',
actionMethods:{
create:'POST',
read:'POST',
update:'POST',
destroy:'POST'
},
api: {
create: 'php/crudActionList.php?action=create',
read: 'php/crudActionList.php?action=read',
update: 'php/crudActionList.php?action=update',
destroy: 'php/crudActionList.php?action=destroy'
},
reader: {
type: 'json',
rootProperty: 'data',
totalProperty:'total',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
rootProperty: 'data'
}
},
});
我认为这里的问题是当没有找到匹配项时 $output 和 $total 变量为空。也许如果你初始化它并在 while 语句之前将它设置为一个空数组,那就可以了。
所以
while($statement->fetch()){
$output[] = array($col1, $col2);
};
应该是
$output = [];
while($statement->fetch()){
$output[] = array($col1, $col2);
};
整篇也一样。提前将 $total 设置为 0。
所以下面
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
应该是
$total = 0;
if ($statement = $conexao->query($sqlTotal)){
$row = $statement->fetch_row();
$total = $row[0];
}
更新:要向用户提供未返回任何记录的消息,您需要使用加载函数的回调选项。 http://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.data.Store-method-load
所以,像下面这样的东西应该可以工作:
store.load({
scope: this,
callback: function(records, operation, success) {
if(records.length === 0){
Ext.Msg.alert("Nothing Found", "No records found");
}
}
});