0

I found how to make filter function in lightning:datatable, and trying to use.

My column has so many items, so I want to make refine search method which can search rows by clicked column's data.

<lightning:input value="{!v.filter}"/>
<lightning:datatable aura:id="table"
                                     id="table"
                                     data="{!v.receive_Data}"
                                     columns="{!v.Columns}" 
                                     />
    filter: function(component, event, helper){
        let receive_Data= component.get("v.receive_Data");
        let filter = component.get("v.filter");
        let clickedColumn = component.get("v.clicked_Column");
        let results;
         try {
           const regex = new RegExp(filter, "i");
            // filter checks each row, constructs new array where function returns true
       //search only from clickedColumn
            results = receive.filter(row=>regex.test(clickedColumn));
        } catch(e) {
            // invalid regex, use full list
        }
        component.set("v.receive_Data", results);
    },

Is there any way to get clicked column's data or label?

1 Answer 1

0

I found the way to make filter by using sort function.

<aura:attribute name="clicked_Column" type="string"/>
<lightning:input value="{!v.filter}"/>
<lightning:datatable aura:id="table"
                                     id="table"
                                     data="{!v.receive_Data}"
                                     columns="{!v.Columns}" 
                                     onsort="{!c.updateColumnSorting}"
                                     />
updateColumnSorting : function(component, event, helper){
        component.set("v.clicked_Column", event.getParam("fieldName"));
    },
  filter: function(component, event, helper){
        let receive_Data= component.get("v.receive_Data");
        let filter = component.get("v.filter");
        let clickedColumn = component.get("v.clicked_Column");
        let results;
         try {
           const regex = new RegExp(filter, "i");
            // filter checks each row, constructs new array where function returns true
       //search only from clickedColumn
            results = receive.filter(row=>regex.test(row[clickedColumn]));
        } catch(e) {
            // invalid regex, use full list
        }
        component.set("v.receive_Data", results);
    },

It works for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .