Concealing certain columns within the Column menu based on specific criteria

I am currently working with the Kendo Column Menu feature on a Kendo grid. My goal is to hide the menus for columns where the title is blank when using the third option, which is the 'Column' option for hiding/showing columns.

Specifically, I need to hide the RefValue4 and RefValue5 columns because their corresponding values are null in the database, making it unnecessary to display them.

My current approach looks like this:

if (grid.dataSource.data()[0].RefName4 == null) {
    grid.hideColumn(18);
}

Unfortunately, I have not been successful in achieving the desired result.

Answer №1

Are you considering only checking the content of the first row in the grid, as shown in your code sample? If so, you can set up a dataBound handler to hide columns based on this criteria:

dataBound : function (e) {
    var grid = e.sender;
    var data = e.sender.dataSource.data();
    if (data.length > 0) {
        $.each(grid.options.columns, function (idx, elem) {
            if (!data[0][elem.field]) {
                grid.hideColumn(idx);
            }
        });
    }
}

This code snippet executes every time new data is received from the server. However, it currently only checks the content of the first row. You can easily adapt this logic to check all rows. One limitation is that it doesn't hide column titles from the menu.

For a live demonstration, visit: http://jsfiddle.net/OnaBai/XNcmt/67/

UPDATE: To ensure that columns with no data are not displayed and do not appear in the menu, adjust the columns configuration within the grid setup. Here's an example of how to achieve this dynamically after receiving data:

// Fetch data from the DataSource
ds.fetch(function (d) {
    var columns = [];
    var definitions = [
        { field: "Id", hidden: true },
        { field: "FirstName", title: "First Name" },
        { field: "LastName", title: "Last Name" },
        { field: "City" },
        { field: "Position" }
    ];
    $.each(definitions, function(idx, elem) {
        if(d.items[0][elem.field]) {
            columns.push(elem);
        }
    });
    $("#grid").kendoGrid({
        dataSource: d.items,
        editable  : false,
        pageable  : true,
        columnMenu: true,
        columns   : columns
    }).data("kendoGrid");
});

View this solution in action here: http://jsfiddle.net/OnaBai/XNcmt/69/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to extract precise information from MongoDB with the help of Mongoose?

I am facing an issue with retrieving and displaying data from MongoDB Atlas to use in my Discord bot replies. Below is the code I used to submit the data: const subregis = "!reg ign:"; client.on("message", msg => { if (msg.content.includes(subregis)) ...

Having trouble applying styles to a component using forwardRef

I'm relatively new to React and still trying to wrap my head around the component's lifecycle. However, the issue at hand is proving to be quite perplexing. One thing that confuses me is why adding "setState(10);" causes the style of the "Test" ...

"Problem with AngularJS: Unable to display data fetched from resource using ng-repeat

I am currently working on an AngularJS application that retrieves data from a RESTful API using $resource. However, I have encountered an issue where the view is not updating with the data once it is received and assigned to my $scope. As a beginner in An ...

An error will be thrown if you try to pass an array attribute of an object as a prop to a React component

I'm trying to grasp why passing an array attribute of a data object into a Component as a prop is causing issues. Is this due to my understanding of how React functions, or are there potential pitfalls in this scenario? Any insight would be greatly ap ...

Error: Collection2 validation did not pass: The field 'name' must be filled in, the field 'email' cannot be empty, and the field 'message' is mandatory

I need to set up a way for visitors to send messages through my website const mongoose = require("mongoose"); mongoose.connect("MongoDb-Connection-Uri") .then(() => { console.log("mongodb connected"); }) .catch(() => { console.log("fail ...

divide a lengthy string into several dynamic divisions

I am attempting to divide a lengthy string into multiple dynamic divs once the height of the div reaches a certain limit. Below is the code that I believed would achieve this: $(document).ready(function () { var arr = longishstring.split(' ' ...

Zero results returned for the angularjs script

I am working on enhancing my skills in angularjs, but I am facing an issue where only the categories are being displayed and the products are not showing up. There are no error messages, so I am having trouble pinpointing where the problem lies. Here is t ...

What is the process of combining two identical objects in Angular JS?

Is it possible to merge and sort two objects in Angular JS that have the same fields, notifications.fb and notifications.tw, based on their time field? ...

The process of generating an efficient build gets stuck and never finishes

I am currently facing an issue while attempting to build my React app. Whenever I execute "npm run build," the terminal gets stuck at "Creating and optimized production build..." and doesn't complete the process. I have tried running this on a system ...

Exploring ES6: Harnessing the Power of Classes

I am currently learning the ES6 syntax for classes. My background is in C#, so I apologize if my terminology is not accurate or if something seems off. For practice, I am working on building a web app using Node and Express. I have defined some routes as ...

Combining PHP code within JavaScript nested inside PHP code

I am currently facing an issue with my PHP while loop. The loop is supposed to iterate through a file, extract three variables for every three lines, and then use those variables to create markers using JavaScript. However, when I try to pass the PHP varia ...

Replicate the preceding input data by simply clicking a button

Here is some HTML and jQuery code that I am working with: $(".btn-copy").click(function() { var previousContent = $(this).prev()[0]; previousContent.select(); document.execCommand('copy'); }); <script src="https://cdnjs.cloudflare.com ...

Storing the selected options from HTML dropdown menus and automatically re-populating them after a postback

Within my form, I have a set of five dropdown lists represented as HTML select elements. The initial dropdown is populated using jQuery upon page load, and each subsequent dropdown is populated based on the selection from the previous one. Additionally, I ...

What is the method for defining the maximum selectable month in mtz.monthpicker?

(edited) Currently, I am utilizing the jquery.mtz.monthpicker plugin along with jquery. My goal is to limit the selection of future months, but it appears that there are no options similar to 'maxDate' like in jquery.ui.datepicker. $('inp ...

Save a JSON object from a URL into a JavaScript variable using jQuery

I am trying to figure out how to use the .getJSON() function to store a JSON object as a JavaScript variable. Can someone guide me through this process? var js = $.getJSON(url, function(data) {...} ); Is it necessary to include the function(data) { ...

Tips for Filling Kendo Dropdown with JSON Data

I'm facing an issue with populating a Kendo dropdown using AJAX JSON response. The dropdown list is showing up blank. Can anyone point out what I might be missing here? Your help would be greatly appreciated. Here's the corresponding HTML code: ...

User authentication using .pre save process

I have an API that accepts users posted as JSON data. I want to validate specific fields only if they exist within the JSON object. For example, a user object could contain: { "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Material UI Input Field, Present Cursor Location

Is there a way to retrieve the current cursor (caret) position in a MaterialUI text field? https://material-ui.com/components/text-fields/ I am looking to make changes to the string content at a specific index, such as inserting a character X between cha ...

Executing a windows application on the client side from aspx.cs

I have created a windows application in C# that is installed for clients. I now need to run this windows application from the client's side when a user clicks a button on an ASP.NET web application. Explanation My task involves scanning hard copies ...

Tips for efficiently handling navigation re-rendering on a Single Page Application using Vue.js

I am currently in the process of developing a Single Page Application using Vue.js. The structure involves having a template in App.vue which includes a navigation bar followed by a router-view component. When a user attempts to log in, a modal window appe ...