This is a migrated thread and some comments may be shown as answers.

truncating the column dynamically

4 Answers 166 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Winni
Top achievements
Rank 1
Winni asked on 05 Dec 2011, 01:46 PM
Hi,

I need to display limited characters only in grid.I followed the following thread and it is working.
http://www.telerik.com/community/forums/aspnet/grid/display-limited-characters-in-radgrid.aspx


Now for diffreent resolutions and monitors this hardcoded character limit is not going well.

So i want to get the column width and number of characters fit to that .I will give the column widths in %.

I want something like if (no:of characters in the column > column width)  then
column.text=truncate till it fits in that column+"..."

Also if there is any other way  to achieve this plz tell me...

Could any body please help me..
Thanks.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2011, 02:30 PM
Hello,

Try setting Wrap property in ItemStyle as true.

-Shinu.
0
Winni
Top achievements
Rank 1
answered on 05 Dec 2011, 02:54 PM
Thanks Shinu.

But in my case the the requirement is that

after certain character the text should be truncated and appended with "...".the full value of the column is shown as tool tip.

Now I am hard coding if the column's length >15 then truncate.

Instead hard coding the 15, I want to make this figure dynamically depending on the screen width.
If the screen is big, then I will increase the 15 and display more characters.

So I thought of getting the column width dynamically and compare with column text length.

0
Antonio Stoilkov
Telerik team
answered on 08 Dec 2011, 09:25 AM
Hello Winni,

You could achieve your scenario by using JavaScript code. I have created an example that depending on the cell width truncates different amount of text. You could set different for columnWidthRatio so it can satisfy your needs.

<telerik:RadCodeBlock runat="server">
    <script>
        function pageLoad() {
            var columnWidthRatio = 8;
            var dataItems = $find("<%= RadGrid1.ClientID %>").get_masterTableView().get_dataItems();
            var notesCell;
            for (var i = 0; i < dataItems.length; i++) {
                notesCell = dataItems[i].get_cell("columnUniqueName");
                var maxTextLength = notesCell.clientWidth / columnWidthRatio;
                var currentDataItemText = notesCell.innerHTML;
                if (currentDataItemText.length > maxTextLength) {
                    notesCell.innerHTML = currentDataItemText.substring(0, maxTextLength) + "...";
                }
            }
        }
    </script>
</telerik:RadCodeBlock>


Best wishes,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Winni
Top achievements
Rank 1
answered on 08 Dec 2011, 09:49 AM
Thanks Antonio..I will try ..
Tags
Grid
Asked by
Winni
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Winni
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or