我有一个用户输入的表单并获取该条目并查询数据库并返回到视图。我设法让一个查询工作,但在尝试处理另一个查询时,它在视图中返回一个未定义的变量错误。见下文:
路线
var allItems:[Item]
var selectedItems[Item]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == allItemsCV {
let item = allItems[IndexPath.row]
if let itemIndex = selectedItems.index(of:item) {
selectedItems.remove(at:itemIndex)
selectedItemsCV.deleteItems(at: [IndexPath(item: itemIndex, section:0)])
} else {
selectedItems.insert(item, at: 0)
selectedItemsCV.insertItems(at: [IndexPath(item: 0, section:0)])
}
allItemsCV.deselectItem(at: indexPath, animated: true)
// cell for item at indexPath needs to render the selected/deselected
state correctly based on the item being in the selectedItems array
allItemsCV.reloadItems(at: [indexPath])
}
}
@IBAction func removeButtonPressed(_ sender: UIButton) {
//...
if let selectedItemPaths = selectedItemCV.indexPathsForSelectedItems {
var allItemIndexPaths = [IndexPath]()
var tempSelectedItems = Array(selectedItems) // Need to use a temporary copy otherwise the element indexes will change
for itemPath in selectedItemPaths {
let removeItem = selectedItems[itemPath.item]
if let removeIndex = tempSelectedItems.index(of: removeItem) {
tempSelectedItems.remove(at: removeItem)
}
if let allItemsIndex = allItems.index(of: removeItem) {
allItemIndexPaths.append(IndexPath(item: allItemsIndex, section: 0))
}
}
selectedItems = tempSelectedItems // Selected items array without the removed items
selectedItemsCV.deleteItems(at:selectedItemPaths)
allItemsCV.reloadItems(at: allItemIndexPaths) // Reload to update the selected status
}
}
控制器
Route::get('/shopsales', 'shopsalescontroller@index');
{
class shopsalescontroller extends Controller
}
shopales view
public function index()
{
$storeNum = request('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',compact('result'));
}
我已经使用了正在运行的查询的确切代码,正在努力理解为什么不这样做。
答案 0 :(得分:0)
试试这个
import { Component } from '@angular/core';
import Chart from 'chart.js';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
title = 'Test';
let myChart = new Chart({});
};
中的
App\Route
这会路由以下操作:Route::resource('/shopsales','ShopSalecontroller');
,index
,create
,store
,show
,edit
和update
将此功能添加到shopales模型
destroy
<{1>}索引中的:
public function scopeNumber($query, $number){
if($number != null){
$query->where('storeNumber','=', "$number");
}
}
请记住在索引视图中包含字段storeNumber
的表单答案 1 :(得分:0)
public function index()
{
$storeNum = request('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',['result'=>compact('result')]);
}
更改上面的控制器代码。 还要检查$ result是否为null。
答案 2 :(得分:0)
尝试这样的事情
public function storeCheckout(Request $request) {
$storeNum=$request->get('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',compact('result'));
}
答案 3 :(得分:0)
解决了,虽然很抱歉浪费你的时间,我的一些观点应该有一个大写字母,修复了清除视图缓存,现在我有查询。
答案 4 :(得分:-1)
在行动指数中 将var $ result更改为$ results
在视图索引中 @foreach($结果为$ result) @endforeach