Extjs 5.0
在网格中,我有一个带有文本字段的收费栏,用于过滤网格(远程)。
工作正常。
但是,如果我使用数据库中不存在的单词进行过滤(例如'mywxtyz'),则会出现错误:Ext.JSON.decode():您正在尝试解码无效的JSON字符串,输出为空。
我想要的是当搜索没有返回任何记录(数据库中不存在),显示简单消息或网格emptyText或窗口中的文本时。
我该怎么做?
提前致谢。
我的代码:
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'
}
},
});
答案 0 :(得分:1)
我认为这里的问题是,当找不到匹配项时,$ 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];
}
更新:要向用户提供没有返回记录的消息,您需要使用load函数的回调选项。 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");
}
}
});