2016-02-12 57 views
0

我想創建一個角度的月份的天數的下拉列表,所選的數字將被傳遞到elasticsearch查詢中。
問題是,爲了有一個'全天'選項查詢要求所有輸入爲'*'。
代碼工作,如果我也像這樣的*顯示的所有選項,但顯然這不是對用戶來說非常可讀:角度選擇選項不能正常工作

JS

$scope.daysInMonthArray = ['*'] 
$scope.day = $scope.daysInMonthArray[0]; 

$scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
}; 

for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push(i + 1) 
} 

HTML

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day for day in daysInMonthArray"> 
</select> 

而且我有試圖用下面的方法讓它顯示'all'這個單詞,但它不起作用。它回發給查詢的只是'[object object]'。

JS

$scope.daysInMonthArray = [{'name': 'all', 'value': '*'}] 
$scope.day = $scope.daysInMonthArray[0]; 

$scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
}; 

for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push({'name': i + 1, 'value': i + 1}) 
} 

HTML

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day as day.name for day in daysInMonthArray track by day.value"> 
</select> 

我寫的第二個錯誤還是有要去這個更好的辦法?

+0

嘗試使用該選項的索引,如果選擇所有的傳球被索引,然後解僱你的查詢是指數。您也可以通過使用下拉列表的值來完成。 –

+0

您如何使用該值來查詢elasticsearch。你應該只需要使用'day.value' –

+0

在發佈我的答案之前,我沒有看到你的第二個實現。看上去不錯。我正在刪除我的答案。 –

回答

0

我們可以將天數作爲值並將標籤對象傳遞給select。這樣我們可以配置什麼進入價值和什麼進入標籤。

app.js

var n = 1; 
    $scope.year = 2016; 

    $scope.daysInMonthArray = [{'value':'*', 'label':'all'}] 
    $scope.day = $scope.daysInMonthArray[0]; 

    $scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
    } 

    for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push(
     { 
     'value':i + 1, 
     'label':i+1 
     }) 
    } 

    $scope.day = $scope.daysInMonthArray[0].value; 

因此你的選擇將是

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day.value as day.label for day in daysInMonthArray"> 
</select>