美好的一天
下面的代码将仓库列表填充到下拉列表中,这是我想要的,但我也想在填充仓库时选择默认选择isMainWarehouse = true
仓库(请注意,只有一个主仓库) )
function PopulateWarehouses() {
var warehouseDropDown = document.getElementById('SelectWarehouse');
var warehouseFragment = document.createDocumentFragment();
_warehouseRepository.getWarehouses().done(function (data) {
for (var element in data.items) {
if (data.items.hasOwnProperty(element)) {
var recievedData = data.items[element];
var id = recievedData.id;
var name = recievedData.warehouseName;
var main = recievedData.isMainWarehouse;
var opt = document.createElement('option');
opt.innerHTML = name;
opt.value = id;
warehouseFragment.appendChild(opt);
}
}
warehouseDropDown.appendChild(warehouseFragment);
$('#SelectWarehouse').selectpicker('refresh');
});}
这是我的下拉列表
<select id="SelectWarehouse" name="SelectWarehouse" class="selectpicker show-tick form-control" data-live-search="true"></select>
此方法只获取所有仓库的列表
// GET: get list of all warehouses
public async Task<ListResultOutput<WarehouseListDto>> GetWarehouses()
{
var warehouses = await _warehouseRepository.GetAllListAsync();
ListResultOutput<WarehouseListDto> dto = new ListResultOutput<WarehouseListDto>(
warehouses
.OrderBy(t => t.WarehouseName)
.ToList()
.MapTo<List<WarehouseListDto>>()
);
return dto;
}
答案 0 :(得分:4)
要选择主要仓库,您只需设置主仓库选项的选定属性
function PopulateWarehouses() {
var warehouseDropDown = document.getElementById('SelectWarehouse');
var warehouseFragment = document.createDocumentFragment();
_warehouseRepository.getWarehouses().done(function (data) {
for (var element in data.items) {
if (data.items.hasOwnProperty(element)) {
var recievedData = data.items[element];
var id = recievedData.id;
var name = recievedData.warehouseName;
var main = recievedData.isMainWarehouse;
var opt = document.createElement('option');
opt.innerHTML = name;
if(main){
opt.selected = 'selected';
}
opt.value = id;
warehouseFragment.appendChild(opt);
}
}
warehouseDropDown.appendChild(warehouseFragment);
$('#SelectWarehouse').selectpicker('refresh');
});}