jQuery UI Widgets Forums Angular row.boundindex in createwidget not correct

Tagged: 

This topic contains 12 replies, has 4 voices, and was last updated by  chzhong1001 6 months, 2 weeks ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author

  • ilywanzi
    Participant

    Hi, I am using createwidget method to create some buttons inside one grid cell, but I found the row.boundindex from createwidget is not correct.
    For example, there are 100 rows in grid, but because of the grid height, only the first 20 rows rendered. When I click the button and print their index in console, it is correct.
    But if I scroll down, click a button in 40th row, will print the wrong index number in console because this row is the 20th row in the grid now.
    Please check this example and replace the columns with the code below, you will understand what I am talking about. When you scroll down, you can see the issue from the button index.
    Please let me know if any way I can fix this issue. Thanks.

    
    columns: any[] =
        [
          {
            text: 'Picture', datafield: 'firstname', width: 100,
            createwidget: (row: any, column: any, value: string, htmlElement: HTMLElement): void => {
              const container = document.createElement('div');
              const btn = document.createElement('button');
                  btn.innerHTML = 'btn'+row.boundindex;
                  container.appendChild(btn);
                  btn.addEventListener('click', () => {
                    console.log('row index', row.boundindex);
                  });
              htmlElement.appendChild(container);
    
            },
            initwidget: (row: number, column: any, value: any, htmlElement: HTMLElement): void => { }
          },
          { text: 'Name', datafield: 'name', width: 200 },
          { text: 'Title', datafield: 'title', width: 200 },
          { text: 'Country', datafield: 'country' }
        ];
    
    • This topic was modified 2 years, 5 months ago by  ilywanzi.

    Hristo
    Participant

    Hello ilywanzi,

    You should implement the initwidget callback for this purpose.
    The createwidget callback is for the first rendering of the rows.

    Best Regards,
    Hristo Hristov

    jQWidgets team
    https://www.jqwidgets.com


    ilywanzi
    Participant

    Hi Hristo, could you please provide a sample code for that? Thanks.


    Hristo
    Participant

    Hello ilywanzi,

    You could find the source of all demos in the download package.
    Also, with each one demo on our site, you could find the tabs with the source of the particular demo.
    Please, take a look at this demo.
    I hope this will help.

    Best Regards,
    Hristo Hristov

    jQWidgets team
    https://www.jqwidgets.com


    chzhong1001
    Participant

    Hi, this topic has another issue, the bounddata of row does not match when scrolling with createwidget method.
    Please let me know if any way can fix this issue. Thanks.
    This is demo image
    link:https://pan.baidu.com/s/1vgRQ4drVk4gQ4iaTfqsWGw
    key:ul2j


    admin
    Keymaster

    Hi,

    The createwidget is used to create the widget in the cell. The initwidget is what is called when you scroll up/down or do any further refresh of the Grid or its properties. The row’s boundindex and bounddata are up to date in the initwidget only.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/


    chzhong1001
    Participant

    Hi, admin:
    can you please provide a sample code for the row’s boundindex and bounddata are up to date in the initwidget ?
    Thanks.


    admin
    Keymaster

    Hi,

    You can look at: https://www.jqwidgets.com/angular/angular-grid/angular-grid-widgetcolumn.htm?light

    This is the code:

    import { Component } from '@angular/core';
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    export class AppComponent {
        source: any =
        {
            localdata: this.generateData(),
            datatype: 'array'
        }
        dataAdapter: any = new jqx.dataAdapter(this.source);
    	getWidth() : any {
    		if (document.body.offsetWidth < 850) {
    			return '90%';
    		}
    		
    		return 850;
    	}
        counter: number = 1;
        columns: any[] =
        [
            {
                text: 'Picture', datafield: 'firstname', width: 100,
                createwidget: (row: number, column: any, value: string, htmlElement: HTMLElement): void => {
                    const container = document.createElement('div');
                    const id = <code>myButton${this.counter}</code>;
                    container.id = id;
                    container.style.border = 'none';
                    htmlElement.appendChild(container);
                    const imgurl = './../../../images/' + value.toLowerCase() + '.png';
                    const options = {
                        width: '100%', height: 90, template: 'success',
                        imgSrc: imgurl, imgWidth: 40, imgHeight: 50, value: value,
                        imgPosition: 'center', textPosition: 'center', textImageRelation: 'imageAboveText'
                    };
                    const myButton = jqwidgets.createInstance(<code>#${id}</code>, 'jqxButton', options);
                    myButton.addEventHandler('click', function (): void {
                        const currentButtonValue = this.children[1].innerHTML;
                        alert(currentButtonValue);
                    });
                    this.counter++;
                },
                initwidget: (row: number, column: any, value: any, htmlElement: HTMLElement): void => {
                    const imgurl = './../../../images/' + value.toLowerCase() + '.png';
                    (<HTMLImageElement>htmlElement.children[1].children[0]).src = imgurl;
                    htmlElement.children[1].children[1].innerHTML = value;
                }
            },
            { text: 'Name', datafield: 'name', width: 200 },
            { text: 'Title', datafield: 'title', width: 200 },
            { text: 'Country', datafield: 'country' }
        ];
        generateData(): any[] {
            let data = new Array();
            let firstNames = ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert', 'Laura', 'Anne'];
            let lastNames = ['Davolio', 'Fuller', 'Leverling', 'Peacock', 'Buchanan', 'Suyama', 'King', 'Callahan', 'Dodsworth'];
            let titles = ['Sales Representative', 'Vice President, Sales', 'Sales Representative', 'Sales Representative', 'Sales Manager', 'Sales Representative', 'Sales Representative', 'Inside Sales Coordinator', 'Sales Representative'];
            let titleofcourtesy = ['Ms.', 'Dr.', 'Ms.', 'Mrs.', 'Mr.', 'Mr.', 'Mr.', 'Ms.', 'Ms.'];
            let birthdate = ['08-Dec-48', '19-Feb-52', '30-Aug-63', '19-Sep-37', '04-Mar-55', '02-Jul-63', '29-May-60', '09-Jan-58', '27-Jan-66'];
            let hiredate = ['01-May-92', '14-Aug-92', '01-Apr-92', '03-May-93', '17-Oct-93', '17-Oct-93', '02-Jan-94', '05-Mar-94', '15-Nov-94'];
            let address = ['507 - 20th Ave. E. Apt. 2A', '908 W. Capital Way', '722 Moss Bay Blvd.', '4110 Old Redmond Rd.', '14 Garrett Hill', 'Coventry House', 'Miner Rd.', 'Edgeham Hollow', 'Winchester Way', '4726 - 11th Ave. N.E.', '7 Houndstooth Rd.'];
            let city = ['Seattle', 'Tacoma', 'Kirkland', 'Redmond', 'London', 'London', 'London', 'Seattle', 'London'];
            let postalcode = ['98122', '98401', '98033', '98052', 'SW1 8JR', 'EC2 7JR', 'RG1 9SP', '98105', 'WG2 7LT'];
            let country = ['USA', 'USA', 'USA', 'USA', 'UK', 'UK', 'UK', 'USA', 'UK'];
            let homephone = ['(206) 555-9857', '(206) 555-9482', '(206) 555-3412', '(206) 555-8122', '(71) 555-4848', '(71) 555-7773', '(71) 555-5598', '(206) 555-1189', '(71) 555-4444'];
            let notes = ['Education includes a BA in psychology from Colorado State University in 1970.  She also completed <code>The Art of the Cold Call.</code>  Nancy is a member of Toastmasters International.',
                'Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981.  He is fluent in French and Italian and reads German.  He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993.  Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.',
                'Janet has a BS degree in chemistry from Boston College (1984).  She has also completed a certificate program in food retailing management.  Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.',
                'Margaret holds a BA in English literature from Concordia College (1958) and an MA from the American Institute of Culinary Arts (1966).  She was assigned to the London office temporarily from July through November 1992.',
                'Steven Buchanan graduated from St. Andrews University, Scotland, with a BSC degree in 1976.  Upon joining the company as a sales representative in 1992, he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London.  He was promoted to sales manager in March 1993.  Mr. Buchanan has completed the courses <code>Successful Telemarketing</code> and <code>International Sales Management.</code>  He is fluent in French.',
                'Michael is a graduate of Sussex University (MA, economics, 1983) and the University of California at Los Angeles (MBA, marketing, 1986).  He has also taken the courses <code>Multi-Cultural Selling</code> and <code>Time Management for the Sales Professional.</code>  He is fluent in Japanese and can read and write French, Portuguese, and Spanish.',
                'Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company.  After completing a course entitled <code>Selling in Europe,</code> he was transferred to the London office in March 1993.',
                'Laura received a BA in psychology from the University of Washington.  She has also completed a course in business French.  She reads and writes French.',
                'Anne has a BA degree in English from St. Lawrence College.  She is fluent in French and German.'];
            let k = 0;
            for (let i = 0; i < firstNames.length; i++) {
                let row = {};
                row['name'] = firstNames[k] + ' ' + lastNames[k];
                row['firstname'] = firstNames[k];
                row['lastname'] = lastNames[k];
                row['title'] = titles[k];
                row['titleofcourtesy'] = titleofcourtesy[k];
                row['birthdate'] = birthdate[k];
                row['hiredate'] = hiredate[k];
                row['address'] = address[k];
                row['city'] = city[k];
                row['postalcode'] = postalcode[k];
                row['country'] = country[k];
                row['homephone'] = homephone[k];
                row['notes'] = notes[k];
                data[i] = row;
                k++;
            }
            return data;
        }
    }

    In initwidget the row, column, value and htmlElement parameters are the actual ones.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/


    chzhong1001
    Participant

    Hi, admin:
    Thanks for your reply! I’ve seen this code,but the issue what the bounddata of row does not match when scrolling with createwidget method does not resolved.
    The code of mine like this:

    createwidget: (row: number, column: any, value: string, htmlElement: HTMLElement): void => {
    const container = document.createElement(‘div’);
    const id = myButton${this.counter};
    container.id = id;
    container.style.border = ‘none’;
    htmlElement.appendChild(container);
    const imgurl = ‘./../../../images/’ + value.toLowerCase() + ‘.png’;
    const options = {
    width: ‘100%’, height: 90, template: ‘success’,
    imgSrc: imgurl, imgWidth: 40, imgHeight: 50, value: value,
    imgPosition: ‘center’, textPosition: ‘center’, textImageRelation: ‘imageAboveText’
    };
    const myButton = jqwidgets.createInstance(#${id}, ‘jqxButton’, options);
    myButton.addEventHandler(‘click’, function (): void {
    const currentButtonValue = this.children[1].innerHTML;
    alert(row.boundindex); // does not match when scrolling
    alert(row.bounddata); // does not match when scrolling

    alert(currentButtonValue);
    });
    this.counter++;
    },
    initwidget: (row: number, column: any, value: any, htmlElement: HTMLElement): void => {
    const imgurl = ‘./../../../images/’ + value.toLowerCase() + ‘.png’;
    (<HTMLImageElement>htmlElement.children[1].children[0]).src = imgurl;
    htmlElement.children[1].children[1].innerHTML = value;
    }


    admin
    Keymaster

    Hi chzhong1001,

    You cannot use row.bounddata or row.boundindex in createwidget. It is called only once in order to create the widget. The row instance after you scroll in the createwidget is the old row instance as the one before you scroll. The initwidget is the method which is necessary to be used for updating the widget created in createwidget and in initwidget the current parameters are passed.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/


    chzhong1001
    Participant

    Hi admin,
    In fact, I want to be able to get row data in the button click event.For example, in the following “myButton.addEventHandler(‘click’, function ()”.
    How do I do that?

    createwidget: (row: number, column: any, value: string, htmlElement: HTMLElement): void => {
    const container = document.createElement(‘div’);
    const id = myButton${this.counter};
    container.id = id;
    container.style.border = ‘none’;
    htmlElement.appendChild(container);
    const imgurl = ‘./../../../images/’ + value.toLowerCase() + ‘.png’;
    const options = {
    width: ‘100%’, height: 90, template: ‘success’,
    imgSrc: imgurl, imgWidth: 40, imgHeight: 50, value: value,
    imgPosition: ‘center’, textPosition: ‘center’, textImageRelation: ‘imageAboveText’
    };
    const myButton = jqwidgets.createInstance(#${id}, ‘jqxButton’, options);
    myButton.addEventHandler(‘click’, function (): void {
    const currentButtonValue = this.children[1].innerHTML;
    alert(row.boundindex); // does not match when scrolling
    alert(row.bounddata); // does not match when scrolling
    alert(currentButtonValue);
    });
    this.counter++;
    },
    initwidget: (row: number, column: any, value: any, htmlElement: HTMLElement): void => {
    const imgurl = ‘./../../../images/’ + value.toLowerCase() + ‘.png’;
    (<HTMLImageElement>htmlElement.children[1].children[0]).src = imgurl;
    htmlElement.children[1].children[1].innerHTML = value;
    }


    admin
    Keymaster

    Hi chzhong1001,

    Instead of that, you can do that in initwidget:

    myButton.element.onclick = () => {
    }

    In that case, the row index will be ok.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/


    chzhong1001
    Participant

    Hi admin,
    This way it can be solved.
    Thanks very much.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.