Individual column seraching (Select Input) not working properly with html

Individual column seraching (Select Input) not working properly with html

harender_24harender_24 Posts: 9Questions: 3Answers: 0

I am using datatable in my table and my table td contains html(label) tags but dropdown is not showing unique values. How could we achieve that? Please check my Fiddle

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    You're getting console errors in this line:

              select.append( '<option value="'+d.split(">")[1].split("<")[0]+'">'+d.split(">")[1].split("<")[0]+'</option>' )
    

    You need to diagnose that - those <label> elements are only in the first column.

  • harender_24harender_24 Posts: 9Questions: 3Answers: 0
    edited October 2019

    Hi Colin
    I have updated the Fiddle with complete HTML and everything is working fine except the thead dropdown not showing unique values.
    Updated the <label> in html

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    I'm not sure if you pasted the right fiddle, but that's still giving the "Uncaught TypeError: Cannot read property 'split' of undefined", and the code looks the same.

  • harender_24harender_24 Posts: 9Questions: 3Answers: 0

    Apologies for that Colin. I thought i saved it. Updated Link

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @harender_24 ,

    The reason is because the column data is the data within the <td> element. In your case, this is:

    <label id="name1">Tiger Nixon</label>
    <label id="name2">Tiger Nixon</label>
    

    As the id is unique, you get both lines. You need to strip out the HTML element before checking for uniqueness.

    Cheers,

    Colin

  • harender_24harender_24 Posts: 9Questions: 3Answers: 0

    Hi Colin
    How should i check in datatable code? I tried to put before Unique() in Column.data() as column.data().text().unique() but, it is not working. Could you suggest?

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    I tried to put before Unique() in Column.data() as column.data().text().unique() but, it is not working

    See my last reply - the ID is preventing the data being unique. You need to form your own array of strings without the labels (you're doing it individually already to get the string for the select) - then do something like this to ensure uniqueness.

This discussion has been closed.