Research developed in Angularjs

Asked

Viewed 110 times

0

I am completely "zero" in Angularjs and need to improve the search functionality on this site https://www.nomalism.com/pesquisa#/ that was not developed by me.

Right now the search is "stuck" to the list of features that hang over the search box, not allowing a free search.

Research Code

public function querySearch()
{
    $query = Product::selectAll()->groupBy('products.id');

    if (Input::get('purpose'))
    {
        $purpose_id = Input::get('purpose');
        $query->join('product_purpose', function($join) use($purpose_id)
        {
            $join->on('product_purpose.product_id', '=', 'products.id')
                 ->where('purpose_id', '=', $purpose_id);
        });
    }

    if (Input::get('category'))
    {
        $query->where('category_id', Input::get('category'));
    }

    $available_filters = array_keys(Product::allFilters());
    foreach ($available_filters as $filter)
    {
        if (Input::has($filter))
        {
            switch ($filter)
            {
                case 'color' :

                    $colors = explode(',', Input::get('color'));
                    foreach ($colors as $i => $color)
                    {
                        $alias = "color_product_$i";
                        $query->join("color_product as $alias", function($join) use($color, $alias)
                                {
                                    $join->on("$alias.product_id", '=', 'products.id')
                                         ->where("$alias.color_id", '=', $color);
                                });
                    }

                    break;

                case 'pattern' :

                    $patterns = explode(',', Input::get('pattern'));
                    foreach ($patterns as $i => $pattern)
                    {
                        $alias = "pattern_product_$i";
                        $query->join("pattern_product as $alias", function($join) use($pattern, $alias)
                                {
                                    $join->on("$alias.product_id", '=', 'products.id')
                                         ->where("$alias.pattern_id", '=', $pattern);
                                });
                    }

                    break;

                case 'width' :

                    $widths = array_map(function($width)
                        {
                            return (string)bcdiv($width, 100, 2);

                        }, explode(',', Input::get('width')));

                    $first_width = array_shift($widths);
                    $query->where(function($query) use($first_width, $widths)
                    {
                        $query->where('dimension_x', $first_width);
                        foreach ($widths as $width)
                        {
                            $query->orWhere('dimension_x', $width);
                        }
                    });

                    break;

                case 'attribute' :

                    $attributes = explode(',', Input::get('attribute'));
                    foreach ($attributes as $i => $attribute)
                    {
                        $alias = "attribute_product_$i";
                        $query->join("attribute_product as $alias", function($join) use($attribute, $alias)
                                {
                                    $join->on("$alias.product_id", '=', 'products.id')
                                         ->where("$alias.attribute_id", '=', $attribute);
                                });
                    }

                    break;

                case 'keyword' :

                    $keywords = explode(',', Input::get('keyword'));
                    foreach ($keywords as $i => $keyword)
                    {
                        $alias = "keyword_product_$i";
                        $query->join("keyword_product as $alias", function($join) use($keyword, $alias)
                                {
                                    $join->on("$alias.product_id", '=', 'products.id')
                                         ->where("$alias.keyword_id", '=', $keyword);
                                });
                    }

                    break;

                case 'name' :

                    $names = explode(',', Input::get('name'));
                    $first_name = array_shift($names);
                    $query->where(function($query) use($first_name, $names)
                    {
                        $query->where('name', $first_name);
                        foreach ($names as $name)
                        {
                            $query->orWhere('name', $name);
                        }
                    });

                    break;

                case 'ref' :

                    $refs = explode(',', Input::get('ref'));
                    $first_ref = array_shift($refs);
                    $query->where(function($query) use($first_ref, $refs)
                    {
                        $query->where('ref', $first_ref);
                        foreach ($refs as $ref)
                        {
                            $query->orWhere('ref', $ref);
                        }
                    });

                    break;
            }
        }
    }

    // Price filter
    if (Input::has('min'))
    {
        $query->where('price_in_cents', '>=', Input::get('min')*100);
    }
    if (Input::has('max'))
    {
        $query->where('price_in_cents', '<=', Input::get('max')*100);
    }

    if (Input::get('sales'))
    {
        $query->whereOnSale();
    }

    $items = $query->where('active', true)
                   ->orderBy('updated_on_remote', 'desc')
                   ->orderBy('ref', 'asc')
                   ->paginate(Input::get('limit'));

    $items->setBaseUrl(url(Input::get('base_url')));

    return $items;
}

public function search()
{
    $items = $this->querySearch();

    Session::put('limit.search', Input::get('limit', 24));

    return View::make('partials.search-results', compact('items'));
}

I intend to change so that the search is free (fulltext search), since at this time I have a field in the products table with all the words that really matter that are searchable.

  • I believe the questioning was unclear, I opened the site but did not identify what kind of change you want to make. I believe that the query is more connected with the logic executed in its Angular application than with the screen itself. Could you try to be clearer in the goal or maybe share the code of this "query"?

1 answer

1


If I understand correctly, you want a fulltext search (for any attribute related to an object that is and screen).

With angular this is very simple, in this example there is also the possibility of ordering

function ShoppingCartCtrl($scope)  {
    
        $scope.items = [
			{Name: "Soap", Price: "25", Quantity: "10"},
			{Name: "Shaving cream", Price: "50", Quantity: "15"},
			{Name: "Shampoo", Price: "100", Quantity: "5"}
		];
			
		$scope.mySortFunction = function(item) {
			if(isNaN(item[$scope.sortExpression]))
				return item[$scope.sortExpression];
			return parseInt(item[$scope.sortExpression]);
		}
}
.bold { font-weight:bold; }

table td{
    padding: 10px;
}

table th{
    font-weight: bold;
    text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <span class="bold">Demonstrating filtering and sorting using Angular JS</span>
    <br /><br />
        <div ng-controller="ShoppingCartCtrl">        
            <div>Sort by: 
            <select ng-model="sortExpression">
					<option value="Name">Name</option>
					<option value="Price">Price</option>
					<option value="Quantity">Quantity</option>
				</select>
            </div>
			<br />
			<div><strong>Filter Results</strong></div>
			<table>
				<tr>
					<td>By Any: </td>
					<td><input type="text" ng-model="search.$" /></td>
				</tr>
				<tr>
					<td>By Name: </td>
					<td><input type="text" ng-model="search.Name" /></td>
				</tr>
				<tr>
					<td>By Price: </td>
					<td><input type="text" ng-model="search.Price" /></td>
				</tr>
				<tr>
					<td>By Quantity: </td>
					<td><input type="text" ng-model="search.Quantity" /></td>
				</tr>
			</table>
            <br />
            <table border="1">
				<thead>
					<tr>
						<th>Name</th>
						<th>Price</th>
						<th>Quantity</th>
					</tr>
				</thead>
				<tbody>
					<tr ng-repeat="item in items | orderBy:mySortFunction | filter:search">
						<td>{{item.Name}}</td>
						<td>{{item.Price | currency}}</td>
						<td>{{item.Quantity}}</td>
					</tr>
				</tbody>
			</table>
			<br />
</div>
</div>

Now you need to analyze what the real search needs are. The more information is needed the longer this search will take.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.