单一输入的多标准形式,是否可能?

时间:2017-05-02 07:20:31

标签: javascript angularjs symfony ionic-framework

我的产品搜索表单有一个服务器端功能,可以使用多个参数,如名称,来源,产品编号和类别。

如果可能的话,我希望能够使用我的表格,并能够通过一次输入根据这些不同的标准得到结果。

我的代码:

在我的service.js中:

var searchProduct = function(form){
    var formdata = new FormData();
    for (var key in form) {
        console.log(key, form[key]);
        formdata.append('form['+key+']', form[key]);
    }
        return $http({
                method  : 'POST',
                url     : url + '/search',
                data    : formdata,
                transformRequest: angular.identity
           }).then(function(result) {
                return result.data;
        },function errorCallback(response) {
         });
};

在我的controller.js中:

$scope.search = function () {
          $ionicLoading.show();
          var myform = angular.copy($scope.form);
          myform.category = (myform.groupe) ? myform.category.id : "";
          AppService.searchProduct(myform).then(function(response){
            $scope.list = response;
            $ionicLoading.hide();;
          });
      };

在我的HTML中:

<form ng-controller="searchCtrl" id="search" class="list" name="myForm" ng-submit="search()" ng-disabled="myForm.$invalid">
      <label class="item item-input" id="search" style="">
        <i class="icon ion-search placeholder-icon"></i>
        <input placeholder="Product" type="text" ng-model="form.name" required>
      </label> 

      <ion-list id="menu-list1">
        <ion-item ng-if="list.length<=0" style="text-align: center;">No result</ion-item>
        <ion-item ng-repeat="item in list track by $index" ng-click="showProduct(item)">{{ item.name }}</ion-item>
      </ion-list>
    </form>

在我的服务器(Symfony)中:

 /**
 * @Route("/api/search", name="api_product_search")
 * @Method("POST")
 * @param Request $request
 */
public function searchAction(Request $request)
{
    if (!$request->isMethod("POST")) {
        $response = new JsonResponse("POST only");
        $response->setStatusCode(Response::HTTP_BAD_REQUEST);
        return $response;
    }

    if (sizeof($request->request->get('form')) == 0) {
        $response = new JsonResponse("This API requires at least one search criterion");
        $response->setStatusCode(Response::HTTP_BAD_REQUEST);
        return $response;
    }

    $products = $this->getDoctrine()->getRepository('AppBundle:product')->search($request->request->get('form'));

    $results = [];
    foreach ($products as $product) {
        $results[] = [
            'id' => $product[0]->getId(),
            'name' => $product[0]->getName(),
            'origin' => $product[0]->getOrigin(),
            'productNumber' => $product[0]->getProductNumber(),
            'category' => $product['category'],
        ];
    }

    return new JsonResponse($results);
}

0 个答案:

没有答案