cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

I can't filter a table by the value taken from a ComboBox

emorales
Explorer
0 Kudos
366

Hello community! First of all, thank you very much for reading me. I'm having a problem that is surely very simple but I can't find the reason for what is happening. I have a ComboBox like this:

<ComboBox
    items="{path:'/TipoAuditoria'}"
    selectedKey="{filters>TiposDeAuditoria}"
    selectionChange="onSubmitFilter"
    enabled="{= !${viewModel>/hasUIChanges}}"
>
    <items>
        <core:Item key="{ID}" text="{DESCRIPCION}" />
    </items>
</ComboBox>

 In the controller my filtering logic is as follows:

onSubmitFilter: function (oEvent) {
    const sValue = oEvent.getSource().getValue(); // Input value
    const sKey = oEvent.getSource().getSelectedKey(); // ComboBox value

    let oView = this.getView();
    let oModel = oView.getModel("filters");
    oModel.setProperty("/AuditoriaDescripcion", sValue);
    oModel.setProperty("/TiposDeAuditoria", sKey);

    // Filter logic
    let sAuditoria = oModel.getProperty("/AuditoriaDescripcion");
    let sTiposDeAuditoria = oModel.getProperty("/TiposDeAuditoria");
    let aFilters = [];

    if (sAuditoria) {
        aFilters.push(new Filter("DESCRIPCION", FilterOperator.Contains, sAuditoria));
    }

    if (sTiposDeAuditoria) {
        aFilters.push(new Filter("TIPOAUDITORIA_ID", FilterOperator.EQ, sTiposDeAuditoria));
    }

    this._oAuditoriaTable.getBinding("items").filter(aFilters);
}

Filtering doesn't work, the "getSelectedKey()" method doesn't seem to bring me any value. I don't know what mistake I'm making. I'm sharing the "filters" model just in case:

_loadFilters: function () {
    const oViewModelFilters = new JSONModel({
        AuditoriaDescripcion: "",
        TiposDeAuditoria: ""
    });

    this.getView().setModel(oViewModelFilters, "filters");
}

I appreciate the help, I need this to finish my view. Thanks!

Accepted Solutions (0)

Answers (3)

Answers (3)

Akhilesh_u
Explorer
0 Kudos

Hi,

I think you are passing combobox fields ID and DESC to table data, whereas DESC field is not there in your table data and this DESC filter causing the issue, pass ID only to filter. Also this ID value should be there in your table data in a respective field.

//remove below filter, because DESCRIPTION field is from combobox model not from table model
    if (sAuditoria) { 
        aFilters.push(new Filter("DESCRIPCION", FilterOperator.Contains, sAuditoria));
    }

//value of ID field from combobox model should be there in the field TIPOAUDITORIA_ID of table model 
    if (sTiposDeAuditoria) { 
        aFilters.push(new Filter("TIPOAUDITORIA_ID", FilterOperator.EQ, sTiposDeAuditoria));
    }

I have prepared working solution for this scenario, Check this link 

Hope this helps.

 

 

Radoslaw_Kiela
Participant
0 Kudos

Hello,

Maybe here's the problem...

selectedKey="{filters>TiposDeAuditoria}"

path should be preceded by "/"

selectedKey="{filters>/TiposDeAuditoria}"

Regards,
Radoslaw

junwu
SAP Champion
SAP Champion
0 Kudos
selectedKey="{filters>/TiposDeAuditoria}"

1.you are missing the / in the binding

2. get the selected key from model after you correct the binding, no need to get selectkey from ui control.

 

emorales
Explorer
0 Kudos

Hi junwu! Thanks for your help, you were right. The code looks like this:

onSubmitFilter: function (oEvent) {
    const sValue = oEvent.getSource().getValue();
    const sKey = oEvent.getSource().getSelectedKey();

    console.log('sKey:' + sKey);

    let oView = this.getView();
    let oModel = oView.getModel("filters");
    oModel.setProperty("/AuditoriaDescripcion", sValue);
    oModel.setProperty("/TiposDeAuditoria", sKey);

    // Lógica de filtrado
    let sAuditoria = oModel.getProperty("/AuditoriaDescripcion");
    let sTiposDeAuditoria = oModel.getProperty("/TiposDeAuditoria");
    let aFilters = [];

    console.log('sTiposDeAuditoria:' + sTiposDeAuditoria);

    if (sAuditoria) {
        aFilters.push(new Filter("DESCRIPCION", FilterOperator.Contains, sAuditoria));
    }

    if (sTiposDeAuditoria) {
        aFilters.push(new Filter("TIPOAUDITORIA_ID", FilterOperator.EQ, sTiposDeAuditoria));
    }

    this._oAuditoriaTable.getBinding("items").filter(aFilters);
}

 The "console.log" on line #5 correctly prints the value.
The "console.log" on line #17 only prints the value if I set the value on line #10, otherwise the filters model does not have the value. Do you realize why this happens to me?
Also, even though I have managed to set the value and used the "sTiposDeAuditoria" variable, the filtering on line #24 does not work for me, the table is shown like this:

emorales_0-1723215289969.png

I don't understand what's happening

junwu
SAP Champion
SAP Champion
0 Kudos
1. check what the http request is sending to server. 2. is your odata service able to do filtering?