QueryBuilderColumn.Operators is null in Create event

Unless you have assigned a List<ListModel> to the QueryBuilderColumn.Operators parameter its value is always null, even though the set of default operators is set on the column. Would it be possible to assign the default operator list to QueryBuilderColumn.Operators? Would be really helpful to have this available in the Create event to allow customisation of the available operators by columns. Currently we have to maintain copy of the list of the defaults operators by datatype and assign this to each column so its available in the Create event.

3 Replies 1 reply marked as answer

MK Mohan Kumar Ramasamy Syncfusion Team December 16, 2020 12:39 PM UTC

Hi Michael, 
 
We have checked your requirement. We can set customize operator in Querybuilder Column tag. Please refer below code snippets. 
 
 
<SfQueryBuilder @ref="QueryBuilderObj" TValue="EmployeeDetails"> 
            <QueryBuilderEvents Created="Created" TValue="EmployeeDetails"></QueryBuilderEvents> 
            <QueryBuilderColumns> 
                <QueryBuilderColumn Field="EmployeeID" Label="Employee ID" Type="ColumnType.Number" Operators="Operators"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="FirstName" Label="First Name" Type="ColumnType.String" Operators="customOperators"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="TitleOfCourtesy" Label="Title of Courtesy" Type="ColumnType.Boolean" Values="Values"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="Title" Label="Title" Type="ColumnType.String" Operators="customOperators"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="HireDate" Label="Hire Date" Type="ColumnType.Date"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="Country" Label="Country" Type="ColumnType.String" Operators="customOperators"></QueryBuilderColumn> 
                <QueryBuilderColumn Field="City" Label="City" Type="ColumnType.String"></QueryBuilderColumn> 
            </QueryBuilderColumns> 
        </SfQueryBuilder> 
     
 
    @code { 
        SfQueryBuilder<EmployeeDetails> QueryBuilderObj; 
        private void Created() 
        { 
            
        } 
        public List<OperatorsModel> Operators = new List<OperatorsModel> { 
        new OperatorsModel { Text="Equal", Value="equal"}, 
        new OperatorsModel { Text="Not equal", Value="notequal"}, 
        new OperatorsModel { Text="Greater than", Value="greaterthan"}, 
        new OperatorsModel { Text="Less than", Value="lessthan"}, 
        new OperatorsModel { Text="Less than or equal", Value="lessthanorequal"}, 
        new OperatorsModel { Text="Greater than or equal", Value="greaterthanorequal"} 
    }; 
        public List<OperatorsModel> customOperators = new List<OperatorsModel> { 
        new OperatorsModel { Text="Equal", Value="equal"}, 
        new OperatorsModel { Text="Not equal", Value="notequal"} 
    }; 
 
        private string[] Values = new string[] { "Mr.", "Mrs." }; 
        public class EmployeeDetails 
        { 
            public int EmployeeID { get; set; } 
            public string FirstName { get; set; } 
            public bool TitleOfCourtesy { get; set; } 
            public string Title { get; set; } 
            public DateTime HireDate { get; set; } 
            public string Country { get; set; } 
            public string City { get; set; } 
        } 
 
    } 
 
 
 
For your reference, we have prepared a sample based on this. Please refer below link. 
 
 
 
Please let us know, if you need any further assistance. 
 
Regards, 
Mohan kumar R 


Marked as answer

HA HappyCamper July 28, 2021 01:59 PM UTC

Mohan,

Your example above works great if you are hardcoding your QueryBuilderColumns.... However, your solution isn't viable if you are trying to create dynamic columns.  In a dynamic column scenario, you would typically try to build your column ui as per the below:

<SfQueryBuilder TValue="ExpandoObject" @ref="Querybuilder">
                                    <QueryBuilderColumns>
                                        @foreach (DataGridColumn item in DataGridColumns)
                                        {
                                            <QueryBuilderColumn Field="@item.FieldName" Label="@item.HeaderString" [email protected]/>
                                        }
                                    </QueryBuilderColumns>
</SfQueryBuilder>

The foreach iterates some sort of list which provides the properties required to build the column.  However, if you have a property of type List<OperatorsModel> and try to assign it to the Operators property above, it does not work.  How would you go about specifying the operators directly?  Again, your sample above isn't viable as the operators are essentially still hardcoded and does not support a dynamic methodology.  In essence you would want to be able to specify specific operators, per column... and each column could have its own custom operators.  The number of columns are unknown until runtime.


GK Gayathri KarunaiAnandam Syncfusion Team July 30, 2021 03:17 AM UTC

Hi HappyCamper, 

We have checked your reported query. We have prepared a sample in which columns are created dynamically  based on your requirement. We can add custom Operators to Querybuilder in Dynamic Columns. Please check the below code snippet. 


 
<SfQueryBuilder TValue="ExpandoObject">  
    <QueryBuilderColumns>  
    @{   
        foreach(var column in columns)  
        {  
            <QueryBuilderColumn Field="@column.Field" Label="@column.Label" Type="@column.Type" Operators="@column.Operators"></QueryBuilderColumn>  
        }  
    }  
    </QueryBuilderColumns>  
</SfQueryBuilder>  
  
@code { 
    private List<QueryBuilderColumn> columns = new();  
    private static string[] Values = new string[] { "Mr.", "Mrs." };  
 
     public List<OperatorsModel> CustomOperators = new List<OperatorsModel> { 
        new OperatorsModel { Text="Equal", Value="equal"}, 
        new OperatorsModel { Text="Not equal", Value="notequal"} 
    }; 
 
    protected override async Task OnInitializedAsync()  
  
    {  
        columns = new List<QueryBuilderColumn>()  
    {  
        new QueryBuilderColumn(){ Field="EmployeeID", Label="Employee ID", Type=ColumnType.Number, Operators=CustomOperators},  
        new QueryBuilderColumn(){ Field="FirstName", Label="First Name", Type=ColumnType.String, Operators=CustomOperators},  
        new QueryBuilderColumn(){ Field="TitleOfCourtesy", Label="Title of Courtesy", Type=ColumnType.Boolean, Values=Values },  
  
    };  
        await base.OnInitializedAsync();  
    } 
 
} 

For your convenience, we have prepared a sample based on this. Please check the sample. 


Please get back to us, if you need further assistance. 

Regards, 
Gayathri K 


Loader.
Up arrow icon