我在Symfony工作,试图对其进行一些修改,简单地添加另一个条件显示,但我遇到了一堵墙;主要是因为我不熟悉序列化和AJAX
目前,我有一种非常简单的方法,可以根据系统中已有的条形码选择扫描的条形码。目前,这可以很好地工作,因为它只是检查是否有任何行,如果有任何行,则生成一个错误方法。
我现在试图检查结果的状态值,以返回不同的消息。
以下是迄今为止我所掌握的有效方法:
以下是我使用的方法:
public function indexCountAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$barcode = $request->query->get('barcode');
$items = $em->getRepository('MyBundle:Items\Item')
->findByBarcode($barcode);
$response = new Response();
$response->setContent($this->getSerializer()->serialize($items, 'json'));
return $response;
}
细枝中的jquery是这样的(路径
item_index_count
只需调用上述方法)
<script type="text/javascript">
var verifyBarcode = function() {
if($('#items_item_barcode').val().length != 0){
$('#items_item_barcode').prop("readonly", true);
$('#barcode-buttons').hide();
jQuery.getJSON('{{ path('item_index_count') }}?' +
$.param({barcode: $('#items_item_barcode').val()}))
.done(checkedBarcode);
}
};
$("#verify-barcode").click(verifyBarcode);
var checkedBarcode = function(items) {
if(items.length == 0){
... bla bla.. this works fine if there's no existing rows ..
} else {
//这是不起作用的部分。由于某种原因,它无法识别
item_status
-一些研究表明,即使只有一行(总是这样;它的“单个条形码扫描”)具有这种状态,它也总是返回“新”而从不“请求”。
if(items.item_status == 'req'){
$('#items_item_barcode').prop("disabled", true);
$('#requested-barcode').removeClass('hidden');
}
//这个很好用
else
{
$('#items_item_barcode').prop("disabled", true);
$('#existing-barcode').removeClass('hidden');
}
}
};
我尝试将item\u status变量引入到细枝中,这似乎是问题的一部分,因为它总是返回item\u状态
new
无论项目的状态如何,仍然不会引入条形码。
这是树枝的主体:
<section id="requested-barcode" class="hidden">
<div class="panel-body callout">
<header>This item is a request.</header>
---this is section never appears, even if it has status req
</div>
<div class="panel-body">
<button type="button" class="btn btn-default" onclick="location.reload(true)">Cancel</button>
</div>
</section>
<section id="existing-barcode" class="hidden">
This section seems to work.... mostly. This text shows
But this line always seems to bring back the same status each time, regardless of the actual status:
{{ item.getItemStatus }}
</div>
</section>
我很确定这是一个相当简单的问题,我很确定这个问题在jquery中,但是这对我来说是一个弱点,我不确定在哪里可以找到答案。FWIW,正在执行
var_dump
属于
$response
或
$items
似乎什么都没做(Symfony让我有点疯狂;错误似乎消失了)
编辑:
使用
{{ dump(item) }}
为我提供以下内容。出于某种原因,它没有得到任何东西,但它得到了一个争吵,但只有
item_status = 'new'
这是完全错误的。
然而,它是在正确的时间抓住了一场争吵;仅当存在现有行时才会生成:
object(MyBundle\Entity\Items\Item)#485 (17) { ["id":protected]=> NULL
["title":protected]=> NULL
["type":protected]=> NULL
["barcode":protected]=> NULL
["item_status":protected]=> string(3) "new"
... etc... }
在这一点上,我觉得json有问题,比如当我运行一个简单的查询来匹配
$个项目
var,我肯定得到了正确的数据。